-
Notifications
You must be signed in to change notification settings - Fork 0
/
L23Q3_SpreadingUdaciousness.py
57 lines (47 loc) · 2.01 KB
/
L23Q3_SpreadingUdaciousness.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
# Spreading Udaciousness
# One of our modest goals is to teach everyone in the world to program and
# understand computer science. To estimate how long this will take we have
# developed a (very flawed!) model:
# Everyone answering this question will convince a number, spread, (input to
# the model) of their friends to take the course next offering. This will
# continue, so that all of the newly recruited students, as well as the original
# students, will convince spread of their
# friends to take the following offering of the course.
# recruited friends are unique, so there is no duplication among the newly
# recruited students. Define a procedure, hexes_to_udaciousness(n, spread,
# target), that takes three inputs: the starting number of Udacians, the spread
# rate (how many new friends each Udacian convinces to join each hexamester),
# and the target number, and outputs the number of hexamesters needed to reach
# (or exceed) the target.
# For credit, your procedure must NOT use: while, for, or import math.
#my solution
i = [0] #cannot run more than once or the list will be cumulative
def hexes_to_udaciousness(n, spread, target):
if n >= target:
return i[0]
else:
n += n*spread
i[0] += 1
return hexes_to_udaciousness(n,spread,target)
#udacity solution
def hexes_to_udaciousness_udacity(n, spread, target):
if n >= target:
return 0
else:
return 1 + hexes_to_udaciousness_udacity(n*(1+spread),spread,target)
# 0 more needed, since n already exceeds target
#print hexes_to_udaciousness(100000, 2, 36230)
#>>> 0
# after 1 hexamester, there will be 50000 + (50000 * 2) Udacians
#print hexes_to_udaciousness(50000, 2, 150000)
#>>> 1
print 'hello World'
# need to match or exceed the target
print hexes_to_udaciousness(50000, 2, 150001)
#>>> 2
# only 12 hexamesters (2 years) to world domination!
#print hexes_to_udaciousness(20000, 2, 7 * 10 ** 9)
#>>> 12
# more friends means faster world domination!
#print hexes_to_udaciousness_udacity(15000, 3, 7 * 10 ** 9)
#>>> 10