1
I have to play the little game "Anoi Tower", in which I need to pass the data that are in the source tower to the destination tower, using an auxiliary tower. however when the data arrive at the destination tower, inside the list appears a 'None':
algorithm:
class Stack:
def __init__(self):
self.__items = []
@property
def items(self):
return self.__items
def isempty(self):
return len(self.__items) == 0
def push(self, item):
self.__items.insert(0,item)
def pop(self):
if not self.isempty():
return self.__items.pop(0)
else:
return print('Não há nenhum item para desempilhar')
def peek(self):
if not self.isempty():
return self.__items[0]
else:
return print('Não há nenhum item no topo')
def __len__(self):
return len(self.items)
class TowerOfHanoi:
def __init__(self):
self.__orig = Stack()
self.__aux = Stack()
self.__dest = Stack()
@property
def orig(self):
return self.__orig
@property
def aux(self):
return self.__aux
@property
def dest(self):
return self.__dest
def insere(self, elemento):
self.__orig.push(elemento)
def movedisk(self, n, orig, dest):
transfere(n, self.__orig, self.__aux,self.__dest)
print(f'{n} foi do pino {orig} para o pino {dest}')
def movetower(self, n, orig, aux, dest):
if n >= 1:
self.movetower(n - 1, orig, dest, aux)
self.movedisk(n, orig, dest)
self.movetower(n - 1, aux, orig, dest)
tower = TowerOfHanoi()
def transfere(n,orig, aux,dest):
while not orig.isempty():
if n>=1:
transfere(n-1, orig, dest, aux)
transfere(n-1, aux, orig, dest)
dest.push(orig.pop())
p = Stack()
tower.insere(3)
tower.insere(2)
tower.insere(1)
print(f'Pilha de origem: {tower.orig.items}')
print(f'A pilha de origem está vazia?{tower.orig.isempty()} ')
print(f'A pilha de destino está vazia?{tower.dest.isempty()} ')
tower.movetower(len(tower.orig.items), "A", "B", "C")
print(f'A pilha de origem está vazia?{tower.orig.isempty()} ')
print(f'A pilha de destino está vazia?{tower.dest.isempty()} ')
print(f'item da pilha de destino: {tower.dest.items}')
print(f'item da pilha de origem:{tower.orig.items}')
print(f'topo dest: {tower.dest.peek()}')
print(f'Tamanho dest:{len(tower.dest.items)}')
print(f'Quant movimentos: {2**len(tower.dest.items)- 1} ')
I saw that in some places you did
return print('...')
; the return of functionprint
isNone
, then whenever this expression is executed its function will also returnNone
, which may justify the existence of such values in its list.– Woss