-
Notifications
You must be signed in to change notification settings - Fork 1
/
K-peg Tower of Hanoi.py
95 lines (75 loc) · 2.7 KB
/
K-peg Tower 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
import sys
import math
from collections import deque
import argparse
p = argparse.ArgumentParser()
p.add_argument('n', type=int)
p.add_argument('k', type=int)
args = p.parse_args()
# See https://www.geeksforgeeks.org/deque-in-python/ for details on Deques
Towers = deque()
# Global variable that keeps track of the number of steps in our solution
number_of_steps = 0
# It is always a good idea to set a limit on the depth-of-the-recursion-tree in Python
sys.setrecursionlimit(3000)
def initialize(n, k) :
for i in range(k) :
X = deque()
if (i == 0) :
for j in range(n) :
X.append(j+1)
Towers.append(X)
def is_everything_legal() :
result = True
for i in range(args.k) :
for j in range(len(Towers[i])) :
for a in range(j,len(Towers[i])) :
if (Towers[i][a] < Towers[i][j]) :
result = False
return(result)
def move_top_disk(source, dest):
global number_of_steps
number_of_steps = number_of_steps + 1
x = Towers[source].popleft()
Towers[dest].appendleft(x)
if (True == is_everything_legal()) :
y = " (Legal)"
else :
y = " (Illegal)"
print ("Move disk " + str(x) + " from Peg " + str(source+1) + " to Peg " + str(dest+1) + y)
def move_using_three_pegs(number_of_disks, source, dest, intermediate) :
if (1 == number_of_disks) :
move_top_disk (source, dest)
else :
move_using_three_pegs (number_of_disks-1, source, intermediate, dest);
move_top_disk(source, dest)
move_using_three_pegs (number_of_disks-1, intermediate, dest, source)
def move(number_of_disks, source, dest, list_of_interm) :
if (number_of_disks > 0):
empty_inter = [x for x in list_of_interm if len(Towers[x]) ==0]
if(len(empty_inter) < 2):
move_using_three_pegs(number_of_disks, source, dest, empty_inter[-1])
else:
p = math.floor(number_of_disks/2)
middle = empty_inter[-1]
empty_inter.pop()
empty_inter.append(dest)
move(p, source, middle,empty_inter)
empty_inter.pop()
move(number_of_disks - p, source, dest, empty_inter)
empty_inter.append(source)
move(p, middle, dest, empty_inter)
def print_peg_state(m) :
global number_of_steps
print ("-----------------------------")
print ("State of Peg " + str(m+1) + " (Top to Bottom): " + str(Towers[m]))
print ("Number of Steps = " + str(number_of_steps))
print ("-----------------------------")
initialize(args.n,args.k)
print_peg_state(0)
inter = [x for x in range(args.k)];
source = inter.pop(0);
dest = inter.pop(-1)
move(args.n, source, dest, inter)
print_peg_state(args.k-1)