Created
June 23, 2013 18:27
-
-
Save marsam/5845978 to your computer and use it in GitHub Desktop.
reversebinary spotify puzzle. https://www.spotify.com/int/jobs/tech/reversed-binary/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Reversed Binary Numbers | |
~~~~~~~~~~~~~~~~~~~~~~~ | |
Yi has moved to Sweden and now goes to school here. The first years of | |
schooling she got in China, and the curricula do not match completely in the | |
two countries. Yi likes mathematics, but now... The teacher explains the | |
algorithm for subtraction on the board, and Yi is bored. Maybe it is possible | |
to perform the same calculations on the numbers corresponding to the reversed | |
binary representations of the numbers on the board? Yi dreams away and starts | |
constructing a program that reverses the binary representation, in her mind. As | |
soon as the lecture ends, she will go home and write it on her computer. | |
Task | |
Your task will be to write a program for reversing numbers in binary. For | |
instance, the binary representation of 13 is 1101, and reversing it gives 1011, | |
which corresponds to number 11. | |
Input | |
The input contains a single line with an integer N, 1 ≤ N ≤ 1000000000. | |
Output | |
Output one line with one integer, the number we get by reversing the binary | |
representation of N. | |
Sample input 1 Sample output 1 | |
13 11 | |
Sample input 3 Sample output 2 | |
47 61 | |
""" | |
from functools import partial | |
to_decimal = partial(int, base=2) | |
def to_binary(number): | |
if number == 0: | |
return '' | |
else: | |
return to_binary(number // 2) + str(number % 2) | |
if __name__ == '__main__': | |
import sys | |
for line in sys.stdin: | |
print(to_decimal(to_binary(int(line))[::-1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment