Open essepuntato opened 4 years ago
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
elif exponent == 1:
return base_number
elif exponent == 2:
return base_number*base_number
else:
return base_number*exponentiation(base_number, exponent-1)
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if result == expected:
return True
else:
return False
print(test_exponentiation(3, 4, 81)) # True
print(test_exponentiation(17, 1, 17)) # True
print(test_exponentiation(2, 0, 1)) # True
def test_exponentiation(base_number, exponent, expected):
if exponentiation(base_number, exponent) == expected:
return True
else:
return False
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
elif exponent == 1:
return base_number
elif exponent == 2:
return base_number * base_number
else:
return base_number * exponentiation(base_number, exponent - 1)
print(test_exponentiation(3, 4, 81)) # True
print(test_exponentiation(17, 1, 17)) # True
print(test_exponentiation(2, 0, 1)) # True
Here I am considering the value of 0^0 in algebra, namely 0^0=1. If we were to consider it from the point of view of mathematical analysis, we would have to leave it undefined, but then I wouldn't really know how to implement the recursive element.
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if result == expected:
return True
else:
return False
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * exponentiation(base_number, exponent - 1)
print(test_exponentiation(3, 4, 81)) #True
print(test_exponentiation(17, 1, 17)) #True
print(test_exponentiation(2, 0, 1)) #True
print(test_exponentiation(0, 0, 1)) #True
print(test_exponentiation(1, 0, 1)) #True
print(test_exponentiation(0, 2, 0)) #True
True
True
True
Hi all,
please find attached my personal solution – also available online:
# Test case for the algorithm
def test_exponentation(base_number, exponent, expected):
result = exponentation(base_number, exponent)
if expected == result:
return True
else:
return False
# Code of the algorithm
def exponentation(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * exponentation(base_number, exponent - 1)
print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(2, 0, 1))
print(test_exponentation(0, 15, 0))
print(test_exponentation(0, 0, 1))
Define a recursive function
def exponentiation(base_number, exponent)
for implementing the exponentiation operation. Test (by implementing the related test case) it on the following inputs: 34, 171, and 20.