본문 바로가기
프로그래밍 공부

12. Python, CheckiO Elementary_Beginning Zeros

by 응_비 2020. 10. 22.

You have a string that consist only of digits. You need to find how many zero digits ("0") are at the beginning of the given string.

Input: A string, that consist of digits.

Output: An Int.

Example:

beginning_zeros('100') == 0

beginning_zeros('001') == 2

beginning_zeros('100100') == 0

beginning_zeros('001001') == 2

beginning_zeros('012345679') == 1

beginning_zeros('0000') == 4

1

2

3

4

5

6

Precondition: 0-9

 

-> Solve it

숫자로만 구성된 문자열이 있는 경우. 주어진 문자열의 시작 부분에서 0자리 수("0")를 찾아야 한다.

 

def beginning_zeros(number: str) -> int:
     return len(number) - len(number.lstrip('0'))


if __name__ == '__main__':
    print("Example:")
    print(beginning_zeros('100'))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert beginning_zeros('100') == 0
    assert beginning_zeros('001') == 2
    assert beginning_zeros('100100') == 0
    assert beginning_zeros('001001') == 2
    assert beginning_zeros('012345679') == 1
    assert beginning_zeros('0000') == 4
    print("Coding complete? Click 'Check' to earn cool rewards!")

댓글