-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomb.py
70 lines (52 loc) · 1.92 KB
/
bomb.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
from interface import ask_question
class Indicators():
def __init__(self):
self._known_indicators = dict()
def __getitem__(self, label):
"""Returns True iff there is a lit indicator with the given label"""
label = label.upper()
if label in self._known_indicators:
return self._known_indicators[label]
else:
question = "Is there a lit indicator with label {}?".format(label)
result = ask_question(question, bool)
self._known_indicators[label] = result
return result
class Bomb():
def __init__(self):
self._serial_number = None
self._num_batteries = None
self._has_parallel_port = None
self.indicators = Indicators()
@property
def serial_number(self):
if self._serial_number is None:
self._serial_number = ask_question(
"What is the serial number?", str)
return self._serial_number
@property
def even_serial_number(self):
final_char = self.serial_number[-1]
return final_char in map(str, range(0, 9, 2))
@property
def odd_serial_number(self):
final_char = self.serial_number[-1]
return final_char in map(str, range(1, 10, 2))
@property
def vowel_serial_number(self):
for vowel in ("a", "e", "i", "o", "u"):
if vowel in self.serial_number.lower():
return True
return False
@property
def num_batteries(self):
if self._num_batteries is None:
self._num_batteries = ask_question(
"How many batteries are there?", int)
return self._num_batteries
@property
def has_parallel_port(self):
if self._has_parallel_port is None:
self._has_parallel_port = ask_question(
"Does the bomb have a parallel port?", bool)
return self._has_parallel_port