Lecture "Recursion", exercise 1
Opened this issue · 19 comments
essepuntato commented
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.
diegochillo commented
def test_exponentiation(base_number,exponent,expected):
result=exponentiation(base_number,exponent)
return result==expected
def exponentiation(base_number,exponent):
if not (exponent):
return 1
else:
return base_number * exponentiation(base_number, exponent - 1)
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
SarahTew commented
I wrote this and it does work, though not for negative exponents so I have it return a string saying the exponent is negative when that's an input. Is that right? Is there a way to make it work with negative exponents as well? Here is the link to PythonTutor if you want to see how it works.
def exponentiation(base_number, exponent):
if exponent < 0:
return "Negative Exponent!"
elif exponent == 0:
return 1
elif exponent == 1:
return base_number
else:
return base_number * exponentiation(base_number, exponent - 1)
print(exponentiation(3,4))
print(exponentiation(17,1))
print(exponentiation(2,0))
print(exponentiation(-2,3))
print (exponentiation(3,-3))
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))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
SusannaPinotti commented
def test_exponential(base_number, exponent, expected):
result = exponential(base_number, exponent)
if expected == result:
return True
else:
return False
def exponential(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * (exponential(base_number, exponent -1))
print(test_exponential(3, 4, 81)) #returns True
print(test_exponential(17, 1, 17)) #returns True
print(test_exponential(2, 0, 1)) #returns True
dbrembilla commented
#test
def test(base_number, exponent, expected):
return expected == exponentiation(base_number, exponent)
#exponential function
def exponentiation(base_number, exponent):
if exponent == 0:
if base_number == 0:
return "Impossible."
elif base_number != 0:
return 1
elif exponent == 1:
return base_number
else:
return base_number * exponentiation(base_number, exponent - 1)
print(test(17, 1, 17)) #returns True
print(test(3, 4, 81)) #returns True
print(test(2, 0, 1)) #returns True
gabrielefiorenza commented
def test_exponentiation (base_number,exponent, expected):
result = exponentiation(base_number,exponent)
return result == expected
def exponentiation(base_number,exponent):
if not exponent:
return 1
else:
return base_number * exponentiation(base_number, exponent-1)
print(test_exponentiation(3,4,81)) #returns True
print(test_exponentiation(17,1,17)) #returns True
print(test_exponentiation(2,0,1)) #returns True
AlessandraFa commented
def test_exponentiation(base_number, exponent, expected):
return expected == exponentiation(base_number, exponent)
def exponentiation(base_number, exponent):
if base_number == 0 and exponent == 0:
return None
elif exponent == 0:
return 1
elif exponent < 0:
exponent = exponent * -1
return 1/(base_number * exponentiation(base_number, exponent - 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
IlaRoss commented
#tests
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if result == expected:
return True
else:
return False
#algorithm
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
elif exponent > 0:
return base_number * exponentiation(base_number, exponent - 1)
elif exponent < 0:
return 1 / base_number * exponentiation(base_number, exponent + 1)
#test runs
a = test_exponentiation(3, 4, 81)
b = test_exponentiation(17, 1, 17)
c = test_exponentiation(2, 0, 1)
print(a)
print(b)
print(c)
yunglong28 commented
# Test function
def test_exponentiation (base_number, exponent, expected):
result = exponentiation (base_number, exponent)
if result == expected:
return True
else:
return False
# Recursive function for calculating a power
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
LuisAmmi commented
def test_exponentiation(base_n, exp, expected):
result = exponentiation(base_n, exp)
if result == expected:
return True
else:
return False
def exponentiation(base_n, exp):
if exp == 0:
return 1
else:
return base_n * exponentiation(base_n, exp - 1)
print(test_exponentiation(3, 4, 81)). #true
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
ChiaraCati commented
def test_exp(base, exponent, expected):
result = exp(base, exponent)
if result == expected:
return True
else:
return False
def exp(base, exponent):
if exponent >= 0: # for exponent
if exponent == 1:
return base
elif exponent > 1:
return base * exp(base, exponent - 1)
else:
return 1
if exponent < 0:
positive = 1 / base
if exponent == -1:
return positive
else:
return round(positive * exp(base, exponent + 1), 2)
print(test_exp(3, 4, 81))
print(test_exp(17, 1, 17))
print(test_exp(2, 0, 1))
print(test_exp(4, -2, 0.06))
edoardodalborgo commented
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))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
AleRosae commented
def test_exp(base_number, exponent, expected):
return expected == exponentiation(base_number, exponent)
def exponentiation (base_number, exponent):
if exponent == 0:
return 1
elif exponent == 1:
return base_number
elif exponent<0:
return 1/(base_number)*(exponentiation(base_number,exponent+1 ))
else:
return base_number * exponentiation(base_number, exponent-1)
print(test_exp(3,4,81))
print(test_exp(17,1,17))
print(test_exp(2,0,1))
print(test_exp(2,-4,0.0625))
fcagnola commented
def test_2_parameter(function, parameter1, parameter2, expected):
result = function(parameter1, parameter2)
return result == expected
def exponentiation(base, exponent):
if exponent < 0: # if exponent is negative do not run function, but return error message
return "exponent must be a positive integer"
if exponent == 0: # base case, anything to the power of 0 returns 1
return 1
else: # recursive function, returns multiplication of base * each run's result
return base * exponentiation(base, exponent-1)
print(test_2_parameter(exponentiation,3,4,81)) # returns True
print(test_2_parameter(exponentiation,17,1,17)) # returns True
print(test_2_parameter(exponentiation,2,0,1)) # returns True
SofiBar commented
def test_exponentiation(base_number, exponent, expected):
return exponentiation(base_number, exponent) == expected
def exponentiation(base_number, exponent):
if exponent == 1:
return base_number
elif exponent == 0:
return 1
elif exponent < 0:
return 1/exponentiation(base_number, -1*exponent)
else:
return base_number*exponentiation(base_number, exponent-1)
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
giorgiasampo commented
def` test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
return result == expected
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))
print (test_exponentiation(17, 1, 17))
print (test_exponentiation(2, 0, 1))
vanessabonanno commented
def test_exponentiation(num, exp, expected):
result = exponentiation(num, exp)
if result == expected:
return True
else:
return False
def exponentiation(num, exp):
if exp == 0:
return 1 # base condition
elif exp == 1:
return num
elif exp > 0:
return num * exponentiation(num, exp - 1)
elif exp < 0:
return 1 / num * exponentiation(num, exp + 1)
else:
return "Wrong input"
print(test_exponentiation(3, 4, 81)) # True
print(test_exponentiation(17, 1, 17)) # True
print(test_exponentiation(2, 0, 1)) # True
laurentfintoni commented
def test_exponentation(int_1, int_2, expected):
result = exponentation(int_1, int_2)
if result == expected:
return True
else:
return False
def exponentation(int_1, int_2):
if int_2 == 0:
return 1
else:
return int_1 * exponentation(int_1, int_2 - 1)
print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(2, 0, 0))
AlessandroBertozzi commented
# Test case for the algorithm
def test_exponentiation(int_1, int_2, expected):
result = exponentiation(int_1, int_2)
if expected == result:
return True
else:
return False
# Code of the algorithm
def exponentiation(int_1, int_2):
if int_2 == 0:
return 1
else:
return int_1 * exponentiation(int_1, int_2 - 1)
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
GiuliaMenna commented
def test_exp(base_number, exponent,expected):
result = exp(base_number, exponent)
if expected == result:
return True
else:
return False
def exp(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * exp(base_number, exponent - 1)
return result
print(test_exp(3,4,81))
print(test_exp(17,1,17))
print(test_exp(2,0,1))