Decode These Python Strings

What number will the following Python code print?

1
2
3
4
5
6
7
8
9
count = 0
sequence_1 = 'ThisIsAStringOfCode'
sequence_2 = 'HereIsAStringInCode'

for i in range(0,len(sequence_1)):
    if sequence_1[i] != sequence_2[i]:
        count = count + 1

print count


The answer is 6.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

25 solutions

Jonathan Wong
Dec 23, 2013

Technically it will print an error because neither sequence is defined as a string.

In any case, so long as that fixed, != means not equal to, so the function compares the ith character in each sequences and counts the number of differing pairs.

True, but the answer would be 6 if they were. Also, seeing as Brilliant accepts answers from 0 to 999 inclusive, you can't enter "Traceback (most recent call last): File "python", line 2, in <module> NameError: name 'ThisIsAStringOfCode' is not defined." (the error) so the only answer you can give that satisfies the question and brilliant's answering format would be 6.

Aaron Hunter - 7 years, 5 months ago

6 how?????????

Sarpreet Riar - 7 years, 4 months ago

Log in to reply

The code count the unequal letters

Mohamed Aly - 7 years, 4 months ago

run it in python, easy!!

Dev Ashish - 7 years, 1 month ago

how it is/??????????????????????????????

sriram bodavula - 7 years, 3 months ago

I copied this off of one of my Python files and it runs there and returns 6 6 . Can you please be more specific as to where the problem is?

Trevor B. - 7 years, 5 months ago

Log in to reply

The problem is on lines two and three. You are defining sequence 1 and sequence 2 as strings but are not "wrapping" them in quotation marks which causes the error: Traceback (most recent call last): File "python", line 2, in NameError: name 'ThisIsAStringOfCode' is not defined. because it thinks ThisIsAStringOfCode is a variable because you have not "wrapped" it in quotes.

Aaron Hunter - 7 years, 5 months ago

Log in to reply

Oh, I see it now. I replaced the variable but forgot the quotes. Can an admin please add in the quotes?

Trevor B. - 7 years, 5 months ago

Log in to reply

@Trevor B. And the last line must be print (count) not print count

Mateo Torres - 7 years, 5 months ago

Log in to reply

@Mateo Torres No, only if you're using the latest version of Python (3.2?)

minimario minimario - 7 years, 5 months ago

Log in to reply

@Minimario Minimario I'm using Python 3.3.3.

Mateo Torres - 7 years, 5 months ago

Log in to reply

@Mateo Torres It doesn't work for Python 3.3.3.

Edward Jiang - 7 years, 5 months ago

Log in to reply

@Edward Jiang It works for 2.7.2 (although I don't use it)

minimario minimario - 7 years, 5 months ago

Log in to reply

@Minimario Minimario please replace p y t h o n p r i n t c o u n t ```python print count ``` by p y t h o n p r i n t ( c o u n t ) ```python print(count) ``` It creates trouble for beginners like me.

Rushikesh Jogdand - 6 years, 2 months ago

yaa it shud print error hw do u get 6?????????

Sowmy Vivek - 7 years, 4 months ago

how did u get 6??

Akash Agarwal - 7 years, 3 months ago

Log in to reply

Counted what ever letters were unequal : "This Vs. Here" and "Of Vs. In"

Venugopal A K - 7 years, 3 months ago

different letters count!!! Answer 6

kashif ali - 7 years, 3 months ago

Python usually doesn't require explicit type declarations for simple things

Jonathan Pritchard - 6 years, 2 months ago
Roger Huang
Dec 25, 2013

The formula is a for loop in Python that checks each character in both strings to see if there is a difference between the characters in the two strings.

It will add +1 to the count every time there is such a difference.

i=0: T and H differ

i=1: h and e differ

i=2: i and r differ

i=3: s and e differ

i=13: O and I differ

i=14: f and n differ

Therefore, because there are 6 characters that differ, printing count will yield 6.

Thanks, Now i got it !

Amira Andromida - 7 years, 4 months ago

the difference between two string is 'here' and 'in' and the number of characters in this two words is 6 therefor answer is 6

Neha Mangla
Jan 21, 2014

As length of sequence 1 is 19. So loop will executed 20 times as it started with a value of 0 for i. Now if character of both sequence 1 and sequence_2 at same position matches, if condition will execute and increase count value with 1. As we can see, for only 6 character if condition set to true. So count value will be 6

Mohammad Saad
Jan 12, 2014

Here we have two strings. They can also be described as an array of characters, which is what the problem is about. The for loop loops through the first string's length (identical to the length of the second string) and compares each character in the two strings. If they differ, then count is incremented, if not, nothing happens.

From there, we see that the two strings only differ at 6 points ("This" and "Here", and "Of" and "In") telling us that there are six places where the strings are different. Therefore, the answer is 6.

I was going to say the same thing thank you for saving me from same typing -)

