1. 문제
https://programmers.co.kr/learn/courses/30/lessons/81301?language=python3
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
2. 나의 풀이
def solution(s):
answer = 0
s = s.replace('one', '1')
s = s.replace('two', '2')
s = s.replace('three', '3')
s = s.replace('four', '4')
s = s.replace('five', '5')
s = s.replace('six', '6')
s = s.replace('seven', '7')
s = s.replace('eight', '8')
s = s.replace('nine', '9')
s = s.replace('zero', '0')
answer = int(s)
# print(s)
return answer
3. 다른 사람 풀이
num_dic = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"}
def solution(s):
answer = s
for key, value in num_dic.items():
answer = answer.replace(key, value)
return int(answer)
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
(Python)프로그래머스 코딩테스트 연습 - 2019 카카오 개발자 겨울 인턴십 - 크레인 인형뽑기 게임 (0) | 2021.11.15 |
---|---|
(Python)프로그래머스 코딩테스트 연습 - 2020 카카오 인턴십 - 키패드 누르기 (0) | 2021.11.11 |
(Java)프로그래머스 코딩테스트 연습 - 그래프 - 가장 먼 노드 (0) | 2021.01.18 |
(Java)프로그래머스 코딩테스트 연습 - 깊이/너비 우선 탐색(DFS/BFS) - 단어 변환 (0) | 2021.01.11 |
(Java)프로그래머스 코딩테스트 연습 - 탐욕법(Greedy) - 섬 연결하기 (0) | 2021.01.09 |