Sep-08-2019, 10:22 AM
Another approach could be using divmod:
def to_binary(num): reminders = [] while num: num, reminder = divmod(num, 2) reminders.append(reminder) return ''.join(str(x) for x in reversed(reminders),)To test:
>>> to_binary(42) '101010' >>> int(to_binary(42), 2) 42
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.