2023. 7. 11. 20:35ㆍHackerRank-Python
Designer Door Mat | HackerRank
Print a designer door mat.
www.hackerrank.com
문제
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 NXM. (N is an odd natural number, and M is 3 times N.)
- The design should have 'WELCOME' written in the center.
- The design pattern should only use |, . and - characters.
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
=> 사이즈 주면, 도배장판 만들어! ., |, - 만 이용해서. 가운데 줄에는 WELCOME이 들어가야함
코드
N, M = map(int,input().split())
for i in range(N//2):
print(
((".|.") * (2*i+1)).center(M, '-')
)
print(
"WELCOME".center(M,'-')
)
for i in range(N//2, 0, -1):
print(
((".|.") * (2*i -1)).center(M, '-')
)
노트
N, M = map(int,input().split()) # INPUT 값 받아오기
print(N, M) # 7 21
for i in range(N//2): # 윗 줄(.|. 가 늘어나는)
print(
((".|.") * (2*i+1)).center(M, '-')
)
OUTPUT
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
print(
"WELCOME".center(M,'-') # 가운데 줄, WELCOME이 쓰이는 줄
)
for i in range(N//2, 0, -1): # 아랫줄(.|.가 줄어드는)
print(
((".|.") * (2*i -1)).center(M, '-') # 윗줄과 대칭이 되게끔 조정
)
- range([시작수], [끝 수], [간격]) 으로 N//2(=3) 부터 0 직적까지, -1씩 차이나게 i 반복.
- 3, 2, 1 을 거침
OUTPUT
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
정리좀 하면
N, M = map(int, input().split())
for i in range(1, N, 2):
print(
('.|.' * i).center(M, '-')
)
print('WELCOME'.center(M, '-'))
for i in range(N-2, 0, -2):
print(
('.|.' * i).center(M, '-')
)
참조
Prepare > Python > Strings > Text Wrap
Text Wrap | HackerRank Wrap the given text in a fixed width. www.hackerrank.com 문제 You are given a string S and width w. Your task is to wrap the string into a paragraph of width w. string: a single string with newline characters ('\n') where the break
my-little-diary.tistory.com
HackerRank Designer Door Mat
I am trying to solve a problem in HackerRank and stuck in this. Help me to write python code for this question Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door...
stackoverflow.com
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Strings > Alphabet Rangoli (0) | 2023.07.14 |
---|---|
Prepare > Python > Strings > String Formatting (0) | 2023.07.12 |
Prepare > Python > Strings > Text Wrap (0) | 2023.07.10 |
Prepare > Python > Strings > Text Alignment (0) | 2023.07.09 |
Prepare > Python > Strings > String Validators (0) | 2023.07.08 |