Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Knute Snortum
• Paul Clapham
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Frits Walraven
Bartenders:
• Ganesh Patekar
• Tim Holloway
• salvin francis

# Easy python problem

Ranch Hand
Posts: 124
1
Hello everyone!
This easy problem took me quite a long time to figure out, partly because of the conversion back from an integer that looks like a binary to a integer (you'll see what I mean), mostly because I know nothing!

Can you please help me to write a more "pythonic" or at least elegant solution? (one without a for loop)

Marshal
Posts: 64471
225
• 1
Please start by telling us what the application is supposed to do. Are you taking integer input in decimal, converting it to binary, reversing the digits and parsing them as a number? What happens to leading and trailing 0s? What is the reversal of 0000_0000_ 0000_0000_ 0000_0000_0010_1010 (=42 in decimal)? Is it 10101 or 0101_0100_0000_ 0000_0000_ 0000_0000_0000?

D.J. Quavern
Ranch Hand
Posts: 124
1
Oh, I'm sorry, I forgot the problem.
It's about a girl who daydreams in class, converts numbers to binary, revert them and convert the new "binary" (which is an int) to the corresponding decimal interpretation.
https://open.kattis.com/problems/reversebinary

Campbell Ritchie
Marshal
Posts: 64471
225
The only thing girls daydream in class about is boys.

So leading 0s are deleted before reversal. I suggest you write down on paper how you would solve that problem.

Saloon Keeper
Posts: 3250
128

D.J. Quavern wrote:Can you please help me to write a more "pythonic" or at least elegant solution? (one without a for loop)

hi DJ,

there is a short solution that does not use a loop. Have a look at the documentation of the 'int'  method. You will see a version that is very suitable here. That version has more than one parameter.

D.J. Quavern
Ranch Hand
Posts: 124
1

Campbell Ritchie wrote:The only thing girls daydream in class about is boys.

Lol    you watch too much movies written by men who think they know all there is to know about women. We have definitely other stuff on our minds than the opposite sex .

Campbell Ritchie wrote:So leading 0s are deleted before reversal. I suggest you write down on paper how you would solve that problem.

I did wrote on paper. I keep all the binary except 0b by slicing, so I don't think I lost leading zeros (?)

Piet Souris wrote:  there is a short solution that does not use a loop. Have a look at the documentation of the 'int'  method. You will see a version that is very suitable here. That version has more than one parameter.

Neat! I found it!

Campbell Ritchie
Marshal
Posts: 64471
225
• 1

D.J. Quavern wrote:. . . you watch too much movies

No, I don't

. . . men who think they know all there is to know about women. . . .

Neat! I found it! . . .

Well done

Campbell Ritchie
Marshal
Posts: 64471
225
• 1
I'm sorry

D.J. Quavern
Ranch Hand
Posts: 124
1
Thank you for your empathy and understanding

D.J. Quavern
Ranch Hand
Posts: 124
1
Look at that! My very first own cow ! Thank you for giving her
I will take good care of her!

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?