Software Engineering

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
            i += 1
    return maxn

Test cases to validate our solution"Basic tests")