say repository

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

알고리즘 문제 풀이/프로그래머스

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

부끄러엇피치 2022. 3. 22. 14:10
728x90

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

출처 : 프로그래머스

 

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

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

programmers.co.kr

def solution(s):
    word = ['zero','one','two','three','four','five','six','seven','eight','nine']
    answer = ""
    alp = ""
    for i in s:
        if i.isdigit(): # 숫자인지
            answer += i
        else:
            alp += i
            if alp in word:
                answer += str(word.index(alp))
                alp = ""

    return int(answer)

풀이 1

숫자면 그냥 답에 추가해준다.

숫자가 아니라면 영문자 문자열에 넣다가 word에 저장해둔 값과 같은 값이 나온다면 word의 인덱스를 답에 넣어주고 영문자 리셋 ""한다.


def solution(s):
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

    for i in range(len(words)):
        s = s.replace(words[i], str(i))

    return int(s)

풀이 2

이건 다른 사람들 풀이이다..

replace를 썼다..

ㅠㅠ 대단한 사람들..

 


2021 카카오 채용연계형 인턴쉽 코딩테스트 문제다.