Description:

Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore (’_’).

solution('abc') # should return ['ab', 'c_']
solution('abcdef') # should return ['ab', 'cd', 'ef']

Solution:

    # py:3.4
    def solution(letters):
    
    split_letters = [ letters[index-1] + letters[index] \
    for index in range(len(letters)) if index % 2 ] 
        
    if  len(letters) % 2:
        split_letters.append(letters[-1] + '_')
    
    return split_letters

Source:

CodeWars

January 1, 2016


Previous post
Python Classes by Example ###Subclassing
Next post
Asymptotic notation Khan Academy has a great explanation on the need of Asymptotic notation. Here is the best part. When we drop the constant coefficients and the less