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

천호영·2022년 4월 29일
0

알고리즘

목록 보기
12/100
post-thumbnail

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

python의 replace를 통해 치환하면 가볍게 풀리는 문제이다. 처음에 이름이 생각이 안나서 replaceall을 사용해야 하는 줄 알았는데, 그냥 replace였다.

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

    for i, eng in enumerate(num_eng_match):
        s = s.replace(eng, str(i))

    return int(s)

정리

  • str.replace(old, new[, count])

    맨 뒤에 count를 함께 전달하면 초반에 나오는 count개수만큼만 치환한다(공식문서)

profile
성장!

0개의 댓글