-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Recursion", exercise 1 #24
Labels
Comments
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(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number*(exponentiation(base_number, exponent-1))
# Test runs
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0 ,1))
# Console output
True
True
True |
def test_my_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
elif exponent == 1:
return base_number
else:
return base_number*exponentiation(base_number, exponent-1)
print(test_my_exponentiation (3, 4, 81))
print(test_my_exponentiation(17, 1, 17))
print(test_my_exponentiation(2, 0, 1))
#Console output:
True
True
True |
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))
print(test_exponentiation(17,1,17))
print(test_exponentiation(2,0,1)) |
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:
result = 1
else:
result = (base_number * (exponentiation(base_number, exponent-1)))
return result
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):
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 (exponentiation(base_number, (exponent - 1)) * base_number)
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1)) |
|
#Define Test Function
def test_exponentiation (int_1, int_2, expected):
result = exponentiation (int_1, int_2)
if expected == result:
return True
else:
return False
#Algorithm Code
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * (exponentiation(base_number, exponent -1))
#Test
print (test_exponentiation(3, 4, 81))
print (test_exponentiation(17, 1, 17))
print (test_exponentiation(2, 0, 1))
#Output
True
True
True |
|
|
|
|
|
|
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.The text was updated successfully, but these errors were encountered: