문제
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
풀이
1) 딕셔너리 이용
def solution(s):
answer = ''
dict = {
'zero' : '0',
'one' : '1',
'two' : '2',
'three' : '3',
'four' : '4',
'five' : '5',
'six' : '6',
'seven' : '7',
'eight' : '8',
'nine' : '9'
}
i = 0
while i < len(s):
if s[i] in dict.values():
answer+=s[i]
i += 1
else:
if s[i:i+3] in dict.keys():
answer+=dict[s[i:i+3]]
i += 3
elif s[i:i+4] in dict.keys():
answer+=dict[s[i:i+4]]
i += 4
elif s[i:i+5] in dict.keys():
answer+=dict[s[i:i+5]]
i += 5
return int(answer)
2) 배열 이용
def solution(s):
nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
for i in range(len(nums)):
s = s.replace(nums[i], str(i))
return int(s)
'⏳ 알고리즘 > python 알고리즘 문제 풀이' 카테고리의 다른 글
프로그래머스 - 삼각 달팽이(월간 코드 챌린지 시즌1) (0) | 2021.11.19 |
---|---|
프로그래머스 - LV2. 더 맵게 (0) | 2021.11.06 |
프로그래머스 - 로또의 최고 순위와 최저 순위( 2021 Dev-Matching ) (0) | 2021.10.31 |
리트코드 - 19.Remove Nth Node From End of List (0) | 2021.08.29 |
프로그래머스 - LV3. 베스트앨범 (0) | 2021.06.15 |