In [1]:
#!/usr/bin/env python3
In [2]:
class Sue:
def __init__(self, sue_id):
self.sue_id = sue_id
self.compound = {}
self.compound["children"] = None
self.compound["cats"] = None
self.compound["samoyeds"] = None
self.compound["pomeranians"] = None
self.compound["akitas"] = None
self.compound["vizslas"] = None
self.compound["goldfish"] = None
self.compound["trees"] = None
self.compound["cars"] = None
self.compound["perfumes"] = None
def set_compound_value(self, compound, value):
self.compound[compound] = value
def is_match_star_1(self, match):
# print ("self: ", self.compound)
# print ("match: ", match)
for n in self.compound:
if self.compound[n] != None:
if self.compound[n] != match[n]:
return False
return True
def is_match_star_2(self, match):
# print ("self: ", self.compound)
# print ("match: ", match)
for n in ["children", "samoyeds", "akitas", "vizslas", "cars", "perfumes"]:
if self.compound[n] != None:
if self.compound[n] != match[n]:
return False
if self.compound["cats"] != None:
if self.compound["cats"] < match["cats"]:
return False
if self.compound["trees"] != None:
if self.compound["trees"] < match["trees"]:
return False
if self.compound["pomeranians"] != None:
if self.compound["pomeranians"] > match["pomeranians"]:
return False
if self.compound["goldfish"] != None:
if self.compound["goldfish"] > match["goldfish"]:
return False
return True
def give_id(self):
return self.sue_id
In [3]:
filename = './input'
sues = []
match = {}
match["children"] = 3
match["cats"] = 7
match["samoyeds"] = 2
match["pomeranians"] = 3
match["akitas"] = 0
match["vizslas"] = 0
match["goldfish"] = 5
match["trees"] = 3
match["cars"] = 2
match["perfumes"] = 1
with open(filename) as f:
for line in f:
sue_text = _, _, _, _, _, _, _, _ = line.replace(':','').replace(',','').split()
sue = Sue(sue_text[1])
sue.set_compound_value(sue_text[2],int(sue_text[3]))
sue.set_compound_value(sue_text[4],int(sue_text[5]))
sue.set_compound_value(sue_text[6],int(sue_text[7]))
sues.append(sue)
for i in range(0,len(sues)):
if sues[i].is_match_star_1(match):
print ("Star 1: ",sues[i].give_id())
for i in range(0,len(sues)):
if sues[i].is_match_star_2(match):
print ("Star 2: ",sues[i].give_id())
In [ ]:
In [ ]: