Maximum Upper case run in a list of strings

Question:

How to write a Python program to find the largest substring of uppercase characters and print the length of that substring?
Sample Input – I lovE PRogrAMMING
Sample Output – 6
Explanation – AMMING is the largest substring with all characters in uppercase continuously.

stuck with this for more than an hour and still no clue 🙁

created a list out of the string by using the split method and no clue on what’s next.

Asked By: Joe

||

Answers:

import re
s='I lovE PRogrAMMING '
upper_case_letters=re.findall(r"[A-Z]+", s)

print(upper_case_letters)
#['I', 'E', 'PR', 'AMMING']

print(max(upper_case_letters,key=len))
#output
'AMMING'

print(len(max(upper_case_letters,key=len)))
#6
Answered By: God Is One

You can use itertools.groupby for the task:

from itertools import groupby

s  ='I lovE PRogrAMMING'

out = max(sum(1 for _ in g) for k, g in groupby(s, str.isupper) if k)
print(out)

Prints:

6
Answered By: Andrej Kesely
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.