Cody Bable


generate functionFri, 19 May 2023

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 ('_'). def split_pairs(text: str) -> Iterable[str]:

def split_pairs(text: str) -> Iterable[str]: return [text[i:i+2] if text[i:i+2] else text[i] + '_' for i in range(0, len(text), 2)] split_pairs('abcd') # ['ab', 'cd'] split_pairs('abc') # ['ab', 'c_'] split_pairs('abcdf') # ['ab', 'cd', 'f_'] split_pairs('a') # ['a_'] split_pairs('') # []

Questions about programming?Chat with your personal AI assistant