-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_towers_of_hanoi.py
57 lines (45 loc) · 1.66 KB
/
test_towers_of_hanoi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from .towers_of_hanoi import towers_of_hanoi, towers_of_hanoi_list
from data_structures.stack.stack import Stack
from re import match
def test_towers_of_hanoi_1():
assert len(list(towers_of_hanoi(1))) == 1
def test_towers_of_hanoi_2():
assert len(list(towers_of_hanoi(2))) == 3
def test_towers_of_hanoi_3():
assert len(list(towers_of_hanoi(3))) == 7
def test_towers_of_hanoi_4():
assert len(list(towers_of_hanoi(4))) == 15
def test_towers_of_hanoi_7_solution():
towers = {"A": Stack(range(7, 0, -1)), "B": Stack(), "C": Stack()}
for move in towers_of_hanoi(7):
move = match(r"Disk (\d+) moved from (\w+) to (\w+)", move)
disk = int(move.group(1))
start = move.group(2)
end = move.group(3)
assert towers[start].pop() == disk
if towers[end]:
assert towers[end].peek() > disk
towers[end].push(disk)
assert len(towers["A"]) == 0
assert len(towers["B"]) == 0
end = towers["C"]
assert len(end) == 7
for i in range(1, 8):
assert end.pop() == i
def test_towers_of_hanoi_list_7_solution():
towers = {"A": Stack(range(7, 0, -1)), "B": Stack(), "C": Stack()}
for move in towers_of_hanoi_list(7):
move = match(r"Disk (\d+) moved from (\w+) to (\w+)", move)
disk = int(move.group(1))
start = move.group(2)
end = move.group(3)
assert towers[start].pop() == disk
if towers[end]:
assert towers[end].peek() > disk
towers[end].push(disk)
assert len(towers["A"]) == 0
assert len(towers["B"]) == 0
end = towers["C"]
assert len(end) == 7
for i in range(1, 8):
assert end.pop() == i