Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print design door mat texture using characters
A door mat pattern is a decorative text design that creates a symmetric mat-like structure. In this pattern, we use hyphens (-), dots (.), and pipe symbols (|) to create a design with "WELCOME" in the center. The mat has dimensions n × m, where m is a multiple of n.
Understanding the Pattern
The door mat follows this structure:
- Top half: Expanding pattern from 1 to n-1 (odd numbers only)
- Middle: "WELCOME" text surrounded by hyphens
- Bottom half: Mirror of the top half in reverse order
Each row contains:
- Left padding of hyphens
- Pattern units of ".|." repeated
- Right padding of hyphens
Example with Small Dimensions
Let's start with a simple example where n = 5 and m = 15:
def create_door_mat(n, m):
# Top half - expanding pattern
for i in range(1, n, 2):
pattern = '.|.' * i
padding = '-' * ((m - len(pattern)) // 2)
print(padding + pattern + padding)
# Middle - WELCOME text
welcome_padding = '-' * ((m - 7) // 2)
print(welcome_padding + 'WELCOME' + welcome_padding)
# Bottom half - contracting pattern (mirror of top)
for i in range(n-2, -1, -2):
pattern = '.|.' * i
padding = '-' * ((m - len(pattern)) // 2)
print(padding + pattern + padding)
# Example with small dimensions
n = 5
m = 15
create_door_mat(n, m)
------.|.------ ---.|..|..|.--- ----WELCOME---- ---.|..|..|.--- ------.|.------
Complete Implementation
Here's the complete solution with a larger example:
def create_door_mat(n, m):
# Top half - expanding pattern
for i in range(1, n, 2):
pattern = '.|.' * i
padding = '-' * ((m - len(pattern)) // 2)
print(padding + pattern + padding)
# Middle - WELCOME text
welcome_padding = '-' * ((m - 7) // 2)
print(welcome_padding + 'WELCOME' + welcome_padding)
# Bottom half - contracting pattern (mirror of top)
for i in range(n-2, -1, -2):
pattern = '.|.' * i
padding = '-' * ((m - len(pattern)) // 2)
print(padding + pattern + padding)
# Test with larger dimensions
n = 7
m = 21
print(f"Door mat pattern ({n} x {m}):")
create_door_mat(n, m)
Door mat pattern (7 x 21): ----------.|.---------- -------.|..|..|.------- ----.|..|..|..|..|.---- -------WELCOME------- ----.|..|..|..|..|.---- -------.|..|..|.------- ----------.|.----------
How It Works
The algorithm works in three phases:
- Top Half: Loop from 1 to n-1 with step 2, creating expanding rows
- Middle: Print "WELCOME" with appropriate padding
- Bottom Half: Loop from n-2 to 0 with step -2, creating contracting rows
For each row, the padding calculation ensures the pattern stays centered:
- Pattern length = i × 3 (each ".|." is 3 characters)
- Padding on each side = (m - pattern_length) ÷ 2
Conclusion
The door mat pattern uses symmetric loops and string repetition to create a decorative text design. The key is calculating proper padding to center each row and using odd numbers to create the expanding/contracting effect.
