last day (24 days later) » 

9:43 PM
1
A: How to get where the binary representation is 1?

Felipe FariaYou could do something alone these lines: # Split the binary and grab interested part, and its size. # >> bin(123456789) == "0b111010110111100110100010101" binary = bin(123456789)[2:] size = len(binary) numbers = [] for i in range(size): # Checks to see if the binary[i] is not equals to zer...

 
What do you mean by "faster", and what is your metric so I can try to help out with more context? :)
@JasonC It's a very powerful language, and very much so worth checking it out!
@EAB Added a few shorter solutions for the sake of it. Might be of help, and second one might even be a tiny bit faster than the first. :)
Of course. I'm sure there is a method to do it with bitwise operators but since this is a simpler problem I didn't want to over-complicate it.
 
This answer is baffling. Why did you choose this kind of string manipulation? A few notes: .split(“0b”) should be .split(“0b”, maxsplit=1). if binary[i] is not “0” should be if binary[i] == “1”. Why aren’t you using ‘enumerate()` instead of range()? Why are you writing int(binary[i]... when at that point in the program you know that binary[i] is 1? And for the love of god, why do you think that this solution is less complicated than using exponentiation!?
 
bin(x) always returns a string with a prefix 0b. The if-statement portion of your criticize, != 0 allows for better generalization when changing to another base on int(..., 2), and to your third point because this is more concise and clear for fellow devs. Many solutions to this problem -- caching a dict, bitwise operations, exponentiation, list manipulation.
 
What part of my comment is “bin(x) always returns...” meant to address? If it is the part concerning .split(“0b”), I now realize that I should have suggested you use bin(...)[2:] instead. As for the if statement, the critical part is the use of is instead of ==, not what you are comparing it to (i.e: 0 or 1).
 
Regarding your first point, using [2:] would indeed be a bit faster -- will update post. On the second point, is and == works the same with strings since Python uses string interning (which is a faster operation), but I can see why you might hold reservations against it
 
9:43 PM
@FelipeFaria It’s not a matter of performance, it’s about clarity and using the right tool for the job. You want to get rid of the first two characters of a string. It’s always the same two first characters. That’s not what split is for. As for is vs ==, the fact that it works is irrelevant. We want to check equality, not identity. It just so happens to be that those two operations produce the same result when it comes to strings. That is an exception. Using is to check for equality just because it works for this particular case can only lead to confusion and bad habits.
@FelipeFaria Oh, and check out the result of print(str_1 == str_3, str_1 is str_3) for str_1 = “aaa”, str_2 = “a” and str_3 = str_2 * 3.
 
@AlexanderCécile My man, you goin' on a rant. I saw the post, I suggested a possible solution (albeit, not the fastest), and inquired to the OP about what his metrics were. Your argument for .split() is sound, but at the end of the day, I wrote for clarity's sake. Regarding the isvs== debate, perhaps I should have clarified it "works the same in this context."
There is really no "proper way" to program Python, and to see you run with this argument is saddening. When you work with other devs in projects you will realize everyone programs differently. Get used to suggesting over intimidating others with solutions -- it works out best for all the parties involved.
 
@FelipeFaria Is this how you are going to react? How dare you pretend that my comments on is vs == are purely dogmatic? You think that "there is no one true way" means "anything goes" or "all ways are perfectly equivalent"? How can you claim that I am forcing my way onto you? None of my comments were aggressive or mean or anything of the sort.
Throughout all of this, I have provided multiple arguments in favour of using == over is. The only response you've given is "it technically works". You want to give me advice on working in a team? How about you learn not to take any criticism or suggestions on your work as a personal attack.
Hi
 

  last day (24 days later) »