[나의 풀이]
def solution(s):
if len(s) == 4 or len(s) == 6:
for i in range(len(s)):
# isalpha함수는 문자열이 문자인지 아닌지를 True,False로 리턴해주고,
# isdigit함수는 문자열이 숫자인지 아닌지를 True,False로 리턴해줍니다.
if not s[i].isdigit():
return False
return True
else:
return False
[다른 풀이]
def alpha_string46(s):
return s.isdigit() and len(s) in (4, 6)
def alpha_string46(s):
import re
return bool(re.match("^(\d{4}|\d{6})$", s))
def alpha_string46(s):
try:
int(s)
except:
return False
return len(s) == 4 or len(s) == 6
[isdigit의 예시]
'workSpace > ALGORITHM' 카테고리의 다른 글
[Python] 자연수 뒤집어 배열로 만들기 (0) | 2020.12.20 |
---|---|
[Python][sort][sorted] 정수 내림차순으로 배치하기 (0) | 2020.12.20 |
[Python] 정수 제곱근 판별 (0) | 2020.12.20 |
[Python] 최대공약수와 최소공배수 (0) | 2020.12.20 |
[Python] 자릿수 더하기 (0) | 2020.12.20 |