Comments (18)
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))
from 2018-2019.
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.
from 2018-2019.
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
from 2018-2019.
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
from 2018-2019.
#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
from 2018-2019.
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))
from 2018-2019.
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))
from 2018-2019.
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))
from 2018-2019.
# 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))
from 2018-2019.
# 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))
from 2018-2019.
from 2018-2019.
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))
from 2018-2019.
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
from 2018-2019.
# 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))
from 2018-2019.
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)
from 2018-2019.
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
from 2018-2019.
#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))
from 2018-2019.
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
```
from 2018-2019.
Related Issues (20)
- Lecture "Brute-force algorithms", exercise 3 HOT 13
- Lecture "Brute-force algorithms", exercise 4 HOT 16
- Lecture "Brute-force algorithms", exercise 5 HOT 12
- Lecture "Organising information: unordered structures", exercise 1 HOT 21
- Lecture "Organising information: unordered structures", exercise 2 HOT 19
- Lecture "Organising information: unordered structures", exercise 3 HOT 17
- Lecture "Recursion", exercise 2 HOT 20
- Lecture "Divide and conquer algorithm", exercise 1 HOT 12
- Lecture "Divide and conquer algorithm", exercise 2 HOT 5
- Lecture "Dynamic programming algorithms", exercise 1 HOT 15
- Lecture "Dynamic programming algorithms", exercise 2 HOT 8
- Lecture "Organising information: trees", exercise 1 HOT 4
- Lecture "Organising information: trees", exercise 2 HOT 4
- Lecture "Organising information: graphs", exercise 1 HOT 14
- Lecture "Organising information: graphs", exercise 2 HOT 12
- Christmas present game + bonus Python exercise
- Lecture "Backtracking algorithms", exercise 1 HOT 3
- Lecture "Greedy algorithms", exercise 1 HOT 2
- Question about an exercise from last year
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2018-2019.