티스토리 뷰
📚 문제

입력

출력

예제 입력
8 28
예제 출력
8 9 18 15 14 19 11 17 16 13
12 10 28 25 24 21 27 26 23 22
20
🧑🏻💻 풀이 과정
- 숫자를 단어로 변환하기 위한 딕셔너리(
word_dict
)를 선언하자. - m부터 n까지의 숫자를 하나씩 가져온 뒤, 숫자 -> 문자로 변환(
word = str(i)
)해서 다시 반복하면서 2자리 이상이면 변환된 단어뒤에" "
를 추가해서 저장하고, 한 자리면 변환된 단어만 저장하자. - 이렇게 구한 값을 문자를 키로, 숫자를 값으로 저장(
_dict
)하자. - 키(문자)를 오름차순 정렬해서 출력하자.
import sys
def number_game(m, n):
global word_dict
_dict = {}
for i in range(m, n + 1):
word = str(i)
word_len = len(word)
tem = ""
for j in range(word_len):
if j + 1 < word_len:
tem += (word_dict[int(word[j])] + " ")
else:
tem += word_dict[int(word[j])]
_dict[tem] = _dict.setdefault(tem, word)
return _dict
m, n = map(int, sys.stdin.readline().rstrip().split())
word_dict = {0: 'zero', 1: 'one', 2: 'two', 3: 'there', 4: 'four', 5: 'five', 6: 'six', 7: 'seven',
8: 'eight', 9: 'nine'}
ans = number_game(m, n)
print_cnt = 1
for i in sorted(ans.keys()):
if print_cnt == 10:
print(ans[i])
print_cnt = 1
else:
print(ans[i], end=" ")
print_cnt += 1
다른 사람이 풀이한 결과를 보면서 스스로 많이 부족하단 것을 느꼈던 시간이였다..
더 깔끔한 코드가 나오도록 연습하자!
'알고리즘' 카테고리의 다른 글
[algorithm] 백준 16499 - 동일한 단어 그룹화하기 (파이썬) (0) | 2022.09.09 |
---|---|
[algorithm] 백준 8892 - 팰린드롬 (파이썬) (0) | 2022.09.08 |
[algorithm] 백준 1764 - 듣보잡 (파이썬) (0) | 2022.09.06 |
[algorithm] 백준 10815 - 숫자 카드 (파이썬) (0) | 2022.09.05 |
[algorithm] 백준 15702 - 중간고사 채점 (파이썬) (0) | 2022.09.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 그리디
- 알고리즘
- 코테
- 인프런
- kotlin
- mysql 8.0
- 리팩토링
- spring boot
- Real MySQL
- Spring
- 정렬
- 스프링부트
- 데이터베이스
- 노마드코더
- 코틀린
- webflux
- 스프링
- Algorithm
- 문자열
- 노마드
- 릿코드
- 스프링 부트
- leetcode
- 백준
- 파이썬
- 북클럽
- 구현
- 김영한
- 자료구조
- MySQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함