Open essepuntato opened 2 days ago
# Code
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * exponentiation(base_number, exponent - 1)
# Test case
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if expected == result:
return True
else:
return False
# Test runs
print(test_exponentiation(3, 4, 81)) # True
print(test_exponentiation(17, 1, 17)) # True
print(test_exponentiation(2, 0, 1)) # True
[
](url)
def test_exponentiation(base_num, exponent, expected):
if exponentiation(base_num, exponent) == expected:
return True
else:
return False
def exponentiation(base_num, exponent):
if exponent == 0:
return 1
else:
return base_num * exponentiation(base_num,exponent-1)
print(test_exponentiation(3,4,81))
#True
print(test_exponentiation(17,1,17))
#True
print(test_exponentiation(2,0,1))
#True
I uploaded this from my phone as I don’t have my laptop this week so if there are any errors please let me know
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if expected == result:
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(20,0,1))
#True
def test_exponentiation(base_number, exponent, expected):
expected = base_number ** exponent
result = exponentiation(base_number, exponent)
if expected == result:
return True
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
elif exponent < 0:
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 (exponentiation(3,4))
print (test_exponentiation(17,1,17))#TRUE
print (exponentiation(17,1))
print (test_exponentiation(2,0,1))#TRUE
print (exponentiation(2,0))
print (test_exponentiation(2,-1,0.5))#TRUE
print (exponentiation(2,-1))
# Testing the function
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if expected == result:
return True
else:
return False
# Code
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
else:
res = exponentiation(base_number, exponent - 1)
return base_number * res
print(test_exponentiation(2, 0, 1))
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(exponentiation(2, 3))
print(exponentiation(2, 8))
def test_exponentiation(base_number, exponent, expected):
result=exponentiation(base_number,exponent)
if result!=expected:
return False
else:
return True
def exponentiation(base_number, exponent):
if exponent == 0:
if base_number == 0:
return "indefinito"
return 1
if exponent < 0:
return 1 / (base_number * exponentiation(base_number, -(exponent) - 1))
else:
return base_number * exponentiation(base_number, exponent - 1)
test_exponentiation(0, 0, "indefinito")
#True
test_exponentiation(2, 1, 2)
#True
test_exponentiation(2, 4 16)
#True
test_exponentiation(-2, 2, 4)
#True
test_exponentiation(2, -2, 1/4)
#True
def test_case(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 0
else:
return base_number + exponentiation(base_number, exponent-1)
print(test_case(3, 4, 12))
print(test_case(17, 1, 17))
print(test_case(2, 0, 0))
True
True
True
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.