Skip to main content
2 of 5
added 48 characters in body; edited tags; edited title
200_success
  • 144.2k
  • 22
  • 188
  • 473

Circles of numbers

I have a function:

def solution(n):
    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:

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