Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python program to print the pattern 1 121 12321 12 1 for n rows [duplicate]

Tags:

python

When n = 5 the pattern must be:

    1
   121
  12321
   121
    1

What I tried till now:

# Pattern 1-121-12321 pyramid pattern

# Reading number of rows
row = int(input('Enter how many lines? '))

# Generating pattern
for i in range(1,row+1):
    
    # for space
    for j in range(1, row+1-i):
        print(' ', end='')
    
    # for increasing pattern
    for j in range(1,i+1):
        print(j, end='')
    
    # for decreasing pattern 
    for j in range(i-1,0,-1):
        print(j, end='')
    
    # Moving to next line
    print()

Output I am getting:

    1
   121
  12321
 1234321
123454321

I know the logic for similar such patterns like:

    *
   ***
  *****
 *******

But the number pattern seems to be confusing and I am not able to get the logic.

TL;DR

Python Program for generating the following Pattern for printing n rows:

eg. When n=7:

   1
  121
 12321
1234321
 12321
  121
   1 
like image 923
VaiMan Avatar asked Sep 29 '21 07:09

VaiMan


People also ask

How to print pattern 1 12 123 in Python?

Now, we will see python program to print pattern 1 12 123. Firstly, we will initialize a variable num=3. The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns. print (j, end=” “) is used to display numbers and the other print (“”) is used for the next line after each row.

How to print the number of rows in a number pattern?

Pattern - 1: Number Pattern Code - rows = int(input("Enter the number of rows: ")) # Outer loop will print number of rows for i in range(rows+1): # Inner loop will print the value of i after each iteration for j in range(i): print(i, end=" ") # print number # line after each row to display pattern correctly print(" ")

How to print pattern using nested for loop in Python?

This is the python program to print pattern 1 12 123. Here, we will see python program to print pattern using nested for loop. Firstly, we will use a function def pattern (n). The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.

How to display a pattern in Python with multiple rows?

Firstly, we will use a function def pattern (n). The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns. print (“*”, end=” “) is used to display the pattern and the other print (“”) is used for the next line after each row. Here, n=5 is initialized and then the function is called.


2 Answers

How about this code:

for i in range(n):
    temp = 0 # Store the value of the number to square (so 1, 11,...)
    if (i < n / 2):
        for j in range(i + 1):
            temp += 10 ** j # Construct the number (1 = 1, 11 = 1 + 10, 111 = 1 + 10 + 10 ^ 2,...)
        for _ in range(int(n / 2 - i)):
            print(' ', end = '') # Add space indentation
    else:
        for j in range(n - i): # Count in reverse now
            temp += 10 ** j # Construct the number (1 = 1, 11 = 1 + 10, 111 = 1 + 10 + 10 ^ 2,...)
        for _ in range(int(i - n / 2) + 1):
            print(' ', end = '') # Add space indentation
    print(temp ** 2) # Square the temporary value

(Fun mathematical fact: the string you print have a property of:
1 = 1 ^ 2; 121 = 11 ^ 2; 12321 = 111 ^ 2;...)

like image 95
Nam Nguyen Hoang Avatar answered Oct 19 '22 19:10

Nam Nguyen Hoang


I will also give my suggestion. So here I initially just outsource the production of the line into a method. I first calculate the length of the current line and depending on the maximum number of lines you want to print determine the padding to get this star shape. Then I produce the actual string, which is dependent on the middle point of the string, so as soon as I exceed this, I decrease the numbering again.

from math import ceil
def produce_line(line, maximum):
    middle = ceil(maximum / 2)
    padding = abs(middle - line)
    line_width = maximum - 2 * padding
    half_lw = ceil(line_width / 2)
    res = (" " * padding) + "".join([str(a) if a <= half_lw else str(abs(2 * half_lw - a)) for a in range(1, line_width + 1)])
    return res
    
lines = 9
for i in range(1, lines + 1):
    print(produce_line(i, lines))
like image 30
Christian Avatar answered Oct 19 '22 19:10

Christian