2018-2019
2018-2019 copied to clipboard
Lecture "Recursion", exercise 1
Define a recursive function def exponentiation(base_number, exponent)
for implementing the exponentiation operation, and test (by implementing the related test case) it on the following inputs: 34, 171, and 20.
def test_pwr(n, expo, expected):
if pwr(n, expo) == expected:
return True
else:
return False
def pwr(n, expo):
if(expo==0):
result = 1
elif(expo==1):
result = n
else:
result = n * pwr(n,expo-1)
return result
print(test_pwr(3,4,81))
print(test_pwr(17,1,17))
print(test_pwr(2,0,1))
#true
#true
#true
First I approached the problem similarly to the multiplication recursion. That is, I wrote with pencil & paper a way to conceive of exponentiation in a recursive way. For example for 3^4:
3^4 = 3 x 3 x 3 x 3 3^4 = 3 x (3^3) 3^4 = 3 x (3 x (3^2)) 3^4 = 3 x (3 x (3 x (3^1))) 3^4 = 3 x (3 x (3 x (3 x (3^0))))
Since we know that any number raised to the power of 0 will give 1 (such as, 2^0=1, just like 64235626^0=1), we can use this as the base case.
# Test case for the exponentiation algorithm
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
if expected == result:
return True
else:
return False
# Code of the exponentiation algorithm
def exponentiation(base_number, exponent):
if exponent == 0: #this is the application of the base case as I mentioned above
return 1 # any number to the power of 0 equals 1
else:
return base_number * exponentiation(base_number, exponent - 1) # reduce the exponent by one and multiply this whole thing by the base_number
# Three test runs of the algorithm
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
def test_exponentiation(n,e, expected):
result = exponentiation(n,e)
if expected == result:
return True
else:
return False
def exponentiation(n, e):
if e==0:
return 1
elif e==1:
return n
else:
return n * exponentiation(n,e-1)
print(test_exponentiation(3,4,81)) #True
print(test_exponentiation(17,1,17)) #True
print(test_exponentiation(2,0,1)) #True
def test_exp(n, e, expected): if expected == exp(n, e): return True else: return False
def exp(n, e): if e == 0: return 1 else: return n*exp(n, e-1)
print(test_exp(3,4,81)) print(test_exp(17,1,17)) print(test_exp(2,0,1))
True True True
All true, I was stack in the temptative of doing this thing with another way, but I'm not able to solve a problems with variables.
# Test case for the algorithm
def test_exponentiation(base_number, exponent, expected):
result = base_number ** exponent
if expected == result:
return True
else:
return False
# Code of the algorithm
def exponentiation(base_number, exponent):
x = base_number
n = exponent
if n == 0:
return 1 # any integer raised to 0 return 1
else:
return x * exponentiation(x, n-1) # reduce the exponent of 1
# multiply the exponentiation of the base_number
# raised to the reduced exponent and the base_number
# run some tests
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
#Test case for the algorithm
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
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)
print(exponentiation(5, 3)) #return 125
print(test_exponentiation(3, 4, 81)) #return True
print(test_exponentiation(17, 1, 17)) #return True
print(test_exponentiation(2, 0, 1)) #return True
def exp_check(n,e,expected): if exp(n,e) == expected: return True else: return False
def exp(n,e): if (e==0): result=1 else: result=n*exp(n,e-1) return result
print (exp(2,3)) {8) print(exp(93,0)) {1)
print(exp_check(3,4,81)) {True) print(exp_check(17,1,17)) {True) print(exp_check(2,0,1)) {True)
Provided that in recursion x y = x * x y-1, that's my idea:
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(num, exponent, expected):
if exponentiation(num, exponent) == expected:
return True
else:
return False
def exponentiation(num, exponent):
if exponent == 0:
return 1
elif exponent == 1:
return num
else:
result = num * exponentiation(num,exponent - 1)
return result
print(test_exponentiation(3, 4, 81)) print(test_exponentiation(17, 1, 17)) print(test_exponentiation(2, 0, 1))
def test_my_alg(n, e, expected):
return my_alg(n, e) == expected
def my_alg(n, e):
if e == 0:
output = 1
else:
output = n * my_alg(n, e - 1)
return output
n1 = 3
e1 = 4
expected1 = 81
print(test_my_alg(n1, e1, expected1))
n2 = 17
e2 = 1
expected2 = 17
print(test_my_alg(n2, e2, expected2))
n3 = 2
e3 = 0
expected3 = 1
print(test_my_alg(n3, e3, expected3))
def test_exponentiation(n,e, expected): result = exponentiation(n,e) if expected == result: return True else: return False
def exponentiation(n, e): if e==0: return 1 elif e==1: return n else: return n * exponentiation(n,e-1)
print(test_exponentiation(3,4,81)) Output:True print(test_exponentiation(17,1,17)) Output:True print(test_exponentiation(2,0,1)) Output:True
# Test case for the algorithm
def test_exponentiation(base_number, exponent, expected):
result = exponentiation(base_number, exponent)
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 Cases
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))
# Test case for the algorithm
def exponentiation_test(base_number, exponent, expected):
if exponentiation(base_number, exponent)== expected:
return True
else:
return False
#code
def exponentiation(base_number, exponent):
if exponent == 0:
return 1
else:
return base_number * exponentiation(base_number, exponent - 1)
#test cases
print(exponentiation_test(3,4,81))
print(exponentiation_test(17,1,17))
print (exponentiation_test(2,0,1))
#Test case for the algorithm
def test_expontentiation(n, exponent, expected) result = exponentiation(n, exponent) if expected == result return True else return False
#Code of the algorithm
def exponentation(n, e): if exponent == 0: return 1 elif exponent == 1: return n else return n ** exponentiation(n, e-1)
print (test_exponentiation(3, 4, 81)) print(test_exponentiation(17, 1, 17)) print(test_exponentiation(2, 0, 1))
def test_exponentation(base_number, exponent, expected):
result = exponentation(base_number, exponent)
if expected == result:
return True
else:
return False
def exponentation(base_number, exponent):
if exponent == 0:
return 1
if exponent == 1:
return base_number
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))
Hi guys,
here my take on the exercise (source code available online):
# Test case for the algorithm
def test_exponentation(int_1, int_2, expected):
result = exponentation(int_1, int_2)
if expected == result:
return True
else:
return False
# Code of the algorithm
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, 1))
Some comments:
-
Python code and indentation: please, in your answers to the various questions, if you have to write down a Python code, be sure that the correct indent is preserved by previewing your post before to publish it. You can use the
```
environment for defining your Python code, e.g.:
```
write your Python code here
```