Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Back to basics solution in Scary category for Multiply (Intro) by grillo
def mult_two(a, b):
def succ(x):
"""Returns the successor of a natural number."""
x += 1
return x
def pred(x):
"""Returns the predecessor of a natural number."""
if x > 0:
x -= 1
return x
def add_two(x, y):
"""Returns the sum of two natural numbers using Peano's recursive definition of sum."""
return add_two(succ(x),pred(y)) if y>0 else x
return add_two( a, mult_two(a, pred(b) ) ) if b > 0 else 0
if __name__ == '__main__':
print("Example:")
print(mult_two(3, 2))
# These "asserts" are used for self-checking and not for an auto-testing
assert mult_two(3, 2) == 6
assert mult_two(1, 0) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
March 3, 2020
Comments: