본문 바로가기

알고리즘/프로그래머스

[Python] 숫자 문자열과 영단어

https://school.programmers.co.kr/learn/courses/30/lessons/81301

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

# 숫자 문자열과 영단어

def solution(s):
    word2idx = {
        'zero' : '0',
        'one' : '1', 
        'two': '2', 
        'three' : '3', 
        'four' : '4', 
        'five': '5', 
        'six': '6', 
        'seven' : '7', 
        'eight' : '8', 
        'nine' : '9'
    }
    
    num = ''
    n_list = []
    
    for i in range(len(s)):
        if num in word2idx.keys():
            n_list.append(word2idx[num])
            num = ''        
        
        if s[i].isdigit():
            n_list.append(s[i])
            
        else:
            num += s[i]
            
    if num != '' and num in word2idx.keys():
        n_list.append(word2idx[num])

    answer = int(''.join(n_list))
            
    return answer

 

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[Python] 삼총사  (0) 2022.11.22
[Python] 구명 보트  (0) 2022.11.21
[Python] K번째 수  (0) 2022.11.19
[Python] 영어 끝말잇기  (0) 2022.11.18
[Python] 짝지어 제거하기  (0) 2022.11.17