Skip to main content
4 of 5
added 23 characters in body
kharandziuk
  • 779
  • 2
  • 9
  • 20

Circles of numbers

I have a function:

def palindrome(n):
    assert(n < 9)
    res = [['0']]
    for x in range(1, n + 1):
        str_x = str(x)
        l = len(res)
        res.insert(0, [str_x] * l)
        res.append([str_x] * l)
        for line in res:
            line.insert(0, str_x)
            line.append(str_x)
    return '\n'.join(
            ''.join(row) for row in res
            )

which creates strings like:

>palindrome(1)
111
101
111
>palindrome(7)
777777777777777
766666666666667
765555555555567
765444444444567
765433333334567
765432222234567
765432111234567
765432101234567
765432111234567
765432222234567
765433333334567
765444444444567
765555555555567
766666666666667
777777777777777

Is there any way to improve the code?

kharandziuk
  • 779
  • 2
  • 9
  • 20