Skip to content

Instantly share code, notes, and snippets.

@KorbenC
Created September 12, 2013 06:01
Show Gist options
  • Save KorbenC/6533498 to your computer and use it in GitHub Desktop.
Save KorbenC/6533498 to your computer and use it in GitHub Desktop.
Spotify Reversed Binary Numbers Puzzle 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…
#!/usr/bin/env python
from __future__ import with_statement
import threading
import time
import sys
# Implementation of Ticker class
class Ticker(threading.Thread):
def __init__(self, msg):
threading.Thread.__init__(self)
self.msg = msg
self.event = threading.Event()
def __enter__(self):
self.start()
def __exit__(self, ex_type, ex_value, ex_traceback):
self.event.set()
self.join()
def run(self):
sys.stdout.write(self.msg)
while not self.event.isSet():
sys.stdout.write(".")
sys.stdout.flush()
self.event.wait(1)
# Implementation of binary number
def get_binary_number(num):
bin_num = ''
reverse_bin_num = ''
remainder = 0
while(num > 0):
remainder = num % 2
num = num / 2
bin_num += str(remainder)
with Ticker("\nBinary Number"):
time.sleep(2)
sys.stdout.write(bin_num)
reverse_bin_num = bin_num[::-1]
with Ticker("\nReverse Binary Number"):
time.sleep(2)
sys.stdout.write(reverse_bin_num)
return get_reverse_binary_number(reverse_bin_num)
#return reverse_bin_num0
# Implementation of reverse binary number
def get_reverse_binary_number(reverse_bin_num):
multiplier = 0
number = 0
for el in reverse_bin_num:
number += int(el) * (2**multiplier)
multiplier += 1
return number
# Implementation of main
def main():
bin = input("Enter a number: ")
reversed = get_binary_number(bin)
with Ticker("\nReverse Decimal"):
time.sleep(2)
sys.stdout.write(str(reversed))
sys.stdout.write("\n")
if __name__ == '__main__':
status = main()
sys.exit(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment