-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlovely_lucky_lambs.py
92 lines (67 loc) · 2.26 KB
/
lovely_lucky_lambs.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
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
90
91
class UnitTest:
def __init__(self, total_lambs, expected_result):
self.total_lambs = total_lambs
self.expected_result = expected_result
def get_generous(total_lambs):
"""
:param total_lambs: [int] number of lambs to divvy out
:return: [int] number of henchmen that I can hire by being generous.
"""
henchmen = 1
total_lambs -= 1
last = 0
current = 1
while total_lambs > 0:
if total_lambs < current * 2: # This is our final loop to handle an edge case [based on the rules].
if total_lambs >= current + last:
return henchmen + 1
else:
return henchmen
else: # We can keep going!
henchmen += 1
last = current
current *= 2
total_lambs -= current
return henchmen
def get_stingy(total_lambs):
"""
:param total_lambs: [int] number of lambs to divvy out
:return: [int] number of henchmen that I can hire by being stingy.
"""
henchmen = 1
total_lambs_left = 1
current = total_lambs - total_lambs_left
if current == 0:
return henchmen
last = 1
current -= last
henchmen += 1
while current > 0:
num_lambs = last + total_lambs_left
current -= num_lambs
if current >= 0:
henchmen += 1
total_lambs_left = last
last = num_lambs
return henchmen
def answer(total_lambs):
"""
:param total_lambs: list of task numbers [integers] for a specific minion to work on.
:return: list of task numbers for a minion to work on without hitting 'Too many tasks'
"""
return get_stingy(total_lambs) - get_generous(total_lambs)
if __name__ == "__main__":
unit_tests = [UnitTest(10, 1),
UnitTest(143, 3)
]
count = 0
for test in unit_tests:
count += 1
result = answer(test.total_lambs)
if result != test.expected_result:
print "[!] Test {} failed.".format(count)
print "\ttotal_lambs: {}".format(test.total_lambs)
print "\texpected result: {}".format(test.expected_result)
print "\tactual result: {}".format(result)
else:
print "[+] Test {} passed.".format(count)