+ 1
Why does this code produce duplicate output?
3 Respostas
+ 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 😊
+ 6
haha yes, I agree @Oma 😊 plus, smart people aren't afraid to ask 😊👍
+ 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.