HackerRank Python: Designer Door Mat Solution

Table of Contents

Question

Solution

				
					N, M = map(int, input().split())

# Upper Half
for i in range(N//2):
    pattern = ".|." * (2*i + 1)
    print(pattern.center(M, "-"))

# Center Line
print("WELCOME".center(M, "-"))

# Lower Half
for i in range(N//2-1, -1, -1):
    pattern = ".|." * (2*i + 1)
    print(pattern.center(M, "-"))

				
			
  1. The input values for N and M are read from the user and stored in the variables.

  2. The first loop generates the upper half of the door mat. It iterates from 0 to N//2 (excluding) and prints each line. The line is constructed by creating a pattern with ".|." repeated (2*i + 1) times and centering it within a line of width M filled with “-” characters using the center method.

  3. The “WELCOME” line is printed in the center using the center method.

  4. The second loop generates the lower half of the door mat. It iterates from N//2-1 down to 0 (including) and prints each line. The line is constructed similarly to the upper half.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment