by d0mine · 10y ago · 0 comments
>>> n = int(bbs, 2)
>>> n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
's no i sn e h e r pm o c'
>>> _.replace(' ', '')[::-1]
'comprehensions'
http://stackoverflow.com/questions/7396849/convert-binary-to...I considered using the [::-1] syntax to reverse the list, but decided that there was enough "cute" stuff in the examples already.
def palindrome(s):
return s == s[::-1]
It could be discussed whether ''.join(reversed(s)) is more readable for a novice programmer learning Python. In general, Python prefers words over punctuation.Also, there are objects that can be reversed() that are not sequences.
http://stackoverflow.com/questions/931092/reverse-a-string-i...
import struct
result=''.join([chr(int(bits,2)) for bits in struct.unpack('8p'*(int(len(bbs)/8)),bbs)])
print(result.replace(' ','')[::-1])