Question: Designer Door Mat [Python Strings]
Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications:
- Mat size must be NxM. (N is an odd natural number, andM is 3 times N.)
- The design should have 'WELCOME' written in the center.
- The design pattern should only use |, . and - characters.
Sample Designs
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
Size: 11 x 33
---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
Input Format
A single line containing the space separated values of and .
Constraints
- 5 < N < 101
- 15 < M < 303
Output Format
Output the design pattern.
Sample Input
9 27
Sample Output
------------.|.------------
---------.|..|..|.---------
------.|..|..|..|..|.------
---.|..|..|..|..|..|..|.---
----------WELCOME----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------
Possible solutions
We will now solve the given problem using various methods and explained each one.
Let us jump into possible solutions.
Solution-1: Using the map function
Let us first solve the problem using the map() function and then we will explain the code.
n,m=map(int,input().split())
s1='-'
s='.|.'
c=(m-3)//2
for i in range(0,n//2):
print(s1*c+s*(2*i+1)+s1*c)
c-=3
print(s1*((m-7)//2)+'WELCOME'+s1*((m-7)//2))
c=3
for i in range((n//2)-1,-1,-1):
print(s1*c+s*(2*i+1)+s1*c)
c+=3
This Solution takes two integer inputs, n and m, and generates an ASCII art message that says "WELCOME". It consists of two halves, the upper half and the lower half, both formed by repeating the pattern '.|.' with dashes on either side. The width of the message is determined by m while the height is determined by n. The number of dashes on either side decreases by 3 after each iteration in the upper half and increases by 3 after each iteration in the lower half. The message "WELCOME" is centered in the middle row of the design.
Solution-2: Using user-defined functions
Now we will use the user-defined functions to solve the given problem.
row,col = map(int,input().split(" "))
ch = ".|."
wel = "WELCOME"
def upper(row,col,ch):
for i in range(row//2):
exp = ch*(2*i+1)
print(exp.center(col,"-"))
def middle(row,col,wel):
print(wel.center(col,"-"))
def bottom(row,col,ch):
for i in range(row//2):
exp = ch*((row-2)-2*i)
print(exp.center(col,"-"))
if __name__ == "__main__": # Calling functions
upper(row,col,ch)
middle(row,col,wel)
bottom(row,col,ch)
This solution generates an ASCII art message that says "WELCOME
". It takes two inputs, row and col, which represent the number of rows and columns for the message respectively. The message consists of three parts: the upper half, the middle row with the word "WELCOME
", and the bottom half. Each part is generated by a separate function: upper, middle, and bottom. The upper and bottom halves are formed by repeating the pattern ".|." with dashes on either side, while the middle row is simply the word "WELCOME
" centered with dashes. The width of the message is determined by col while the height is determined by row. The upper function generates the upper half of the message, the middle function generates the middle row, and the bottom function generates the bottom half of the message. The functions are then called within the main block to generate the full message.
Solution-3: Using if-else statements
Now let us see how we can solve the same problem using the if-else statements in Python.
dm = list(map(int, input().rstrip().split()))
n=dm[0]
m=dm[1]
t=(m-7)//2
k=(n//2)+1
for i in range (1,n+1):
if i==k:
print('-'*t+'WELCOME'+'-'*t)
elif(i<k):
print('---'*(k-i)+('.|.'*(i))+('.|.'*(i-1))+('---'*(k-i)))
else:
print(('---'*(i-k))+('.|.'*(n-i+1))+('.|.'*(n-i))+('---'*(i-k)))
This code generates an ASCII art message that says "WELCOME
". It takes two inputs, n and m, which represent the number of rows and columns for the message respectively. The message consists of three parts: the upper half, the middle row with the word "WELCOME
", and the bottom half. The upper half and bottom half are formed by repeating the pattern '.|.' with dashes on either side. The number of dashes decreases on each iteration in the upper half and increases on each iteration in the bottom half, until the middle row is reached where "WELCOME
" is centered with dashes. The width of the message is determined by m while the height is determined by n
. The code uses a for loop to iterate through the number of rows and conditions to determine what is printed in each row.
Solution-4: Using for loop
We will now solve the problem using the for loop and will explain the code.
height,width=list(map(int,input().split()))
j=1
for i in range(height):
if i<height//2 :
print((".|."*j).center(width,"-"))
j+=2
elif i==height//2:
print("WELCOME".center(width,"-"))
elif i>height//2 :
j-=2
print((".|."*j).center(width,"-"))
This solution also takes two inputs, height and width, which represent the number of rows and columns for the message respectively. The message consists of three parts: the upper half, the middle row with the word "WELCOME
", and the bottom half. The upper half and bottom half are formed by repeating the pattern '.|.' with dashes on either side. The pattern starts with 1 '.|.' and increases by 2 for each iteration in the upper half, until it reaches the middle row where "WELCOME
" is centered with dashes. In the bottom half, the pattern decreases by 2 for each iteration, until it reaches the bottom row. The width of the message is determined by width while the height is determined by height. The code uses a for loop to iterate through the number of rows and conditions to determine what is printed in each row. The center function is used to center the pattern and the word "WELCOME
" with dashes.
Summary
In this short article, we discussed how we can solve the Designer Door Mat problem from the hacker rank. We solved the problem using four different methods and explained each of them.
Further Reading
Question on Hacker Rank: Python Designer Door Mat [Strings]