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
Check if lowercase and uppercase characters are in same order in Python
Suppose we have a string with only lowercase or uppercase letters. We need to check whether both lowercase and uppercase letters follow the same order respectively. This means if we extract all lowercase letters and all uppercase letters separately, converting the lowercase sequence to uppercase should match the uppercase sequence.
So, if the input is like s = "piPpIePE", then the output will be True, as the lowercase letters "piepe" when converted to uppercase become "PIEPE", which matches the extracted uppercase sequence "PIEPE".
Algorithm
To solve this, we follow these steps ?
- Initialize empty strings for lowercase and uppercase letters
- Iterate through each character in the string
- If character is uppercase, append to uppercase string
- If character is lowercase, append to lowercase string
- Convert lowercase string to uppercase and compare with uppercase string
Method 1: Using ASCII Values
We can check if a character is uppercase by comparing its ASCII value ?
def solve(s):
lowercase = ""
uppercase = ""
for i in range(len(s)):
if ord(s[i]) >= 65 and ord(s[i]) <= 90:
uppercase += s[i]
else:
lowercase += s[i]
to_upper = lowercase.upper()
return to_upper == uppercase
s = "piPpIePE"
print(solve(s))
True
Method 2: Using Built-in Methods
A cleaner approach using Python's built-in string methods ?
def solve(s):
lowercase = ""
uppercase = ""
for char in s:
if char.isupper():
uppercase += char
else:
lowercase += char
return lowercase.upper() == uppercase
s = "piPpIePE"
print(solve(s))
True
Method 3: Using List Comprehension
A more concise solution using list comprehensions ?
def solve(s):
lowercase = ''.join([char for char in s if char.islower()])
uppercase = ''.join([char for char in s if char.isupper()])
return lowercase.upper() == uppercase
# Test with multiple examples
test_cases = ["piPpIePE", "aAbBcC", "aBcDeF", "abcDEF"]
for test in test_cases:
result = solve(test)
print(f"'{test}' ? {result}")
'piPpIePE' ? True 'aAbBcC' ? True 'aBcDeF' ? False 'abcDEF' ? False
How It Works
Let's trace through the example "piPpIePE" ?
- Extract lowercase letters: "p", "i", "p", "e" ? "pipe"
- Extract uppercase letters: "P", "I", "P", "E" ? "PIPE"
- Convert lowercase to uppercase: "pipe".upper() ? "PIPE"
- Compare: "PIPE" == "PIPE" ? True
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| ASCII Values | Medium | Fast | Understanding ASCII |
| Built-in Methods | High | Fast | Clean, readable code |
| List Comprehension | High | Fast | Concise solutions |
Conclusion
Use the built-in method approach for the best balance of readability and performance. The algorithm extracts lowercase and uppercase sequences separately, then compares them after case conversion.
---