Tuesday, November 28, 2023
HomeSoftware EngineeringLargest Quantity Groupings in String in Python

Largest Quantity Groupings in String in Python

The problem

You might be be given a string that has lowercase letters and numbers. Your activity is to check the quantity groupings and return the biggest quantity. Numbers won’t have main zeros.

For instance, clear up("gh12cdy695m1") = 695, as a result of that is the biggest of all quantity groupings.

The answer in Python code

Choice 1:

import re
def clear up(s):
    return max(map(int,re.findall(r"(d+)", s)))

Choice 2:

def clear up(s):
    return max(map(int,"".be a part of(" " if x.isalpha() else x for x in s).cut up()))

Choice 3:

def clear up(s):
    i, maxn, L = 0, 0, len(s)
    numStart = False
    whereas i < L:
        if s[i].isdigit():
            j = i+1
            whereas j<L and s[j].isdigit():
                j += 1
            if int(s[i:j]) > maxn:
                maxn = int(s[i:j])
            i = j+1
            i += 1
    return maxn

Take a look at instances to validate our resolution

take a look at.it("Primary checks")
take a look at.assert_equals(clear up('gh12cdy695m1'),695)
take a look at.assert_equals(clear up('2ti9iei7qhr5'),9)
take a look at.assert_equals(clear up('vih61w8oohj5'),61)
take a look at.assert_equals(clear up('f7g42g16hcu5'),42)
take a look at.assert_equals(clear up('lu1j8qbbb85'),85)


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments