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!")
'프로그래밍 공부' 카테고리의 다른 글
13. Python, CheckiO Elementary_Nearest Value (0) | 2020.10.22 |
---|---|
12. Python, CheckiO Elementary_Beginning Zeros (0) | 2020.10.22 |
10. Python, CheckiO Elementary_Max Digit (0) | 2020.10.22 |
9. Python, CheckiO Elementary_Replace First (0) | 2020.10.22 |
8. Python, CheckiO Elementary_All Upper I (0) | 2020.10.22 |
댓글