DEV Community

Filipe Bezerra
Filipe Bezerra

Posted on

How did I solve Split Strings coding challenge

This is the first post of a series about online solving coding challenges and it's about how I solved the "Split Strings" coding challenge.

One of my daily activities is to practice and solve coding challenges and after a time doing it I decided to write about how I solve each of them.

This code challenge is hosted at codewars.com and here it's instructions:

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 ('_').

Examples:

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

When solving problems the first thing that I like to do is to divide and conquer, so I break down the whole problem in small problems and start from that.

My first solution was using a declarative programming model so that I can freely express my thoughts, here's the solution that I found:

def solution(s):
    list_of_pairs = []
    pair = ''
    for char in s:
        pair += char
        if len(pair) == 2:
            list_of_pairs.append(pair)
            pair = ''
    if pair:
        list_of_pairs.append(pair + '_')
    return list_of_pairs
Enter fullscreen mode Exit fullscreen mode

There's no complexity with this solution but it's a little verbose and totally declarative.

The first thing it was to declare the data structures that'll store the final result, the list of pairs and the string pair accumulator used inside the for loop.

Inside the for loop I increase the accumulator then checks if it's time to populate the list with the pair and reset the accumulator.

As soon as the for loop is completed, before returning the result, I need to check if there's something left in the accumulator and append it to the list, appending the underscore to the end of the pair following the challenge rules.

All tests passed.

This was a very straightforward and not optimized solution.

Next step was to refactor the solution and find a efficient solution writing in imperative way, here's the solution that I found:

def solution(s):
    return [s[i]+'_' if len(s[i:]) == 1 else s[i:i+2] 
            for i in range(0, len(s), 2)]
Enter fullscreen mode Exit fullscreen mode

The first thing it was to split up the string in a range using 2 steps between each item.

With that range I can iterate using a list comprehension so that I can extract what I need, pairs of 2 characters.

This second final solution was written in a imperative programming model and used advanced features of Python like list comprehension and slicing.

One thing that we should keep in mind, we can find another way to get the same result, perhaps more effiecient than this one, this is the beauty within software development, with that we can conclude this solution coud be improved. What do you think?

Top comments (0)