Add currently solved exerises from Chapter 16
This commit is contained in:
parent
0dde0c524d
commit
1a90502946
6 changed files with 269 additions and 0 deletions
28
thinkcspy/Chapter-16-Exercises/hanoi.py
Normal file
28
thinkcspy/Chapter-16-Exercises/hanoi.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
def stackTowers(num):
|
||||
A = []
|
||||
B = []
|
||||
C = []
|
||||
rounds = 0
|
||||
|
||||
|
||||
for i in range(num):
|
||||
A.insert(0, i+1)
|
||||
|
||||
|
||||
def hanoi(num, start, aux, end):
|
||||
nonlocal rounds
|
||||
|
||||
if num == 1:
|
||||
end += [start.pop()]
|
||||
rounds += 1
|
||||
print(rounds, A, B, C)
|
||||
else:
|
||||
hanoi(num - 1, start, end, aux)
|
||||
end += [start.pop()]
|
||||
rounds += 1
|
||||
print(rounds, A, B, C)
|
||||
hanoi(num -1, aux, start, end)
|
||||
|
||||
hanoi(num, A, B, C)
|
||||
|
||||
stackTowers(5)
|
||||
Loading…
Reference in a new issue