Beispiel #1
0
 def test_2(self):
     self.assertEqual("Left", balance("!??", "?!!"))
Beispiel #2
0
 def test_1(self):
     self.assertEqual("Right", balance("!!", "??"))
Beispiel #3
0
 def test_4(self):
     self.assertEqual("Balance", balance("!!???!????", "??!!?!!!!!!!"))
Beispiel #4
0
 def test_3(self):
     self.assertEqual("Left", balance("!?!!", "?!?"))
Beispiel #5
0
def test_balance(left, right, expected):
    assert balance(left, right) == expected
Beispiel #6
0
    r'^(?P<name>\w+) \((?P<weight>\d+)\)( \-\> (?P<children>.*?))?$')


def process_file(f):
    programs = {}
    parents = {}  # maps child to parent
    for line in f:
        match = pattern.match(line.strip())
        name, weight, children = match.group('name'), match.group(
            'weight'), match.group('children')
        programs[name] = Node(name, int(weight))
        if children:
            programs[name].children = set(children.split(', '))
    for child_name, parent_name in parents.items():
        programs[child_name].parent_name = parent_name
    return programs


with open('test_input.txt', 'r') as f:
    test_programs = process_file(f)

with open('input.txt', 'r') as f:
    programs = process_file(f)

assert bottom_program(test_programs) == 'tknk'
bottom_name = bottom_program(programs)
print(bottom_name)

assert balance(test_programs, 'tknk') == ('ugml', 60)
print(balance(programs, bottom_name))