Skip to content

Commit 5e7609c

Browse files
committed
Finished working on the allergies challenge, interesting binary method
1 parent 3430ced commit 5e7609c

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed
1.17 KB
Binary file not shown.
11.1 KB
Binary file not shown.

allergies/allergies.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
class Allergies:
1+
allergies = {
2+
1: 'eggs', #binary number 1
3+
2: 'peanuts', #binary number 10
4+
4: 'shellfish', #binary number 100
5+
8: 'strawberries', #binary number 1000
6+
16: 'tomatoes', #binary number 10000
7+
32: 'chocolate', #binary number 100000
8+
64: 'pollen', #binary number 1000000
9+
128: 'cats' #binary number 10000000
10+
}
211

12+
class Allergies(object):
313
def __init__(self, score):
4-
pass
14+
"""Calculates allergies for given score."""
15+
self.score = score
16+
self.lst = []
17+
self.__calculate_allergies()
518

6-
def allergic_to(self, item):
7-
pass
19+
def __calculate_allergies(self):
20+
value = 1
21+
binary = "{:b}".format(self.score).zfill(8)
822

9-
@property
10-
def lst(self):
11-
pass
23+
for i in binary[::-1]:
24+
if value >= 256:
25+
break
26+
if i == '1':
27+
self.lst.append(allergies[value])
28+
value <<= 1
29+
30+
def allergic_to(self, food):
31+
"""Check if person is allergic on given food."""
32+
return food in self.lst

0 commit comments

Comments
 (0)