본문 바로가기

Python_Matter/[Check_IO]Home

Count Digits

반응형

Quiz>

You need to count the number of digits in a given string.

 

Input: A Str.

 

Output: An Int.

 

Example:

count_digits('hi') == 0

count_digits('who is 1st here') == 1

count_digits('my numbers is 2') == 1

count_digits('This picture is an oil on canvas '

 'painting by Danish artist Anna '

 'Petersen between 1845 and 1910 year') == 8

count_digits('5 plus 6 is') == 2

count_digits('') == 0

 

def count_digits(text: str) -> int:
    # your code here
    return 0


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!")

 

Solve Case1>

1. itertools, operator 모듈 사용

   itertools : 반복자 생성 모듈

   operator : 파이썬의 내장 연산자에 해당하는 효율적인 함수 집합 모듈

 

2. 모듈 임포트

import itertools
import operator

 

3. 리스트끼리 연결 하는 chain을 사용한다.

def split_pairs(a):
    connection_character = itertools.chain(a, '_')

 

4. 원본 리스트를 변경하지 않고 새 리스트를 생성하는 map을 사용하여 리스트를 이어서 반환 해준다.

def split_pairs(a):
    return map(operator.add, connection_character, connection_character)

 

Solve Case 2>

1. a의 문자의 갯수를 변수에 할당한다.

def split_pairs2(a):
    len_split_pairs = len(a)

 

2. 문자의 갯수가 0이면 빈 리스트를 반환한다.

def split_pairs2(a):
    if len_split_pairs == 0:
        return []

 

3. 문자의 갯수가 1개 이면 a와 _를 합쳐서 반환한다.

def split_pairs2(a):
    if len_split_pairs == 1:
        return [a + '_']

 

4. 문자의 갯수가 1개 이상이면 a를 슬라이싱해서 반환한다.

def split_pairs2(a):
    else:
        return [a[:2]] + split_pairs2(a[2:])

 

Code>

# Case1


def split_pairs(a):
    connection_character = itertools.chain(a, '_')
    return map(operator.add, connection_character, connection_character)

# Case2


def split_pairs2(a):
    len_split_pairs = len(a)
    if len_split_pairs == 0:
        return []
    if len_split_pairs == 1:
        return [a + '_']
    else:
        return [a[:2]] + split_pairs2(a[2:])

 

Example>

if __name__ == '__main__':
    print("Example:")
    print(list(split_pairs('abcd')))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert list(split_pairs('abcd')) == ['ab', 'cd']
    assert list(split_pairs('abc')) == ['ab', 'c_']
    assert list(split_pairs('abcdf')) == ['ab', 'cd', 'f_']
    assert list(split_pairs('a')) == ['a_']
    assert list(split_pairs('')) == []
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Result>

Example:

['ab', 'cd']

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

반응형

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

Backward Each Word  (0) 2020.04.15
Find Quotes  (0) 2020.04.15
All the Same  (0) 2020.04.15
Non-unique Elements  (0) 2020.04.15
Popular Words  (0) 2020.04.15