What number will the following Python code print?
1 2 3 4 5 6 7 8 9 |
|
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.
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.
6 how?????????
Log in to reply
The code count the unequal letters
run it in python, easy!!
how it is/??????????????????????????????
I copied this off of one of my Python files and it runs there and returns 6 . Can you please be more specific as to where the problem is?
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.
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?
Log in to reply
@Trevor B. – And the last line must be print (count) not print count
Log in to reply
@Mateo Torres – No, only if you're using the latest version of Python (3.2?)
Log in to reply
@Minimario Minimario – I'm using Python 3.3.3.
Log in to reply
@Mateo Torres – It doesn't work for Python 3.3.3.
Log in to reply
@Edward Jiang – It works for 2.7.2 (although I don't use it)
Log in to reply
@Minimario Minimario – please replace ‘ ‘ ‘ p y t h o n p r i n t c o u n t ‘ ‘ ‘ by ‘ ‘ ‘ p y t h o n p r i n t ( c o u n t ) ‘ ‘ ‘ It creates trouble for beginners like me.
yaa it shud print error hw do u get 6?????????
how did u get 6??
Log in to reply
Counted what ever letters were unequal : "This Vs. Here" and "Of Vs. In"
different letters count!!! Answer 6
Python usually doesn't require explicit type declarations for simple things
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 !
the difference between two string is 'here' and 'in' and the number of characters in this two words is 6 therefor answer is 6
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
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 -)
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.
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.
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)
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
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;
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
I don't learn pyhton but after reading the code i'm getting interesting in python
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
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
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.
because it counts different letters..
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++
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
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.
The program just counts dissimilar characters in both the strings.
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
Problem Loading...
Note Loading...
Set Loading...
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.