Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Split Pairs by Steven2222
from math import ceil
def split_pairs(a):
""" Breaks string in couples of chars if uneven number
last char coupled to underscore
In: str
Out: str """
# Determine length even/uneven and treat condition accordingly
if len(a)%2 == 0:
return [a[i-2:i] for i in range(2, len(a) +1, 2)]
else:
return [a[i-2:i] for i in range(2, len(a) +1, 2)] + [a[-1:] + '_']
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!")
April 22, 2021
Comments: