jeeyeonLIM / coding_test

Let's practice the coding test!
1 stars 0 forks source link

Level1. 문자열 다루기 기본 #19

Open jeeyeonLIM opened 4 years ago

jeeyeonLIM commented 4 years ago

문제 설명

문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 a234이면 False를 리턴하고 1234라면 True를 리턴하면 됩니다.

제한 사항

s는 길이 1 이상, 길이 8 이하인 문자열입니다. 입출력 예 s return
a234 false
1234 true

출처

https://programmers.co.kr/learn/courses/30/lessons/12918#

jeeyeonLIM commented 4 years ago

코드

def solution(s):
    answer = True
    if s.isdigit() and (len(s) == 4 or len(s) ==6):
        answer = True
    else : 
        answer = False
    return answer
jeeyeonLIM commented 4 years ago

함수 정리

'문자열'.swapcase
'문자열'.isdigit
'문자열'.isalnum
jeeyeonLIM commented 3 years ago

다시풀기

False인 경우만 골라내고, 그렇지 않으면 True 반환

def solution(s):

문자열 길이부터 검사

if len(s) in (4,6):
    # 모두 숫자로 구성되어 있는 경우에만 True
    if s.isdigit():
        return True
    else: return False
else:
    return False