'Printing Pattern in Python

1. The Problem

Given a positive integer n. Print the pattern as shown in sample outputs.

A code has already been provided. You have to understand the logic of the code on your own and try and make changes to the code so that it gives correct output.

1.1 The Specifics

Input: A positive integer n, 1<= n <=9

Output: Pattern as shown in examples below

Sample input:

4

Sample output:

4444444

4333334

4322234

4321234

4322234

4333334

4444444

Sample input:

5

Sample output:

555555555

544444445

543333345

543222345

543212345

543222345

543333345

544444445

555555555

2. My Answer

2.1 My Code

n=int(input())

answer=[[1]]
for i in range(2, n+1):
    t=[i]*((2*i)-3)
    answer.insert(0, t)
    answer.append(t)
    for a in answer:
        a.insert(0,i)
        a.append(i)


print(answer)      
outlst = [' '.join([str(c) for c in lst]) for lst in answer]

for a in outlst:
    print(a)

2.2 My Output

Input: 4
4 4 4 4 4 4 4 4 4

4 4 3 3 3 3 3 3 3 4 4

4 4 3 3 2 2 2 2 2 3 3 4 4

4 3 2 1 2 3 4

4 4 3 3 2 2 2 2 2 3 3 4 4

4 4 3 3 3 3 3 3 3 4 4

4 4 4 4 4 4 4 4 4

2.3 Desired Output

4444444

4333334

4322234

4321234

4322234

4333334

4444444


Solution 1:[1]

Your answer isn't as expected because you add the same object t to the answer list twice:

answer.insert(0, t)
answer.append(t)

More specifically, when you assign t = [i]*(2*i - 3), a new data structure is created, [i, ..., i], and t just points to that data structure. Then you put the pointer t in the answer list twice.

In the for a in answer loop, when you use a.insert(0, i) and a.append(i), you update the data structure a is pointing to. Since you call insert(0, i) and append(i) on both pointers that point to the same data structure, you effectively insert and append i to that data structure twice. That's why you end up with more digits than you need.

Instead, you could run the loop for a in answer for only the top half of the rows in the answer list (and the middle row that has was created without a pair). E.g. for a in answer[:(len(answer)+1)/2].

Other things you could do:

  • using literals as the arguments instead of reusing the reference, e.g. append([i]*(2*i-3)). The literal expression will create a new data structure every time.
  • using a copy in one of the calls, e.g. append(t.copy()). The copy method creates a new list object with a "shallow" copy of the data structure.

Also, your output digits are space-separated, because you used a non-empty string in ' '.join(...). You should use the empty string: ''.join(...).

Solution 2:[2]

n=5
answer=[[1]]
for i in range(2, n+1):
    t=[i]*((2*i)-3)
    answer.insert(0, t)
    answer.append(t.copy())
    for a in answer:
        a.insert(0,i)
        a.append(i)
answerfinal=[]
for a in answer:
    answerfinal.append(str(a).replace(' ','').replace(',','').replace(']','').replace('[',''))
for a in answerfinal:
    print(a)

Solution 3:[3]

I divided the output area into 6 parts and then assembled it into 2 halves then solved the problem

#include<stdio.h> 
 int main()
 {int N, i, j;
   printf("Enter N: "); 
   scanf("%d", &N);
   for(i=N; i>=1; i--)//UPPER HALF
  {  
    for(j=N; j>i; j--)//1st
       {printf("%d", j);    } 
     for(j=1; j<=(i*2-1); j++)//2nd
       {printf("%d", i);     }
     for(j=i+1; j<=N; j++)//3rd 
       {printf("%d", j);     } 
  printf("\n");//new line
   }
   for(i=1; i<N; i++)//LOWER HALF 
 {
   for(j=N; (j-1)>i; j--)//4th 
      {printf("%d", j); }
   for(j=0; j<=(i*2-1); j++)//5th
      {printf("%d", i+1); }
   for(j=i+1; j<=N; j++)//6th 
      {printf("%d", j); } 
  printf("\n");//new line
  } 
 return 0;
}

And it works and gives desirable output Please suggest if there is any more shortcut way to solve it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mypetlion
Solution 3 SATISH KUMAR PATRA