Algorithm PS/프로그래머스
[프로그래머스] level1 / 숫자 문자열과 영단어 / Python 파이썬
migrationArc
2021. 11. 16. 17:54
[프로그래머스] level1 / 숫자 문자열과 영단어 / Python 파이썬
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
풀이
영어 문자를 숫자와 매칭시켜 변환하는 문제이다.
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 = ''
L = len(s)
for i in range(L):
if s[i].isdecimal():
answer += s[i]
continue
tmp = ''
for j in range(i, L):
tmp += s[j]
if tmp in num_dic:
answer += num_dic[tmp]
break
return int(answer)
더 좋은 풀이
dictionary 와 replace 를 활용한 것이 재미있다.
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)