5 Best Ways to Check if Lowercase and Uppercase Characters Are in the Same Order in Python

Rate this post

πŸ’‘ Problem Formulation: Given a string, the challenge is to verify whether the order of uppercase characters is the same as the order of their lowercase counterparts. For example, if the input is "PyThon", the desired output is True since ‘P’ precedes ‘T’, as does ‘y’ before ‘h’ in both cases.

Method 1: Iterative Comparison

This method involves iterating over the characters of the string and comparing the relative order of the uppercase and lowercase letters. We maintain two pointers that scan the string for uppercase and lowercase letters, respectively, and compare their relative positions.

Here’s an example:

def is_same_order(s):
    uppercase = [c for c in s if c.isupper()]
    lowercase = [c for c in s if c.islower()]
    return uppercase == [c.upper() for c in lowercase]

print(is_same_order("PyThon"))

Output: True

This code snippet creates two separate lists of uppercase and lowercase letters and then compares the uppercase list with a list of the lowercase letters converted to uppercase. This checks if the characters are in the same order, regardless of their case.

Method 2: Using Regular Expressions

Regular expressions can be used to match patterns in strings. We can match all uppercase and lowercase letters separately and then compare their relative order.

Here’s an example:

import re

def is_same_order(s):
    uppercase = re.findall(r'[A-Z]', s)
    lowercase = re.findall(r'[a-z]', s)
    return uppercase == [c.upper() for c in lowercase]

print(is_same_order("PyThon"))

Output: True

This code utilizes regular expressions to find all uppercase and lowercase characters. Then, similar to the first method, it compares their relative order after converting the lowercase characters to uppercase.

Method 3: Zip and Comparison

The zip() function can be utilized to combine two lists and compare their elements in pairs. This method will zipped both uppercase and lowercase lists and compare their elements to ensure they match.

Here’s an example:

def is_same_order(s):
    uppercase = [c for c in s if c.isupper()]
    lowercase = [c for c in s if c.islower()]
    return all(u == l.upper() for u, l in zip(uppercase, lowercase))

print(is_same_order("PyThon"))

Output: True

The code above zips together the uppercase and converted lowercase character lists, checking that each corresponding pair of characters matches.

Method 4: Using ASCII Values

This method compares the ASCII values of uppercase and lowercase letters to determine if they are in the same order, considering upper and lower case letters have a constant difference in their ASCII values.

Here’s an example:

def is_same_order(s):
    uppercase, lowercase = [], []
    for c in s:
        if c.isupper():
            uppercase.append(ord(c))
        elif c.islower():
            lowercase.append(ord(c))
    return uppercase == [l + 32 for l in lowercase]

print(is_same_order("PyThon"))

Output: True

The ASCII value of uppercase characters is 32 less than their lowercase counterparts. This code snippet creates two lists of ASCII values for uppercase and lowercase characters, then compares them with the required adjustment.

Bonus One-Liner Method 5: Functional Approach

A one-liner utilizing a functional approach can be elegant and efficient. This method combines filter and map functions to quickly compare the order of uppercase and lowercase characters.

Here’s an example:

print((lambda s: list(filter(str.isupper, s)) == list(map(str.upper, filter(str.islower, s))))("PyThon"))

Output: True

This one-liner uses a lambda function to filter and map uppercase and lowercase characters and compare their lists directly within the print function.

Summary/Discussion

  • Method 1: Iterative Comparison. Straightforward and easy to understand. Can be less efficient for very long strings due to the list comprehension and the case conversion of lowercase characters.
  • Method 2: Using Regular Expressions. Good for pattern matching in strings. However, regular expressions may not be as readable or performant as other methods for simple tasks.
  • Method 3: Zip and Comparison. Pythonic and concise. But it requires understanding of zip() and list comprehensions, which might be complex for beginners.
  • Method 4: Using ASCII Values. Low-level control and interesting. Might not be as intuitive and requires knowledge of ASCII.
  • Method 5: Functional Approach. Compact and advanced. Excellent for one-liners but can be cryptic and harder to debug or understand for some developers.