Tuesday, April 23, 2024
HomeSoftware EngineeringDiscover the Most Size Distinction between Lists/Arrays in Python

Discover the Most Size Distinction between Lists/Arrays in Python


The problem

You’re given two arrays a1 and a2 of strings. Every string consists of letters from a to z. Let x be any string within the first array and y be any string within the second array.

Discover max(abs(size(x) − size(y)))

If a1 and/or a2 are empty return -1 in every language besides in Haskell (F#) the place you’ll return Nothing (None).

Examples:

a1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
a2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
mxdiflg(a1, a2) --> 13

The answer in Python

Choice 1:

def mxdiflg(a1, a2):
    if a1 and a2:
        return max(
            len(max(a1, key=len)) - len(min(a2, key=len)),
            len(max(a2, key=len)) - len(min(a1, key=len)))
    return -1

Choice 2:

def mxdiflg(a1, a2):
    if a1 and a2:
        return max(abs(len(x) - len(y)) for x in a1 for y in a2)
    return -1

Choice 3:

def mxdiflg(a1, a2):
    if not a1 or not a2: return -1
    
    max_a1 = max([len(x) for x in a1])
    min_a1 = min([len(x) for x in a1])
    
    max_a2 = max([len(x) for x in a2])
    min_a2 = min([len(x) for x in a2])
    
    return max(max_a1 - min_a2, max_a2 - min_a1)

Take a look at circumstances to validate our answer

check.describe("mxdiflg")
check.it("Fundamental exams")
s1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
s2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
check.assert_equals(mxdiflg(s1, s2), 13)
s1 = ["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"]
s2 = ["bbbaaayddqbbrrrv"]
check.assert_equals(mxdiflg(s1, s2), 10)
s1 = ["ccct", "tkkeeeyy", "ggiikffsszzoo", "nnngssddu", "rrllccqqqqwuuurdd", "kkbbddaakkk"]
s2 = ["tttxxxxxxgiiyyy", "ooorcvvj", "yzzzhhhfffaaavvvpp", "jjvvvqqllgaaannn", "tttooo", "qmmzzbhhbb"]
check.assert_equals(mxdiflg(s1, s2), 14) 
s1 = []
s2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
check.assert_equals(mxdiflg(s1, s2), -1) 
s2 = []
s1 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
check.assert_equals(mxdiflg(s1, s2), -1) 
s1 = []
s2 = []
check.assert_equals(mxdiflg(s1, s2), -1) 

from random import randint

def mxdiflgSol(a1, a2):
    mx = -1
    for x in a1:
        for y in a2:
            diff = abs(len(x) - len(y))
            if (diff > mx):
                mx = diff
    return mx

def do_ex(okay):
    a1, i = [], 0
    whereas (i < okay):
        res, j = "", 0
        whereas (j < randint(1, 20)):
            res += chr(randint(97, 122)) * randint(1, 3)
            j += 1
        a1.append(res)
        i += 1
    return a1

def randomTests():
    print("100 Random exams ****************** ")
    for _ in vary(0, 100):
        s1 = do_ex(randint(0, 10))
        s2 = do_ex(randint(0, 8))
        check.assert_equals(mxdiflg(s1, s2), mxdiflgSol(s1, s2))
        
randomTests()
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments