+ 1
Can someone help sith this python recursion?
def convert(num): return (num % 2 + 10 * convert(num // 2))
2 Respostas
+ 6
# Hi, Marina Nikolic!
# You just have to add a base case:
def convert(num):
# Base case: if the number is 0, return 0.
if num == 0:
return 0
else:
# Recursive case: compute binary representation.
return num%2 + 10*convert(num // 2)
# Test the function with an example.
print(convert(5)) # Expected output: 101
+ 5
To sith it, you must show it the power of the dark side.