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