Saturday, September 30, 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 job is to match the quantity groupings and return the biggest quantity. Numbers is not going to 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 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
        else:
            i += 1
    return maxn

Check instances to validate our resolution

take a look at.it("Fundamental exams")
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)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments