Open In App

Program to print a doormat pattern having a string written in the center in Python

Last Updated : 28 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given the number of rows R and the number of columns C, the task is to print a doormat pattern as shown in the examples below. The number of columns in the mat’s design must be 3 times the number of rows in the design. The doormat should have GeeksforGeeks printed in the center. The width of the mat is equal to the number of columns in the pattern. Note that the string “GeeksforGeeks” contains 13 characters which is an odd number, therefore the number of rows has to be odd to design the mat properly by maintaining the symmetricity. Examples:

Input: R = 11, C = 33 
Output:
    ---------------|*|---------------
    ------------|*||*||*|------------
    ---------|*||*||*||*||*|---------
    ------|*||*||*||*||*||*||*|------
    ---|*||*||*||*||*||*||*||*||*|---
    ----------GeeksforGeeks----------
    ---|*||*||*||*||*||*||*||*||*|---
    ------|*||*||*||*||*||*||*|------
    ---------|*||*||*||*||*|---------
    ------------|*||*||*|------------
    ---------------|*|---------------

Input: R = 9, C = 27
Output:
    ------------|*|------------
    ---------|*||*||*|---------
    ------|*||*||*||*||*|------
    ---|*||*||*||*||*||*||*|---
    -------GeeksforGeeks-------
    ---|*||*||*||*||*||*||*|---
    ------|*||*||*||*||*|------
    ---------|*||*||*|---------
    ------------|*|------------

Approach: The whole pattern can be divided into 3 different parts. The first part contains a pattern having a triangle. The second part contains a string “GeeksforGeeks” and the third part contains a pattern having an inverted triangle. This pattern can easily be printed having a top and inverted triangle by using inbuilt string alignment functions.

  1. ljust : This method returns a left-aligned string having length l.
  2. center : This method returns a center-aligned string having length l.
  3. rjust : This method returns a right-aligned string having length l.

Below is the implementation of the above approach: 

Python3




# Python3 implementation of the approach
 
# Function to print the doormat pattern
def doormatPattern(rows, columns):
    width = columns
     
    # Print the pattern having a top triangle
    for i in range (0, int (rows / 2)):
        pattern = "|*|" * ((2 * i) + 1)
        print (pattern.center (width, '-'))
     
    # Print GeeksforGeeks in the center
    print ("GeeksforGeeks".center (width, '-'))
     
    # Print the pattern having
    # an inverted triangle
    i = int (rows / 2)
    while i > 0:
        pattern = "|*|" * ((2 * i) - 1)
        print (pattern.center (width, '-'))
        i = i-1
    return
 
# Driver code
rows = 7
columns = rows * 3
doormatPattern(rows, columns)


Output:

---------|*|---------
------|*||*||*|------
---|*||*||*||*||*|---
----GeeksforGeeks----
---|*||*||*||*||*|---
------|*||*||*|------
---------|*|---------

Time complexity : O(n^2), where n is the number of rows.

Space complexity : O(n^2)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads