Hackerrank - Designer Door Mat Solution
1 min read

Hackerrank - Designer Door Mat Solution

Hackerrank - Designer Door Mat Solution

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 X. ( is an odd natural number, and  is  times .)
  • 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

Output Format

Output the design pattern.

Sample Input

9 27

Sample Output

------------.|.------------
---------.|..|..|.---------
------.|..|..|..|..|.------
---.|..|..|..|..|..|..|.---
----------WELCOME----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------

Solution in Python

#take rows and columns and convert both to integer using map function 
rows,columns = map(int,input().split())
#Middle row where "WELCOME" will be written
middle = rows//2+1
#Top part of door mat
for i in range(1,middle):
    #calculate number of .|. required and multiply with .|.
    center = (i*2-1)*".|."
    #Move our center pattern to center using string.center method and fill left and part with "-" 
    print(center.center(columns,"-"))
#print middle part welcome
print("WELCOME".center(columns,"-"))
#create bottom part in reverse order like we did in the top part
for i in reversed(range(1,middle)):
    center = (i*2-1)*".|."
    print(center.center(columns,"-"))

Another solution

x,y = map(int,input().split())
items = list(range(1,x+1,2))
items = items+items[::-1][1:]
for i in items:
    text= "WELCOME" if i == x else '.|.'*i
    print (text.center(y,'-'))

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×