Solving the Quest Triangle Palindromic Puzzle in Python

I am 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:

1
121
12321
1234321
123454321

You cannot take more than two lines. You must execute the code using only 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 only think of a “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?

+8
source share
11 answers

( @raina77ow ):

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

wtf :

f=lambda n:n and[f(n-1),print((10**n//9)**2),range(1,n+1)];f(5)
+3
def palindrome(N):
    for i in range(1, N + 1):
        print(int('1' * i)**2)

palindrome(int(input()))
  • 1 * 1 = 1
  • 11 * 11 = 121
  • 111 * 111 = 12321
+2

:

set(map(lambda x:print((10**x//9)**2),range(1,N+1)))
+1
for i in range(1,int(input())+1):
   print(int((10**i-1)/9)**2)

1 -> (   10 - 1) / 9 =    1,    1 *    1 = 1
2 -> (  100 - 1) / 9 =   11,   11 *   11 = 121
3 -> ( 1000 - 1) / 9 =  111,  111 *  111 = 12321
4 -> (10000 - 1) / 9 = 1111, 1111 * 1111 = 1234321
+1

, . , :

  N = int(input())
  arr = []
  for i in range(1,N+1):
         arr.append(i)
         print(arr+arr[-2: :-1])
+1
for i in range(1, N + 1):
    print(*list(range(1, i + 1)) + list(range(i - 1, 0, -1)), sep = None)
0

, (), , , Python:

from math import log10

i = 1
while (N > log10(i)): print(i**2); i = i * 10 + 1
0

, :

    for i in range(1,5):
        print [j for j in range(1,i+1) ], [j for j in range(i-1,0,-1) ]

:

[1] []

[1, 2] [1]

[1, 2, 3] [2, 1]

[1, 2, 3, 4] [3, 2, 1]

[1, 2, 3, 4, 5] [4, 3, 2, 1]

0
    for i in range(2,int(raw_input())+2): 
        print ''.join(([unicode(k) for k in range(1,i)]))+""+''.join(([unicode(k) for k in range(i-2,0,-1)]))
        print ''.join(map(unicode,range(1,i)))+""+''.join(map(unicode,range(i-2,0,-1)))

Hope this helps.

0
source

Use this code:

prefix = ''
suffix = ''

for i in range(1,n):
    middle = str(i)
    string = prefix + middle + suffix
    print(string)

    prefix = prefix + str(i)
    suffix = ''.join(reversed(prefix)) 

-1
source

Source: https://habr.com/ru/post/1621962/


All Articles