honey_pot
[python] 숫자 문자열과 영단어 본문
코드
def solution(s):
word = ''
num = ['zero', 'one', 'two','three','four','five',
'six','seven','eight','nine']
res =[]
for i in s:
if i.isnumeric():
res.append(int(i))
else:
word += str(i)
if len(word) > 2 and word in num:
su = num.index(word)
res.append(int(su))
word=''
return int(''.join(str(i) for i in res))
출력부분에서 계속 null이 나오길래 뭐가 문제인가 했더니 return으로 써야하는데 print로 써서였다
딕셔너리로도 풀 수 있을 것 같은데 zero까지 주어졌으니 리스트로 만들어서 인덱스를 뽑아오는게 더 편할 것 같아서 리스트로 풀었다.
'문제 풀이' 카테고리의 다른 글
[python] 폰켓몬 (0) | 2021.09.08 |
---|---|
[python] 키패드 누르기 (0) | 2021.09.02 |
[python] 실패율 (0) | 2021.08.30 |
[python] 모의고사 (0) | 2021.08.30 |
[python] 나누어 떨어지는 숫자 배열 (0) | 2021.07.19 |
Comments