Open In App

Python Program to print digit pattern

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The program must accept an integer N as the input. The program must print the desired pattern as shown in the example input/ output. Examples:

Input : 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *’s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the first line. The second digit is 1 so print one *. So on and the last i.e., the fifth digit is 5 hence print five *s in the fifth line. Input : 60710 Output : |****** | |******* |* |

Approach Read the input For each digit in the integer print the corresponding number of *s If the digit is 0 then print no *s and skip to the next line 

Python3




# function to print the pattern 
def pattern(n): 
  
    # traverse through the elements 
    # in n assuming it as a string 
    for i in n: 
  
        # print | for every line 
        print("|", end = "") 
  
        # print i number of * s in 
        # each line 
        print("*" * int(i)) 
  
# get the input as string         
n = "41325"
pattern(n) 


Output:

|****
|*
|***
|**
|*****

Time complexity: O(n) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant

Alternate solution that takes integer as input : 

Python3




n = 41325
x = [] 
while n>0
    x.append(n%10
    n //= 10
for i in range(len(x)-1,-1,-1): 
    print('|'+x[i]*'*'
  
# code contributed by Baivab Dash 


Output:

|****
|*
|***
|**
|*****

Time complexity: O(n) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(n) since an extra list is used so in the worst case the space taken by the algorithm will be linear

Using List comprehension and join():

Another approach that could be used to solve this problem is to use a list comprehension to generate the desired output.

Here is an example of how this could be implemented:

Python3




def pattern(n):
    # Convert the input number to a string
    n = str(n)
      
    # Use a list comprehension to generate the desired output
    pattern = ["|" + "*" * int(digit) for digit in n]
      
    # Join the elements of the pattern list and print the result
    print("\n".join(pattern))
  
# Test the function
pattern(41325)
  
#This code is contributed by Edula Vinay Kumar Reddy


Output

|****
|*
|***
|**
|*****

This approach has a time complexity of O(n) and a space complexity of O(n), as it requires one traversal of the input and creates a new list to store the generated pattern.



Previous Article
Next Article

Similar Reads

Count of N-digit numbers having digit XOR as single digit
Given an integer N, the task is to find the total count of N-Digit numbers such that the Bitwise XOR of the digits of the numbers is a single digit. Examples: Input: N = 1Output: 9Explanation: 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers. Input: N = 2Output: 66Explanation: There are 66 such 2-digit numbers whose Xor of digits is a single digit number.
14 min read
Count ways to generate N digit number such that its every digit divisible by previous digit
Given a number N, the task is to count the number of ways to create an N digit number from digits 1 to 9 such that every digit is divisible by its previous digit that is if the number is represented by an array of digits A then A[i + 1] % A[i] == 0. print the answer modulo 109 + 7. Examples: Input: N = 2Output: 23Explanation: For N = 2 possible ans
15+ min read
Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Program to Print Butterfly Pattern (Star Pattern)
Given an integer N, print N rows of Butterfly pattern. Examples: Input: 3Output: * *** ********* *** * Input: 5Output: * *** ***** ******* ***************** ******* ***** *** * Approach: The problem can be solved using three nested loops inside an outer loop. The outer loop will run for the rows, the first inner loop will print the stars, the secon
6 min read
Program to Print Inverted Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N - 1) stars, the second row has (2 * N - 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned. Examples: Input: 3Output: ***** *** * Input: 5Output: ********* ******* ****
4 min read
Program to Print Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * *** ***** **************** Approach: The problem ca
4 min read
Python program to print checkerboard pattern of nxn using numpy
Given n, print the checkerboard pattern for a n x n matrix Checkerboard Pattern for n = 8: It consists of n * n squares of alternating 0 for white and 1 for black. We can do the same using nested for loops and some if conditions, but using Python's numpy library, we can import a 2-D matrix and get the checkerboard pattern using slicing. W2'll be us
3 min read
three90RightbarBannerImg