Python program to find maximum uppercase run?


It is frequently helpful to find and extract patterns or segments of interest while working with strings. Finding the longest string of uppercase letters in each string is a typical problem. Numerous situations, including text processing, data analysis, and even text validation, present this issue. We will investigate two methods—the iterative method and the regular expression method—to solve this issue. These methods offer various methods for locating and extracting the longest uninterrupted sequence of capital letters from a string. We may effectively complete this assignment and learn how to handle related pattern extraction issues in Python by comprehending and putting these strategies into practice.

Approaches

To search maximum uppercase run-in Python, we can follow the two methods −

  • Utilizing the iterative method.

  • Utilizing the regular expression.

Let u s investigate both approaches −

Approach-1: Utilizing the iterative method.

The input string is scanned character by character using an iterative technique. To keep track of the current run of uppercase letters and their length, it keeps two variables: current_run and current_run_length. When a non-letter character or a lowercase letter is detected, it compares the current run length to the longest run length that has been discovered thus far. It updates the maximum run and its length if the current run length is longer. It then returns the longest possible run along with its length.

With an example input string, the code calls the find_max_uppercase_run_iterative function and outputs the input string, the maximum uppercase run, and its length.

Algorithm

The steps to find the maximum uppercase run in Python are as follows −

Step 1 − Initialize the maximum run length as 0 and the current run length as 0.

Step 2 − Iterate through each character in the input string.

Step 3 − If the character is an uppercase letter append it to the current run. Increment the current run length. If the character is not an uppercase letter, then compare the current run length with the maximum run length.

Step 4 − If the current run length is greater, update the maximum run length. Reset the current run and the current run length to empty values.

Step 5 − Check if the current run length is greater than the maximum run length. If true, update the maximum run length.

Step 6 − Return the maximum run length.

Example

def find_max_uppercase_run_iterative(string):
   maxRunLength = 0
   currentRunLength = 0
   maxRun = ""
   currentRun = ""

   for char in string:
      if char.isupper():
         currentRun += char
         currentRunLength += 1
      else:
         if currentRunLength > maxRunLength:
            maxRunLength = currentRunLength
            maxRun = currentRun
         currentRun = ""
         currentRunLength = 0

   if currentRunLength > maxRunLength:
      maxRunLength = currentRunLength
      maxRun = currentRun

   return maxRun, maxRunLength


input_string = "PrograMMINGisFUN"
maxRun, maxRunLength = find_max_uppercase_run_iterative(input_string)

print("Input String:", input_string)
print("Maximum Uppercase Run:", maxRun)
print("Maximum Uppercase Run Length:", maxRunLength)

Output

Input String: PrograMMINGisFUN
Maximum Uppercase Run: MMING
Maximum Uppercase Run Length: 5

Approach-2: Utilizing the regular expression

The input string is searched for all instances of consecutive runs of capital characters using the regular expression technique. To extract all uppercase letter sequences, it combines the regular expression pattern [A-Z]+ with the re.findall() function. The match list is a record of the matches. Finding the match with the longest length using the max () function yields the maximum run. The maximum run is then returned along with its length.

With an example input string, the code executes the find_max_uppercase_run_regex function and outputs the input string, the maximum uppercase run, and its length.

Algorithm

The steps to find the maximum uppercase run in Python are as follows −

Step 1 − import the module for regular expressions.

Step 2 − In order to match one or more consecutive capital letters, define a regular expression pattern.

Step 3 − To discover every instance of the pattern in the input string that matches, use the regular expression findall function.

Step 4 − If there are any matches then find the match that is the longest possible. Set the maximum run setting. The maximum run should be set to an empty string if no matches are discovered.

Step 5 − Determine the maximum run length.

Step 6 − Return the length as well as the maximum run.

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)

Output

Input String: PrograMMINGisFUN
Maximum Uppercase Run: MMING
Maximum Uppercase Run Length: 5

Conclusion

We looked at the iterative method and the regular expression method to determine the largest uppercase run-in a given text. The regular expression technique makes use of pattern matching, whereas the iterative method iterates through the string character by character. The option relies on requirements and preferences, although both strategies offer effective answers. Knowing these techniques gives us adaptable tools for removing and examining uppercase runs in strings.

Updated on: 28-Jul-2023

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements