본문 바로가기

Python_Matter/[Check_IO]Elementary

Split Pairs

반응형

Quiz>

Split the string into pairs of two characters. 

If the string contains an odd number of characters, 

then the missing second character of the final pair should be replaced with an underscore ('_').

 

Input: 

A string.

 

Output: 

An iterable of strings.

 

Example:

split_pairs('abcd') == ['ab', 'cd']
split_pairs('abc') == ['ab', 'c_']

 

Precondition: 

0<=len(str)<=100

 

def split_pairs(a):
    # your code here
    return None


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

 

Solve Case 1>

1. itertools, operator 모듈 사용하여 구할 수 있다.

    itertools : 반복자 생성 모듈

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

import itertools
import operator

 

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

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

 

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

    operator.add는 리스트를 이어서 반환해준다.

def split_pairs(a):
    connection_character = itertools.chain(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를 2번째까지 슬라이싱해서 반환한다.

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

 

Code>

import itertools
import operator

def split_pairs(a):
    connection_character = itertools.chain(a, '_')
    return map(operator.add, connection_character, connection_character)
    
    
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!")
    
    
if __name__ == '__main__':
    print("Example:")
    print(list(split_pairs2('abcd')))

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

 

Result Case 1>

Example:

['ab', 'cd']

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

 

Result Case 2>

Example:

['ab', 'cd']

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

 

반응형

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

Nearest Value  (0) 2020.04.11
Beginning Zeros  (0) 2020.04.11
Max Digit  (0) 2020.04.11
Replace First  (0) 2020.04.11
Remove All Before  (0) 2020.04.11