Designer Door Mat in Python - HackerRank Solution

Designer Door Mat in Python - HackerRank Solution
Designer Door Mat in Python - HackerRank Solution


Problem :


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 N X M. ( N is an odd natural number, and M 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 N and M.

Constraints :

  • 5 < N < 101
  • 15 < M < 303

Output Format :

Output the design pattern.



Sample Input :

9 27

Sample Output :

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



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Designer Door Mat in Python - HackerRank Solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
# Designer Door Mat in Python - HackerRank Solution START
N, M = map(int, input().split())
for i in range(1, N, 2):
    print(str('.|.' * i).center(M, '-'))
print('WELCOME'.center(M, '-'))
for i in range(N-2, -1, -2):
    print(str('.|.' * i).center(M, '-'))
# Designer Door Mat in Python - HackerRank Solution END





Disclaimer :
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
9 Comments
  • Unknown
    Unknown Thursday, December 31, 2020

    it’s very nice blog! Shelf Mat Sheets

  • Full Oyun
    Full Oyun Wednesday, March 10, 2021

    solution is wrong. please check again.

  • Full Oyun
    Full Oyun Wednesday, March 10, 2021

    but it work when i change python 3, so some of code is not working on python 2, lol

  • Zayed
    Zayed Wednesday, June 23, 2021

    Code is wrong, The last line of output is incorrect

  • erwerwe
    erwerwe Wednesday, November 03, 2021

    N, M = map(int, input().split())
    iter = N//2
    s=""
    for i in range(0,iter):
    first = (M - 3*((2*i)+1))//2
    second=2*(i)+ 1
    s=(first*"-") + (second*".|.") + (first*"-")
    i=i+1
    print(s)
    print((M-7)//2*"-"+"WELCOME"+(M-7)//2*"-")

    j=iter-1
    while(j>=0):

    first = (M - 3*((2*j)+1))//2
    second=2*(j)+ 1
    s=(first*"-") + (second*".|.") + (first*"-")
    j=j-1
    print(s)

  • AnDanh
    AnDanh Tuesday, November 16, 2021

    This comment has been removed by the author.

  • AnDanh
    AnDanh Tuesday, November 16, 2021

    n, m = map(int, input().split())
    c = ".|."
    bw = int(n/2);
    for i in range(n):
    if(bw == i):
    print("WELCOME".center(m, "-"))
    elif(i < bw):
    print((c*(2*i+1)).center(m, "-"))
    else:
    print((c*(2*(n-(i+1))+1)).center(m,"-"))

  • Leonid Churakov
    Leonid Churakov Thursday, December 30, 2021

    You also can use recursion:

    import math

    def print_welcome(height, width, pic = '.|.', size = 1): # default parameters: pic == picture simbols,
    # size == start count of pics
    n = height
    if n == 1: # check if it's last recursion element.
    print('WELCOME'.center(width, '-'))
    else:
    print((pic * size).center(width, '-')) # print descend count of elemens
    size += 2 # increase count of pic elements
    print_welcome(n-1, width, pic, size) # recursive call
    size -= 2 # decrease count of pic elements. work once
    print((pic * size).center(width, '-')) # prints ascend count of pics

    height, width = map(int, input().split())

    half_height = height_half = math.trunc((height) / 2 + 1) # calculates half of height for recursion
    print_welcome(half_height, width)

  • Unknown
    Unknown Tuesday, April 05, 2022

    N,M=map(int,input().split())
    for i in range(1,N,2):
    print(str('.|.' * i).center(M,'-'))
    print('WELCOME'.center(M,'-'))
    for i in range(N-2,-1,-2):
    print(str('.|.' * i).center(M,'-'))

    Use this above code and give input as 7 21
    and you will get the required output.

Add Comment
comment url