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.
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
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
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.
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
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