Saturday, April 6, 2019

Designer Door Mat - Hacker Rank 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----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------
Designer Door Mat - Hacker Rank Solution

Python String center() Method

Syntax
str.center(width[, fillchar])
width: Total width of the string.
fillchar: Filler character.
Problem Setter's code:
N,M = map(int,raw_input().split())
for i in xrange(1, N, 2): 
    print ( str('.|.')*i ).center(M, '-')
print str('WELCOME').center(M, '-')
for i in xrange(N-2, -1, -2): 
    print ( str('.|.')*i ).center(M, '-')

7 comments:

  1. Replies
    1. use range instead of xrange

      Delete
    2. use range instead of xrange and input instead of raw input

      Delete
  2. n,m = map(int,input().split())
    pattern = [ ((".|.")*(2*i+1)).center(m,'-') for i in range(n//2) ]
    print('\n'.join( pattern + ["WELCOME".center(m,'-')] + pattern[::-1] ))

    ReplyDelete
  3. N,M = map(int,input().split())
    c = ".|."
    for i in range(N//2):
    print((c*((i*2)+1)).center(M, '-'))
    print(('WELCOME'.center(M,'-')))
    for j in range(N//2):
    print((c*((N-(j*2))-2)).center(M, '-'))

    This is the solution I came up with

    ReplyDelete
  4. What's the meaning of
    "if__ name__=="__ main__":" in hackerrank

    ReplyDelete
    Replies
    1. it represents main class just like void main in java

      Delete

Powered by Blogger.