Comments (4)
def test_exponentiation(base_number, exponent, expected, solution_dict):
if exponentiation(base_number, exponent, solution_dict) == expected:
return True
else:
return False
def exponentiation(base_number, exponent, solution_dict):
if base_number ** exponent not in solution_dict:
if exponent == 0:
return 1
elif exponent == 1:
return base_number
elif exponent == 2:
return base_number * base_number
else:
return base_number * exponentiation(base_number, exponent - 1, solution_dict)
return solution_dict[base_number]
print(test_exponentiation(3, 4, 81, dict())) # True
print(test_exponentiation(17, 1, 17, dict())) # True
print(test_exponentiation(2, 0, 1, dict())) # True
from 2019-2020.
This is a dynamic version of the exponentiation algorithm that uses the property of multiplication between powers with same bases to implement a divide and conquer approach.
def test_exponentiation(base_number, exp, d, expected):
result = exponentiation_dp(base_number, exp, d)
if expected == result:
return True
else:
return False
def exponentiation_dp (base_number, exp, d):
if (base_number, exp) not in d:
if exp == 0:
d[(base_number, exp)] = 1
elif exp == 1:
d[(base_number, exp)] = base_number
else:
half_exp = exp // 2
exponentiation1 = exponentiation_dp(base_number, half_exp, d)
exponentiation2 = exponentiation_dp(base_number, half_exp, d)
if exp % 2 == 0:
d[(base_number, exp)] = exponentiation1 * exponentiation2
else:
d[(base_number, exp)] = exponentiation1 * exponentiation2 * base_number
return d[(base_number, exp)]
print(test_exponentiation(2, 5, dict(), 32)) #True
print(test_exponentiation(4, 4, dict(), 256)) #True
print(test_exponentiation(2, 9, dict(), 512)) #True
from 2019-2020.
def test_exponentiation(base_number, exponent, expected, solution_dict):
result = exponentiation(base_number, exponent, solution_dict)
if result == expected:
return True
else:
return False
def exponentiation(base_number, exponent, solution_dict):
if exponent not in solution_dict:
if exponent == 0:
solution_dict[exponent] = 1
else:
return base_number * exponentiation(base_number, exponent - 1, solution_dict)
return solution_dict.get(exponent)
print(test_exponentiation(3, 4, 81, dict())) #True
print(test_exponentiation(17, 1, 17, dict())) #True
print(test_exponentiation(2, 0, 1, dict())) #True
print(test_exponentiation(0, 0, 1, dict())) #True
print(test_exponentiation(1, 0, 1, dict())) #True
print(test_exponentiation(0, 2, 0, dict())) #True
from 2019-2020.
Hi all,
please find attached my personal solution – also available online:
# Test case for the function
def test_exponentation(base_number, exponent, solution_dict, expected):
result = exponentation(base_number, exponent, solution_dict)
if expected == result:
return True
else:
return False
# Code of the function
def exponentation(base_number, exponent, solution_dict):
exp_pair = (base_number, exponent)
if exp_pair not in solution_dict:
if exponent == 0:
solution_dict[exp_pair] = 1
else:
solution_dict[exp_pair] = base_number * exponentation(base_number, exponent - 1, solution_dict)
return solution_dict[exp_pair]
# Tests
my_dict = {}
print(test_exponentation(3, 2, my_dict, 9))
print(test_exponentation(3, 4, my_dict, 81))
print(test_exponentation(17, 1, my_dict, 17))
print(test_exponentation(2, 0, my_dict, 1))
print(test_exponentation(2, 6, my_dict, 64))
print(test_exponentation(0, 15, my_dict, 0))
print(test_exponentation(0, 0, my_dict, 1))
from 2019-2020.
Related Issues (20)
- Lecture "Brute-force algorithms", exercise 5 HOT 6
- Bonus exercise (AtariGo) HOT 1
- Lecture "Organising information: unordered structures", exercise 1 HOT 8
- Lecture "Organising information: unordered structures", exercise 2 HOT 8
- Lecture "Organising information: unordered structures", exercise 3 HOT 11
- Lecture "Recursion", exercise 1 HOT 7
- Lecture "Recursion", exercise 2 HOT 9
- Lecture "Divide and conquer algorithms", exercise 1 HOT 3
- Lecture "Divide and conquer algorithms", exercise 2 HOT 7
- Lecture "Divide and conquer algorithms", exercise 3 HOT 4
- Lecture "Dynamic programming algorithms", exercise 1 HOT 3
- Lecture "Organising information: trees", exercise 1 HOT 4
- Lecture "Organising information: trees", exercise 2 HOT 5
- Lecture "Backtracking algorithms", exercise 1
- Lecture "Backtracking algorithms", exercise 2 HOT 1
- Lecture "Organising information: graphs", exercise 1 HOT 4
- Lecture "Organising information: graphs", exercise 2 HOT 5
- Lecture "Greedy algorithms", exercise 1 HOT 1
- Lecture "Greedy algorithms", exercise 2 HOT 1
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 2019-2020.