[Python3] 프로그래머스 카카오 기출 "숫자 문자열과 영단어" 풀이

돌멩이·2024년 8월 7일
0

알고리즘

목록 보기
6/17

📌 문제 정보

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

유형: 구현

난이도: Lv.1

스스로 풀었는가? ✅

특이사항: 2021 카카오 채용연계형 인턴십 문제




💻 작성 코드

def solution(s):
    answer = s
    digit_eng_dict = {
        "zero": 0,
        "one": 1,
        "two": 2,
        "three": 3,
        "four": 4,
        "five": 5,
        "six": 6,
        "seven": 7,
        "eight": 8,
        "nine": 9
    }
    
    for key in digit_eng_dict.keys():
        answer = answer.replace(key, str(digit_eng_dict.get(key)))
    return int(answer)



🎯 접근 방식

  1. 숫자와 영단어가 1대1로 대응되므로 dictionary를 이용한다.
  2. dictionary를 순회하면서 replace method를 활용해 영단어를 숫자로 바꾼다.



💡 개선사항

  1. dict.keys() 대신 dict.items()을 이용하면 쉽게 key와 value 값을 가져올 수 있다.
  2. dictionary에 선언할 때부터 value를 str로 선언하면 형변환이 필요없다.



[ktb-algorithm-study] 1주차

profile
하나를 배웠을 때 하나를 알면 잘하는 것이다. 💡

0개의 댓글