728x90
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
- 코드1
def solution(s):
answer = ''
# 영어 : 숫자
en = {'zero':'0','one':'1','two':'2','three':'3','four':'4',
'five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
english = ''
for i in s:
# 숫자면 바로 넣기
if i in '0123456789':
answer += i
# 영어면 영단어 완성할 때까지
else:
english += i
# 영단어가 완성되면 넣기
try:
answer += en[english]
english = ''
except:
pass
return int(answer)
- 코드2
def solution2(s):
# 영어 : 숫자
en = {'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9'}
answer = s
# 영단어를 바로 숫자로 바꾸기
for key, value in en.items():
answer = answer.replace(key, value)
return int(answer)
- 풀면서 얻은 점
- dict.items()
- for 문 돌릴 때 key와 value 동시에 얻을 수 있는 거
- 자주 안쓰니 까먹어 간다..
- dict.items()
728x90
반응형
'TIL - 프로그래밍 > Python 알고리즘' 카테고리의 다른 글
[백준] 1987. 알파벳 - Python (0) | 2022.06.06 |
---|---|
[프로그래머스] Lv.2 오픈채팅방 - Python (0) | 2022.06.05 |
[프로그래머스] Lv.1 로또의 최고 순위와 최저 순위 - Python (0) | 2022.06.04 |
[백준] 1697. 숨바꼭질 - Python (0) | 2022.06.03 |
[프로그래머스] Lv.2 수식 최대화 - Python (0) | 2022.06.02 |
댓글