Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find maximum uppercase run?
Finding the longest consecutive sequence of uppercase letters in a string is a common text processing task. This problem appears in data analysis, text validation, and pattern extraction scenarios. We'll explore two efficient approaches: the iterative method and regular expressions.
Approaches
To find the maximum uppercase run in Python, we can use two methods:
Using the iterative method
Using regular expressions
Let's examine both approaches in detail.
Method 1: Using Iterative Approach
The iterative method scans the string character by character, tracking the current run of uppercase letters and comparing it with the maximum run found so far.
Algorithm
The steps to find the maximum uppercase run are:
Step 1 ? Initialize maximum run length as 0 and current run length as 0.
Step 2 ? Iterate through each character in the input string.
Step 3 ? If the character is uppercase, append it to the current run and increment the length.
Step 4 ? If the character is not uppercase, compare current run with maximum run and update if longer.
Step 5 ? Reset current run variables and continue.
Step 6 ? Return the maximum run and its length.
Example
def find_max_uppercase_run_iterative(string):
max_run_length = 0
current_run_length = 0
max_run = ""
current_run = ""
for char in string:
if char.isupper():
current_run += char
current_run_length += 1
else:
if current_run_length > max_run_length:
max_run_length = current_run_length
max_run = current_run
current_run = ""
current_run_length = 0
# Check final run in case string ends with uppercase
if current_run_length > max_run_length:
max_run_length = current_run_length
max_run = current_run
return max_run, max_run_length
input_string = "PrograMMINGisFUN"
max_run, max_run_length = find_max_uppercase_run_iterative(input_string)
print("Input String:", input_string)
print("Maximum Uppercase Run:", max_run)
print("Maximum Uppercase Run Length:", max_run_length)
The output of the above code is ?
Input String: PrograMMINGisFUN Maximum Uppercase Run: MMING Maximum Uppercase Run Length: 5
Method 2: Using Regular Expressions
The regular expression approach uses pattern matching to find all consecutive uppercase sequences, then selects the longest one using the max() function.
Algorithm
The steps using regular expressions are:
Step 1 ? Import the regular expression module.
Step 2 ? Define pattern [A-Z]+ to match consecutive uppercase letters.
Step 3 ? Use re.findall() to find all matches in the input string.
Step 4 ? Find the longest match using max() with key=len.
Step 5 ? Return the maximum run and its length.
Example
import re
def find_max_uppercase_run_regex(string):
matches = re.findall(r'[A-Z]+', string)
max_run = max(matches, key=len) if matches else ""
max_run_length = len(max_run)
return max_run, max_run_length
input_string = "PrograMMINGisFUN"
max_run, max_run_length = find_max_uppercase_run_regex(input_string)
print("Input String:", input_string)
print("Maximum Uppercase Run:", max_run)
print("Maximum Uppercase Run Length:", max_run_length)
The output of the above code is ?
Input String: PrograMMINGisFUN Maximum Uppercase Run: MMING Maximum Uppercase Run Length: 5
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Iterative | O(n) | O(1) | Memory efficiency |
| Regular Expression | O(n) | O(k) | Concise code |
Conclusion
Both methods effectively find the maximum uppercase run in a string. The iterative approach offers better memory efficiency, while regular expressions provide more concise and readable code. Choose based on your specific requirements for performance versus code simplicity.
