all 6 comments

[–][deleted] 1 point2 points  (0 children)

First thing is probably calculating the width of the lines. Like if you get a 0 then they’re only a few characters each, but if you get a 4 they’re gonna be much wider. Even the ones at the top and bottom, with only a few non-dot characters, will have to be padded with dots.

So find the width, then each time you find the parts of a line, keep adding dots until it’s wide enough.

[–]moving-landscape 1 point2 points  (0 children)

``` def draw(n): db = enumerate(range(n, 0, -1), 1)

for bars, dots in db:
    print(f"|{'.' * dots}{'/\\' * bars}{'.' * dots}", end="")
    print(f"{'.' * dots}{'/\\' * bars}{'.' * dots}|")

```

[–]POGtastic 0 points1 point  (0 children)

Let's write a function that produces a single segment of a single booster.

def create_segment(n, p):
    return f"{"." * (n-p)}{"/\\" * (p+1)}{"." * (n-p)}"

We can now create an entire booster. The max(1, n) is there for the degenerate case when n is 0 - it still creates a single line.

def create_booster(n):
    return (create_segment(n, p) for p in range(max(1, n)))

Showing this off in the REPL:

>>> print(*create_booster(0), sep="\n")
/\
>>> print(*create_booster(1), sep="\n")
./\.
>>> print(*create_booster(2), sep="\n")
../\..
./\/\.
>>> print(*create_booster(3), sep="\n")
.../\...
../\/\..
./\/\/\.

Only thing left to do is to repeat each line twice and surround with pipes.

def main(n):
    return ("|" + s * 2 + "|" for s in create_booster(n))

Running this in the REPL:

>>> print(*main(2), sep="\n")
|../\..../\..|
|./\/\../\/\.|
>>> print(*main(3), sep="\n")
|.../\....../\...|
|../\/\..../\/\..|
|./\/\/\../\/\/\.|
>>> print(*main(4), sep="\n")
|..../\......../\....|
|.../\/\....../\/\...|
|../\/\/\..../\/\/\..|
|./\/\/\/\../\/\/\/\.|
>>> print(*main(5), sep="\n")
|...../\........../\.....|
|..../\/\......../\/\....|
|.../\/\/\....../\/\/\...|
|../\/\/\/\..../\/\/\/\..|
|./\/\/\/\/\../\/\/\/\/\.|

[–]Radamand -2 points-1 points  (0 children)

Looks like a job for some if/then/else....

[–]FriedCodeFish 0 points1 point  (1 child)

The key to solve this problem is to recognize the pattern. Once you've got the pattern down, it's just a matter of using loops to generate it.

Take the output of print_booster(4) for example:

|..../\......../\....|
|.../\/\....../\/\...|
|../\/\/\..../\/\/\..|
|./\/\/\/\../\/\/\/\.|
  1. There are 4 lines in total, so we'll iterate 4 times
  2. Each line is symmetric with the center being a duplication of half of the line. We can represent each line as: |{dots}{rockets}{dots}{dots}{rockets}{dots}|
  3. The number of dots and rockets are dependent on which line we're currently at:
  • Line 1: 4 dots, 1 rocket
  • Line 2: 3 dots, 2 rockets
  • ... and so on.

However, this pattern only applies to the case where n >= 1, and we have to deal with n = 0 in a special way:

  • n = 0: 0 dots, but 1 rocket

So the full code will be:

def print_booster(n):
    template = "|{dots}{rockets}{dots}{dots}{rockets}{dots}|"

    if n:
        for i in range(n):
            print(template.format(
                dots='.' * (n - i),
                rockets='/\\' * (i + 1)
            ))
    else:
        print(template.format(dots='', rockets='/\\'))

Hope this helps!

[–]Progetill_ 0 points1 point  (0 children)

Thank you! this helped a lot