-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disc04.py
72 lines (60 loc) · 1.46 KB
/
Disc04.py
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
def count_stair_ways(n):
"""Returns the number of ways to climb up a flight of
n stairs, moving either 1 step or 2 steps at a time.
>>> count_stair_ways(4)
5
"""
"*** YOUR CODE HERE ***"
if n == 0:
return 1
elif n < 0:
return 0
else:
return count_stair_ways(n - 1) + count_stair_ways(n - 2)
def count_k(n, k):
""" Counts the number of paths up a flight of n stairs
when taking up to and including k steps at a time.
>>> count_k(3, 3) # 3, 2 + 1, 1 + 2, 1 + 1 + 1
4
>>> count_k(4, 4)
8
>>> count_k(10, 3)
274
>>> count_k(300, 1) # Only one step at a time
1
"""
"*** YOUR CODE HERE ***"
if n == 0:
return 1
elif n < 0:
return 0
elif k == 0:
return 0
else:
total = 0
i = 1
while i <= k:
total += count_k(n - i, k)
i += 1
return total
def even_weighted(s):
"""
>>> x = [1, 2, 3, 4, 5, 6]
>>> even_weighted(x)
[0, 6, 20]
"""
return [s[i] * i for i in range(len(s)) if i % 2 == 0]
def max_product(s):
"""Return the maximum product that can be formed using
non-consecutive elements of s.
>>> max_product([10,3,1,9,2]) # 10 * 9
90
>>> max_product([5,10,5,10,5]) # 5 * 5 * 5
125
>>> max_product([])
1
"""
if s == []:
return 1
else:
return max(max_product(s[1:]), s[0] * max_product(s[2:]))