일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 큐
- 카카오 기출
- graph
- Greedy
- Algorithm
- DFS
- 문자열
- Code Refactoring
- DP
- 정렬
- Queue
- 알고리즘
- DVWA
- heap
- Brute Force
- programmers
- 완전탐색
- 동적계획법
- Dynamic Programming
- BFS
- 코딩테스트
- 힙
- binary search
- 그래프
- 프로그래머스
- 백준
- 탐욕법
- django
- string
- sort
Archives
- Today
- Total
생각과 고민이 담긴 코드
프로그래머스 - 숫자 문자열과 영단어 (2021 카카오 채용연계 인턴십) / Level 1 본문
문제 : https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
풀이
def solution(s):
nums = [str(i) for i in range(10)]
en_nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
word = []
s = list(s)
i = 0
while True:
if "".join(word) in en_nums:
s.insert(i, str(en_nums.index("".join(word))))
word = []
i += 1
elif s[i] not in nums:
word.append(s[i])
s.remove(s[i])
else:
i += 1
if i >= len(s):
if word:
s.insert(i, str(en_nums.index("".join(word))))
break
else:
break
return int("".join(s))
쉬운 문제였다. 영어단어로 된 문자열을 해당하는 숫자로 바꿔주는 문제이다.
다만 리스트를 순회하면서 삽입, 삭제를 하기 때문에 index를 세심하게 체크해야 했다.
+ 다른 사람들의 풀이를 보니 replace 함수를 사용하여 더 쉽게 풀 수 있었다.
built-in 함수들을 더 숙달시켜야겠다.
'Algorithm > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 거리두기 확인하기 (2021 카카오 채용연계 인턴십) / Level 2 (0) | 2022.01.03 |
---|---|
프로그래머스 - 신규 아이디 추천 (2021 KAKAO 블라인드 채용) / Level 1 (0) | 2021.12.12 |
프로그래머스 - 로또의 최고 순위와 최저 순위 (2021 Dev-Matching 웹백엔드) / Level 1 (0) | 2021.12.04 |
프로그래머스 - 기지국 설치 (Summer / Winter Coding ~ 2018) / Level 3 (0) | 2021.11.19 |
프로그래머스 - 스킬트리 (Summer / Winter Coding ~2018) / Level 2 (0) | 2021.11.18 |