#๐Ÿ”’ Towers of Hanoi print function

33 messages ยท Page 1 of 1 (latest)

rocky pewter
#

OK!
I'm working on a project for school. It's Towers of Hanoi. There can be between 2 - 9 disks, but only 3 towers.
I'm trying to wrap my head around this print_towers() function on line 17. How can I print the information stored in the copied list of stacks to match the examples I provided?
In the file I tried starting it but I just can't figure it out!
Please help!

mighty elbowBOT
#

@rocky pewter

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pseudo escarp
#

So, you're just asking about this function here, no?:

def print_towers(towers: list[Stack], num_of_disks: int):
    """
    Purpose: Print current state of towers.
    Parameters: (list) - towers, (int) - num_of_disks
    Returns: None
    """
    copies = []
    for t_stack in towers:
        c_stack = t_stack.copy()
        copies.append(c_stack) # copies = [Stack([3, 2, 1]), Stack([]), Stack([])]

    for i in range(num_of_disks, 0, -1):
        if len(copies[]) < i :
                print(" " * num_of_disks + "|" + " " * num_of_disks, end="")
rocky pewter
#

yes

#

just that one

#

I sent the whole file and the stack file for context

pseudo escarp
#

First of all there's a double indent on the last line, but anyway, what's your question here?
Is that function already working correctly or are there errors?

rocky pewter
#

That's how far I got but I think that's completely wrong

#

if you look at the screenshots

pseudo escarp
#

That's how you want it to look, no?
What does num_of_disks specify? Is it just the same as len(towers)?

rocky pewter
#

I'm trying to match the stacks (representing each tower 1 - 3) to the output

#

the num_of_disks is how many disks start on the first tower at the beginning of the game

#

so here: num_of_disk = 3

#

int

pseudo escarp
#

Okay, so your current code doesn't work at all, does it?
I assume that at the latest this line:

if len(copies[]) < i:
```will throw an error
rocky pewter
#

yes

#

line 28 to 30 is spit balling

#

for i in range(num_of_disks, 0, -1):
if len(copies[]) < i :
print(" " * num_of_disks + "|" + " " * num_of_disks, end="")

pseudo escarp
#

Okay. Can you start by trying to explain how you think you can solve this (i.e. just in English words and without any code)?
I.e., assuming num_of_disk = 3, what do you need to do to for each of these steps:

  • print the first line
  • print the second line
  • print the third line
  • print the last line
rocky pewter
#

for each row of each tower, if the length of the stack is less than the height of the tower, print an empty row for that specific tower

#

the number of spaces on either side of the "|" is the num_of_disks

#

it's a Stack class, so you might have to .pop()

pseudo escarp
#

Okay, yeah, that's what your code would do, if it were implemented correctly, but what are all the (other) things you need to do & check?

#

because so far you've only covered empty towers, but the interesting case is when they are not empty

rocky pewter
#

I don't know the best way to describe it but

pseudo escarp
#

but I think you mean the correct thing

rocky pewter
#

when the height of the tower is equal to length of the stack you need to pop off the top value of the stack and print the equal amount of asterisks as the value within the stack. the spaces need to line up as well

#

idk I'm truly stumped on this

pseudo escarp
#

When stuck, simplify: Start for the simple case where num_of_disks = 1 (even though that wouldn't even be possible, but whatever).
You are allowed to hardcode it for this num_of_disks = 1 case.

Then you can hardcode a solution for the num_of_disks = 2 case.
Once you've done that, maybe you see some patterns emerging already, if not, feel free to post both these solutions here and maybe I can help you with discovering the pattern then.

mighty elbowBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.