본문 바로가기
프로그래밍 문제/[Python] CheckIO

CheckIO - Count digits

by UltraLowTemp-Physics 2020. 12. 14.
728x90

Problem: py.checkio.org/en/mission/count-digits/

My Solution:

def count_digits(text: str) -> int:
    # your code here
    count = 0
    for n in list(text):
        if n.isdigit():
            count = count + 1
    return count


if __name__ == '__main__':
    print("Example:")
    print(count_digits('hi'))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert count_digits('hi') == 0
    assert count_digits('who is 1st here') == 1
    assert count_digits('my numbers is 2') == 1
    assert count_digits('This picture is an oil on canvas '
 'painting by Danish artist Anna '
 'Petersen between 1845 and 1910 year') == 8
    assert count_digits('5 plus 6 is') == 2
    assert count_digits('') == 0
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

A good solution that I see

def count_digits(text: str) -> int:
    return sum(c.isdigit() for c in text)

 

728x90

'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글

CheckIO - Between Markers  (0) 2020.12.21
CheckIO - Bigger price  (0) 2020.12.20
CheckIO - Days between  (0) 2020.12.12
CheckIO - first word  (0) 2020.12.11
CheckIO - three words  (0) 2020.12.11

댓글