HackerRank: Designer Door Mat Notes

Background

I think the "Designer Door Mat" is suitable for serving as a practice for your understanding of the Python center() method.

Problem

The problem gives you two number N and M where N is an odd natural number and M=3N. You should print out a graph like this:

    Size: 7 x 21 
    ---------.|.---------
    ------.|..|..|.------
    ---.|..|..|..|..|.---
    -------WELCOME-------
    ---.|..|..|..|..|.---
    ------.|..|..|.------
    ---------.|.---------
    
    Size: 11 x 33
    ---------------.|.---------------
    ------------.|..|..|.------------
    ---------.|..|..|..|..|.---------
    ------.|..|..|..|..|..|..|.------
    ---.|..|..|..|..|..|..|..|..|.---
    -------------WELCOME-------------
    ---.|..|..|..|..|..|..|..|..|.---
    ------.|..|..|..|..|..|..|.------
    ---------.|..|..|..|..|.---------
    ------------.|..|..|.------------
    ---------------.|.---------------

The "WELCOME" should be written in the center, and the design pattern should only contain | , . and - characters.

Constraints

  • \( 5 < N < 101 \)
  • \( 15 < M < 303 \)

Solution

The key thing to notice here is that only the ".|." pattern is repeating. Also, as I mentioned in this article, the general pattern for the number of ".|." in each line is 1, 3, 5, 7...

My solution is printing from the first line to the middle first. While printing, I also store the lines containing ".|." into a list as a stack and then print out the lower part using the stack.

# Enter your code here. Read input from STDIN. Print output to STDOUT

n,m = map(int,input().split())

mid = int((n+1)/2)
bottom = []
for i in range(mid-1):
        result = (".|."*(2*i+1)).center(m,"-")
        print(result)
        bottom.insert(0,result+"\n")

print("WELCOME".center(m,"-"))
print("".join(bottom))

My solution should be a bit faster than the one given in the Editorial section.

I hope this article helps!

Reference

  1. HackerRank: Designer Door Mat 讲解 (My Chinese Blog)