HACKER RANK Python Strings Challenges - Designer Door Mat Solution.'py'

Python Strings Challenges - Designer Door Mat Solution
Problem Statement
Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat.
Design specifications are as follows:
1. Size of mat must be NXM. (N is an odd natural number and M is 3 times N.)
2. Design should have 'WELCOME' written in the center.
3. Design pattern should only use '|', '.' and '-' characters.

Sample Designs
    Size: 7 x 21 
    ---------.|.---------
    ------.|..|..|.------
    ---.|..|..|..|..|.---
    -------WELCOME-------
    ---.|..|..|..|..|.---
    ------.|..|..|.------
    ---------.|.---------
    
    Size: 11 x 33
    ---------------.|.---------------
    ------------.|..|..|.------------
    ---------.|..|..|..|..|.---------
    ------.|..|..|..|..|..|..|.------
    ---.|..|..|..|..|..|..|..|..|.---
    -------------WELCOME-------------
    ---.|..|..|..|..|..|..|..|..|.---
    ------.|..|..|..|..|..|..|.------
    ---------.|..|..|..|..|.---------
    ------------.|..|..|.------------
    ---------------.|.---------------
Input Format
Single line containing 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----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------
Note : More than 6 lines of code will result in 0 score. Blank lines are not counted.

Solution:


N,M=map(int,raw_input().split())
a=[('.|.'*i).center(M,'-') for i in range(1,N,2)]
for e in a+['WELCOME'.center(M,'-')]+list(reversed(a)):print(e)

Thanks for Visiting, Hope this helps you....

Copyright © 2015 HackerRank.
All Rights Reserved

Comments

  1. Can you please explain how it works?

    ReplyDelete
  2. For the first time,
    '.|.' prints for 1 time because of for i in range(1,N,2)
    And for the next time it prints for three times, because of '2' in for loop.

    And then, "('.|.'*i).center(M,'-')" means, print '.|.' at the center of M.
    All these operations were saved in a list named 'a'.

    Then, do the same process again, but, with the addition of "Welcome" and reversing the list 'a'.

    ReplyDelete

Post a Comment

Popular Posts