Not that long ago I discovered the Spotify puzzles and thought they would be a good excuse to improve my limited Python knowledge.
So here are my attempts at solving the ”Reverse” and “Zipfsong” puzzles in Python:
Reverse:
#!/usr/bin/env python
import sys
for number in sys.stdin:
print int('{:b}'.format(int(number))[::-1], 2)
Zipfsong:
#!/usr/bin/env python
import sys
import heapq
num_songs, num_select = (int(n) for n in sys.stdin.readline().split())
songs = []
for line_num in xrange(1, num_songs + 1):
plays, song = sys.stdin.readline().split()
quality = int(plays) * line_num
heapq.heappush(songs, (-quality, line_num, song))
for i in xrange(num_select):
print heapq.heappop(songs)[2]
I’m quite new to Python so this might not be the best Python code in the world. If you can think of a way it could be improved though, I would love to know! Just leave a comment.