Skip to main content
Tweeted twitter.com/StackCodeReview/status/1020006257250496513
added 1 character in body
Source Link
kharandziuk
  • 779
  • 2
  • 9
  • 20

I have a function:

def palindrome(n):
    assert(n < 910)
    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?

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?

I have a function:

def palindrome(n):
    assert(n < 10)
    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?

added 23 characters in body
Source Link
kharandziuk
  • 779
  • 2
  • 9
  • 20

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?

I have a function:

def palindrome(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:

>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?

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?

added 69 characters in body
Source Link
kharandziuk
  • 779
  • 2
  • 9
  • 20

I have a function:

def solutionpalindrome(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:

>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?

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?

I have a function:

def palindrome(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:

>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?

added 48 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
Source Link
kharandziuk
  • 779
  • 2
  • 9
  • 20
Loading