Return Short Long Short in Python

0 min read 138 words

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')
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts