-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl1.py
More file actions
79 lines (58 loc) · 1.71 KB
/
l1.py
File metadata and controls
79 lines (58 loc) · 1.71 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
import random
import pprint
pp = pprint.PrettyPrinter(indent=1, width=180 )
num_floors=100
fatal_floor=random.randint(1,100)
safe_floor=None
print "fatal {}".format(fatal_floor)
class egg(object):
def __init__(self, state='safe'):
self.state=state
def change_state(self, state):
self.state=state
num_tries=dict()
eggs=list()
num_eggs=2
for e1 in range(num_eggs):
eggs.append(egg())
def prn_result(num_tries):
for i in num_tries.keys():
print "iter {} tries {}".format(i, num_tries[i])
for i in range(1,num_floors):
j=i
if i > fatal_floor:
break
print "iter {}".format(i)
num_tries[i]=list()
while (j<num_floors):
if j < fatal_floor:
#print "less"
num_tries[i].append(j)
j = j + i
elif j >= fatal_floor:
#print "more"
num_tries[i].append(j)
if len(eggs) > 1:
e1= eggs.pop()
e1.change_state('break')
if (num_tries[i].index(j-1)):
safe_floor=j-1
print "safe {}".format(safe_floor)
break
if len(eggs) > 1:
j=j+i
continue
else:
for k in xrange((j-i)+1, j):
num_tries[i].append(k)
if k == fatal_floor:
safe_floor=k-1
print "safe {}".format(safe_floor)
#pp.pprint(num_tries)
#prn_result(num_tries)
break
else:
continue
break
#pp.pprint(num_tries)
prn_result(num_tries)