pythonpython-3.xpalindrome

Solving palindromic 'Triangle Quest' puzzle in Python


I'm trying to solve this programming puzzle:

You are given a positive integer N (0 < N < 10). Your task is to print a palindromic triangle of size N.

For example, a palindromic triangle of size 5 is:

1
121
12321
1234321
123454321

You can't take more than two lines. You have to complete the code using exactly one print statement.

Note: Using anything related to strings will give a score of 0. Using more than one for-statement will give a score of 0.

I can think only of 'dumb' way to do this:

for i in range(1, N+1):
    print([0, 1, 121, 12321, 1234321, 123454321, 12345654321, 1234567654321, 123456787654321, 12345678987654321][i])

Is there a more elegant solution?


Solution

  • I ended up doing the following (thanks @raina77ow for the idea):

    for i in range(1, N+1):
        print((111111111//(10**(9-i)))**2)