Why does this code produce duplicate output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does this code produce duplicate output?

https://code.sololearn.com/cLTRJYUrkbr1

12th Oct 2017, 1:11 PM
Oma Falk
Oma Falk - avatar
3 Answers
+ 11
It's just an illusion, by using the end of the list indexing "-1" it is not returning the last element of each binary number of your range, which goes from one to eleven. I mean this part of the code: bin(i)[2:-1] ) For example: - The binary number of ONE is "1" so it returns empty because that 1 will never be printed as it is the last index. - The binary number of TWO is "10" so zero won't be returned, therefore only 1 gets printed. -The binary number of THREE is "11" so the 1 at the right won't be returned, therefore only 1 gets printed. -The binary number of FOUR is "100" so the zero at the right won't be returned, therefore only 10 gets printed. -The binary number of FIVE is "101" so the 1 at the right won't be returned, therefore only 10 gets printed, and so on... If you erase that "-1" on the code and leave it like this: bin(i)[2:] ) you will see that the complete binary number gets printed 😊 Hope I was able to explain it well 😊
13th Oct 2017, 9:34 AM
Pao
Pao - avatar
+ 6
haha yes, I agree @Oma 😊 plus, smart people aren't afraid to ask 😊👍
13th Oct 2017, 9:04 PM
Pao
Pao - avatar
+ 2
@Paola LOL .... of course .. I see it now too in sololearn we so often discuss skills which programmers need. One important necessary thing is that special kind of humor you need for situations like this.
13th Oct 2017, 10:17 AM
Oma Falk
Oma Falk - avatar