Thursday, February 29, 2024
HomeSoftware EngineeringReturn Brief Lengthy Brief in Python

Return Brief Lengthy Brief in Python

The problem

Given 2 strings, a and b, return a string of the shape brief+lengthy+brief, with the shorter string on the surface and the longer string on the within. The strings is not going to be the identical size, however they might be empty ( size “ ).

For instance:

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

The answer in Python code

Possibility 1:

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

Possibility 2:

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

Possibility 3:

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

Check instances to validate our resolution

import take a look at
from resolution import resolution

@take a look at.describe("Mounted Assessments")
def fixed_tests():
    @take a look'Primary Check Instances')
    def basic_test_cases():
        take a look at.assert_equals(resolution('45', '1'), '1451')
        take a look at.assert_equals(resolution('13', '200'), '1320013')
        take a look at.assert_equals(resolution('Quickly', 'Me'), 'MeSoonMe')
        take a look at.assert_equals(resolution('U', 'False'), 'UFalseU')


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments