반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- 토익 환급
- PrivateRouter
- 파이썬
- Kriss 재택
- 코드잇
- IT 자격증
- IT자격증
- 색연필
- 프로그래밍
- 소묘
- leetcode
- 클린 코드
- Python
- KSTQB
- 테스팅 자격증
- 코딩
- 연필소묘
- csts
- 미켈란젤로
- 웹개발
- 취미
- 다비드상
- 알고리즘
- clean code
- 그림
- 재택근무
- react
- 코딩테스트
- 연필
Archives
- Today
- Total
글모음
[프로그래머스] 숫자 문자열과 영단어 [level1, Python] 본문
728x90
반응형
[코드 1] regex 정규 표현식 사용
import re
def solution(s):
num_dic = {'zero' : '0', 'one' : '1', 'two' : '2','three' : '3', 'four' : '4','five' : '5', 'six' : '6', 'seven' : '7', 'eight' : '8', 'nine' : '9'}
ans = ''
option = re.compile("zero|one|two|three|four|five|six|seven|eight|nine|\d")
nums = option.findall(s)
for n in nums:
try :
n = num_dic[n]
ans += n
except :
ans += n
return int(ans)
특정 문자들을 찾아낸다는 생각에 꽂혀서 정규 표현식만 생각을 했다.
더 간단한 replace 함수는 생각도 못했음 ㅋㅋ
정규 표현식은 계속 헷갈려서 날 잡아서 정리해야겠다.
[코드 2] 정규 표현식 사용 X -> 리스트, replace 함수 이용
def solution(s):
nums = ['zero','one','two','three','four','five','six','seven','eight','nine']
for i in range(len(nums)):
s = s.replace(nums[i],str(i))
return int(s)
[코드3] 정규 표현식 사용 X -> dictionary, replace 함수 이용
def solution(s):
num_dic = {'zero' : '0', 'one' : '1', 'two' : '2','three' : '3', 'four' : '4','five' : '5', 'six' : '6', 'seven' : '7', 'eight' : '8', 'nine' : '9'}
for key, value in num_dic.items():
s = s.replace(key,value)
return int(s)
728x90
반응형