Skip to content

Instantly share code, notes, and snippets.

@fabian57
Last active August 29, 2015 14:11
Show Gist options
  • Save fabian57/579c015244019ff7bd1c to your computer and use it in GitHub Desktop.
Save fabian57/579c015244019ff7bd1c to your computer and use it in GitHub Desktop.
def print_halves(n):
print n
while n > 0:
n = n / 2
print n
print_halves(57)
def binary_length(n):
i = 0
while n > 0:
n = n / 2
i += 1
return i
binary_length(57)
def binary_list(n):
acc = []
while n > 0:
acc += [n % 2]
n = n / 2
return acc
binary_list(57)
def binary_string(n):
acc = ""
while n > 0:
acc = str(n % 2) + acc
n = n / 2
return acc
binary_string(57)
@laowantong
Copy link

Parfait, a ceci près que les trois dernières devraient être des fonctions sans effet de bord (remplace print par return).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment