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

**11. Python, CheckiO Elementary_Split Pairs

by 응_비 2020. 10. 22.

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_']

1

2

Precondition: 0<=len(str)<=100

 

-> Solve it

 

def split_pairs(a):  
    return split_pairs[0,1]+split_pairs[2,3]...?

 

  for t in split_pairs(a):

    result = split_pairs[t, t+1]...?

 

def split_pairs(a):
    # your code here
    new_list = []
    while a:
        if len(a) > 1:
            new_list.append(a[:2])
            a = a[2:]
        else:
            new_list.append(a[:] + '_')
            break

    return new_list




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

댓글