ブリブリ備忘録 おっ、python

HackerRankの問題とコメント(python3) 拙いですが...

Designer Door Mat

・問題

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----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------

ソースコード

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

・コメント

ドアマット(?)を設計する問題。コピペでドアマットの形が崩れてしまったので、正しい形は以下のURL参照。

ドアマットの形に着目して、.|.が何個存在するかを考え、前回の問題で登場した.centerを用いて中央に配置する。

例などから分かるように、真ん中の行を挟んで対象なので、その性質を用いている。

patternという配列があれば、それと並びが逆の配列は、pattern[::-1]という形で指定することができる。

・URL

https://www.hackerrank.com/challenges/designer-door-mat/problem