본문 바로가기

Algorithm/프로그래머스

[프로그래머스] Lv1. 숫자 문자열과 영단어 - Python

Problem : 문제 링크
How to solve : 접근법, 풀이 과정
solution.py : 제출 코드
Improvement : 다른 분들의 풀이를 확인 후 개선한 코드

 

Problem

 

코딩테스트 연습 - 숫자 문자열과 영단어

네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자

programmers.co.kr

How to solve

// 패드에다가 풀이 했음. 정리 후, 여기에 추가할 예정

 


solution.py

def solution(s):
    answer = ""
    words = {
        "zero" : '0',
        "one" : '1', 
        "two" : '2',
        "three" : '3',
        "four" : '4',
        "five" : '5',
        "six" : '6',
        "seven" : '7',
        "eight" : '8',
        "nine" : '9'
    }
    
    word = ""
    
    
    for i in s:
        if i.isdigit():
            answer +=i
            continue
        
        else:
            word+=i
            if words.get(word):
                answer +=words[word]
                word=""
    answer= int(answer)   
    
    
    return answer

Improvement