Now using regex for data parsing
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import re
|
||||
|
||||
def process_rules(data):
|
||||
rules = {}
|
||||
for line in data:
|
||||
line = line.replace(' bags', '').replace(' bag', '').replace('.', '')
|
||||
color, rule = [r.strip() for r in line.split('contain ')]
|
||||
rule = rule.split(', ')
|
||||
color = re.search("(\w+ \w+) bags contain", line).group(1)
|
||||
rule = re.findall("(\d+) (\w+ \w+)", line)
|
||||
|
||||
rules[color] = []
|
||||
for bag in rule:
|
||||
if bag == 'no other': break
|
||||
rules[color].append((bag[2:], int(bag[0])))
|
||||
rules[color].append((bag[1], int(bag[0])))
|
||||
|
||||
return rules
|
||||
|
||||
|
||||
Reference in New Issue
Block a user