Notice
Recent Posts
Link
- Today
- Total
hye-log
[프로그래머스]숫자 문자열과 영단어(Python) 본문
0. 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/81301?language=python3
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 설명
1) 주어진 문자열에서 숫자는 숫자로
2) 문자는 문자에 해당하는 숫자로 반환
2. 입출력
# input
s = 'one4seveneight'
# output
result = 1478
3. 코드
1) 첫 번째 코드
def solution(s):
# s : 문자열 배열
answer = []
dict_list = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] # index - 숫자 변환 리스트
stack = [] # 한 글자씩 확인
for i in range(len(s)):
c = s[i] # 문자열에서 한 글자 가져오기
answer.append(c) if c.isdigit() else stack.append(c) # 숫자면 정답에 추가, 문자면 스택에 추가
stack_join = ''.join(stack) # 스택 단어 합치기
if stack_join in dict_list: # dict_list에 stack_join이 존재하면
answer.append(str(dict_list.index(stack_join))) # index(숫자)로 정답에 추가
stack = [] # 스택 비우기
return int(''.join(answer))
실행 결과
4. 알게된 점
1) join 함수를 이용하여 문자열 합치기(반환은 문자열로)
# 문자열 합치기
list = ['a', '1', 'b', '2']
''.join(list) # 'a1b2'
5. Github 링크
https://github.com/iihye/Algorithm/blob/main/Programmers/number_word.py
GitHub - iihye/Algorithm
Contribute to iihye/Algorithm development by creating an account on GitHub.
github.com
728x90
'CodingTest > Programmers' 카테고리의 다른 글
[프로그래머스]없는 숫자 더하기(Python) (0) | 2022.07.19 |
---|---|
[프로그래머스]키패드 누르기(Python) (0) | 2022.07.18 |
[프로그래머스]신규 아이디 추천(Python) (0) | 2022.07.12 |
[프로그래머스]로또의 최고 순위와 최저 순위(Python) (0) | 2022.07.08 |
[프로그래머스]신고 결과 받기(Python) (0) | 2022.07.06 |
Comments