본문 바로가기

Python_Matter/[Check_IO]Elementary

Number Length

반응형

Quiz>

You have a positive integer.

Try to find out how many digits it has?

 

Input: 

A positive Int

 

Output:

An Int.

 

Example:

number_length(10) == 2
number_length(0) == 1

 

def number_length(a: int) -> int:
    # your code here
    return None


if __name__ == '__main__':
    print("Example:")
    print(number_length(10))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert number_length(10) == 2
    assert number_length(0) == 1
    assert number_length(4) == 1
    assert number_length(44) == 2
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Solve>

1. 입력값을 문자열로 변환 후 다시 리스트화 시키면 한글자씩 나눌수 있다.

    이때 len을 통해 들어있는 원소 개수를 알수 있다.

def number_length(a: int):
    return len(list(str(a)))

 

Code>

def number_length(a: int):
    return len(list(str(a)))

 

Example>

if __name__ == '__main__':
    print("Example:")
    print(number_length(10))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert number_length(10) == 2
    assert number_length(0) == 1
    assert number_length(4) == 1
    assert number_length(44) == 2
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Result>

Example:

2

Coding complete? Click 'Check' to earn cool rewards!

반응형

'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글

Backward String  (0) 2020.04.11
End Zeros  (0) 2020.04.11
Acceptable Password I  (0) 2020.04.11
All Upper I  (1) 2020.04.11
Correct Sentence  (0) 2020.04.11