유자차의 재테크 공부방

[프로그래머스] 숫자 문자열과 영단어 본문

파이썬/알고리즘 문제 풀이

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

유자차H 2022. 3. 1. 10:48
반응형

팁!

문자열.replace(a, b) : 문자열 안에 있는 a를 b로 치환


풀이 방법

def solution(s):
    words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    for n in words:
        s = s.replace(n, str(words.index(n)))
    ans = int(s)
    return ans​
def solution(s):
    
     n2s = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
     number = ""
     ans = ""
     for i in s:
         if i.isdigit():
             ans += i
         else:
             number += i
             if number in n2s:
                 ans += str(n2s.index(number))
                 number = ""
    
     return int(ans)
반응형
Comments