-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw5.py
More file actions
89 lines (65 loc) · 2.65 KB
/
hw5.py
File metadata and controls
89 lines (65 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'''
Created on February 28, 2019
@author: Bharddwaj Vemulapalli
username: bvemulap
Pledge: I pledge my honor that I have abided by the Stevens Honor System.
CS115 - Hw 5
'''
import turtle # Needed for graphics
# Ignore 'Undefined variable from import' errors in Eclipse.
def sv_tree(trunk_length, levels):
if levels > 0:
turtle.forward(trunk_length)
turtle.left(45)
sv_tree(trunk_length/2, levels - 1)
turtle.right(90)
sv_tree(trunk_length/2, levels - 1)
turtle.left(45)
turtle.backward(trunk_length)
def fast_lucas(n):
'''Returns the nth Lucas number using the memoization technique
shown in class and lab. The Lucas numbers are as follows:
[2, 1, 3, 4, 7, 11, ...]'''
def fast_lucas_helper(n,memo):
if n <= 1:
return [2,1][n]
elif n in memo:
return memo[n]
else:
result = fast_lucas_helper(n - 1, memo) + fast_lucas_helper(n - 2, memo)
memo[n] = result
return result
return fast_lucas_helper(n, {})
def fast_change(amount, coins):
'''Takes an amount and a list of coin denominations as input.
Returns the number of coins required to total the given amount.
Use memoization to improve performance.'''
def fast_change_helper(amount, coins, memo):
if (amount, coins) in memo:
return memo[(amount, coins)]
if amount == 0:
result = 0
elif amount < 0:
result = float("inf")
elif coins == ():
result = float("inf")
else:
result = min(1 + fast_change_helper(amount - coins[0],coins,memo), fast_change_helper(amount,coins[1:],memo) )
memo[(amount, coins)] = result
return result
# Call the helper. Note we converted the list to a tuple.
return fast_change_helper(amount, tuple(coins), {})
# If you did this correctly, the results should be nearly instantaneous.
print(fast_lucas(3)) # 4
print(fast_lucas(5)) # 11
print(fast_lucas(9)) # 76
print(fast_lucas(24)) # 103682
print(fast_lucas(40)) # 228826127
print(fast_lucas(50)) # 28143753123
print(fast_change(131, [1, 5, 10, 20, 50, 100]))
print(fast_change(292, [1, 5, 10, 20, 50, 100]))
print(fast_change(673, [1, 5, 10, 20, 50, 100]))
print(fast_change(724, [1, 5, 10, 20, 50, 100]))
print(fast_change(888, [1, 5, 10, 20, 50, 100]))
# Should take a few seconds to draw a tree.
sv_tree(100, 4)