Return Short Long Short in Python


The challenge

Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty ( length `` ).

For example:

solution("1", "22") # returns "1221"
solution("22", "1") # returns "1221"

The solution in Python code

Option 1:

def solution(a, b):
    if a.isdigit():
        if a<b:
            return f"{a}{b}{a}"
        else:
            return f"{b}{a}{b}"
    else:
        if len(a)<len(b):
            return f"{a}{b}{a}"
        else:
            return f"{b}{a}{b}"

Option 2:

def solution(a, b):
    return a+b+a if len(a)<len(b) else b+a+b

Option 3:

def solution(a, b):
    return '{0}{1}{0}'.format(*sorted((a, b), key=len))

Test cases to validate our solution

import test
from solution import solution

@test.describe("Fixed Tests")
def fixed_tests():
    @test.it('Basic Test Cases')
    def basic_test_cases():
        test.assert_equals(solution('45', '1'), '1451')
        test.assert_equals(solution('13', '200'), '1320013')
        test.assert_equals(solution('Soon', 'Me'), 'MeSoonMe')
        test.assert_equals(solution('U', 'False'), 'UFalseU')