Skip to main content
2 of 3
clean code
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155

I'd recommend you start over. I always start patterns that are mirrored in the x axis by figuring out how to do a quarter of the pattern. I also use a view, so that if you want to change the icons you can.

And so I start with:

def palindrome(n):
    view = ''.join(map(str, range(n + 1)))
    for size in reversed(range(n + 1)):
        half = view[size:]
        print(half)

palindrome(3)
3
23
123
0123

After this we know to use view[size] as the center, and we repeat it size amount of times.

def palindrome(n):
    view = ''.join(map(str, range(n + 1)))
    for size in reversed(range(n + 1)):
        half = view[size] * size + view[size:]
        print(half)

palindrome(3)
3333
2223
1123
0123

From this you will then just have to mirror the output:

def palindrome(n):
    view = ''.join(map(str, range(n + 1)))
    for sizes in (reversed(range(n + 1)), range(1, n + 1)):
        for size in sizes:
            half = view[size] * size + view[size:]
            print(half[:0:-1] + half)

Going forward, you could enter view into it and make it output whatever you want.

def palindrome(view):
    n = len(view)
    for sizes in (reversed(range(n)), range(1, n)):
        for size in sizes:
            half = view[size] * size + view[size:]
            print(half[:0:-1])

palindrome('.|.|')
|||||||
|.....|
|.|||.|
|.|.|.|
|.|||.|
|.....|
|||||||
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155