Branislav Dinic - 7 years, 2 months ago
Victor Soerensen
Jan 7, 2014

I read through the code. It simply prints out the number of letters in sequence_1 that are different from the corresponding letters in sequence_2 . It compares char 1 of sequence_1 to char 1 of sequence_2 , char 2 to char 2, char 3 to char 3, etc.

Once I knew this, and since the values have been so nicely formated for me (thanks!), I simply counted the corresponding letters.

I must raise my hand in protest, however. The program will not print this out, as it currently stands. The reason is that there are 4 syntax errors that should be fixed before it runs. You see, it is missing a double quote before and after every literal string. (sequence 1 and sequence 2). As it is, Python just chockes and dies in error.

Swathi Gudupu
Jan 7, 2014

len(sequence 1) is 18. count=0. The number of different characters in sequence 1=count. Here 6 characters are different in both the strings... i.e, "This" and "here" 4 characters are different , "of" and "In" are different 2 characters. Totally 6 characters are different. Count increases to 6. So answer is 6.

Toby Hayes
Jan 7, 2014

Simple string comparison letter by letter:

T != H h != e i != r s != e I == I s == s and so on

There are six letters that do not match up by position, each of which increments the count variable by one: This/Here (Positions 0 to 4) and Of/In (Positions 12 to 13)

Prasun Biswas
Dec 28, 2013

In this program, we see that both strings have same lengths. count=0 at first. The "for loop" with i as counter variable runs from i=0 to i=String length. In the "if loop", we see that it is checked that the characters of the same position in both strings are equal or not. If not equal, count variable is incremented. The first 4 letters of both strings satisfy the condition as each letter of one string is different from the character in that position of the other string. So, count becomes 4 at the end of checking the 4th position characters with i=3. After that, strings are having same letters in corresponding positions so the condition is now false. Then at the time of checking of the parts "Of" and "In" of the strings, the condition is again fulfilled and count increments to 5 and then 6. The rest of the string does not satisfy the condition and thus count is not further incremented. So, count=6 at end and "print count" prints the value of count = 6 =\boxed{6}

Manoj Tiwari
Oct 25, 2017

If it works correctly without any error than output of count value is 6

i know it's 6 but i wrote the same code in C++ to be assured:

string s1 = "ThisIsAStringOfCode";
string s2 = "HereIsAStringInCode";

int numb = 0;

for ( int i=0; i<s1.size(); i++)
    if (s1[i] != s2[i])
        numb++;
cout << numb;

cout << endl;
return 0;
Kshitij Dadhekar
Apr 18, 2014

It will compare each character in the first string with the second Then it will increment the count of non-matching characters which, in this case is 6

Angga Pranata
Apr 16, 2014

I don't learn pyhton but after reading the code i'm getting interesting in python

Naveen Damaraju
Mar 22, 2014

I am not bothered about syntax errors, I compiled with my brain. This problem is comparing the two strings, if the letter in string does not match count will increase, as "Here" and "In" are not matching answer is 6

Sudhanshu Kumar
Mar 21, 2014

here, len(sequence 1)=19 so the loop execute between 0-18 on the execution of loop each and every time sequence 1 compare to sequence_2 on comparison if they are not similar then count and increment by 1.

ThisIsAStringOfCode HereIsAStringInCode

here, sequence 1[0] != sequence 2[0] (t!=h) so count=0; sequence 1[1] != sequence 2[1] (h!=e) so count=1;

this execution will repeat until i becomes 18.

after execution of loop we get count=5; after looping count becomes count+1; so count=5+1=6

Vaibhav Zambad
Mar 21, 2014

In this code , the value of count is incremented every time whenever the character of sequence 1 matches the character of sequence 2 As there are 6 characters which are same in both the strings so the answer is 6.

Sadia Siddiqa
Mar 13, 2014

its 6 !!!

Hafeez Khoso
Mar 13, 2014

because it counts different letters..

Saddam Ranjhani
Mar 12, 2014

Length Of String 1 and 2 is same There is going in traversing of string so each different letter at same index will result in couter++

Kenith Aiyappa
Mar 8, 2014

Here the code counts non-matching letters of the 2 strings. The for loop iterates the for the length of the string (2 strings have same length) and the If condition within for loop checks whether each character in the 2 strings does not match each other , then the count is increased

Kabirul Islam
Mar 7, 2014

the answer must be 6

Sehaj Taneja
Mar 2, 2014

Simple Math.

This is fairly simple. The code looks at the first letter of each string, then the second letter and so on. Every time the two letters are different 1is added to count. This happens six times, so the answer is six.

Rahul Ahiray
Jan 27, 2014

The program just counts dissimilar characters in both the strings.

Renan Prata
Dec 27, 2013

In this case, count will be incremented when char sequence was different, then, this program willcount how many different characters in each string; In this case is 6.

the answer is 6

Dicksha Chohan - 7 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...