Ваш метод обратной очереди метод возвращает неправильные значения.
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return "Disc({})".format(self.value)
class Queue:
def __init__(self, name):
self.name = name
self.head = None
self.tail = None
def is_empty(self):
return self.head is None
def len(self):
current = self.head
len = 0
while current is not None:
len += 1
current = current.next
return len
def enqueue(self, value):
node = Node(value)
if self.is_empty():
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def dequeue(self):
if self.is_empty():
return "EMPTY"
elif self.head == self.tail:
help = self.head.value
self.head = None
self.tail = None
return help
else:
jump = self.head.value
self.head = self.head.next
return jump
def peek(self):
return self.head.value
class QueueTower:
def __init__(self, number_of_discs):
self.number_of_discs = number_of_discs
self.A, self.B, self.C = Queue("A"), Queue("B"), Queue("C")
for i in range(number_of_discs, 0, -1):
self.A.enqueue(i)
def reverse_queue(self, q):
if not q.is_empty():
if q.head != q.tail:
data = q.peek()
q.dequeue()
self.reverse_queue(q) # recursion
q.enqueue(data)
return q
else:
data = q.peek()
q.dequeue()
q.enqueue(data)
return q
return Queue(q.name)
def valid_move(self, a, b):
if not a.len():
a.enqueue(b.dequeue())
elif not b.len():
if a.len() == self.number_of_discs:
d = self.reverse_queue(a)
else:
d = a
b.enqueue(d.dequeue())
elif a.peek() > b.peek():
e = b
a = self.reverse_queue(a)
a.enqueue(e.peek())
self.reverse_queue(a)
b.dequeue()
else:
b = self.reverse_queue(b)
b.enqueue(a.dequeue())
self.reverse_queue(b)
def hanoi(self, n):
if n % 2 == 0:
self.B, self.C = self.C, self.B
move = 2 ** n
for i in range(1, move):
if i % 3 == 1:
self.valid_move(self.A, self.C)
if i % 3 == 2:
self.valid_move(self.A, self.B)
if i % 3 == 0:
self.valid_move(self.B, self.C)
# Start formatting output.
tower_a, tower_b, tower_c = self.A, self.B, self.C
if n % 2 == 0:
tower_b, tower_c = self.C, self.B
for j in [tower_a, tower_b, tower_c]:
print("\n |", j.name, "|", end=" ")
current = j.head
while current:
print(current, "|", end=" ")
current = current.next
print()
# End formatting output.
print("\nMove needed is: " + str(move - 1))
tower = QueueTower(3)
tower.hanoi(3)
Вывод:
| A | Disc(2) | Disc(3) |
| B |
| C | Disc(1) |
| A | Disc(3) |
| B | Disc(2) |
| C | Disc(1) |
| A | Disc(3) |
| B | Disc(1) | Disc(2) |
| C |
| A |
| B | Disc(1) | Disc(2) |
| C | Disc(3) |
| A | Disc(1) |
| B | Disc(2) |
| C | Disc(3) |
| A | Disc(1) |
| B |
| C | Disc(2) | Disc(3) |
| A |
| B |
| C | Disc(1) | Disc(2) | Disc(3) |
Move needed is: 7