Largest Number Groupings in String in Python


The challenge

You are be given a string that has lowercase letters and numbers. Your task is to compare the number groupings and return the largest number. Numbers will not have leading zeros.

For example, solve("gh12cdy695m1") = 695, because this is the largest of all number groupings.

The solution in Python code

Option 1:

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

Option 2:

def solve(s):
    return max(map(int,"".join(" " if x.isalpha() else x for x in s).split()))

Option 3:

def solve(s):
    i, maxn, L = 0, 0, len(s)
    numStart = False
    while i < L:
        if s[i].isdigit():
            j = i+1
            while 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

Test cases to validate our solution

test.it("Basic tests")
test.assert_equals(solve('gh12cdy695m1'),695)
test.assert_equals(solve('2ti9iei7qhr5'),9)
test.assert_equals(solve('vih61w8oohj5'),61)
test.assert_equals(solve('f7g42g16hcu5'),42)
test.assert_equals(solve('lu1j8qbbb85'),85)