π‘ Problem Formulation: The task is to write a Python program that can take a string input and selectively capitalize the characters that appear more than once. For example, given the input ‘programming’, the desired output is ‘pRogRamming’, with the repeated characters ‘R’ and ‘m’ capitalized.
Method 1: Using a Dictionary
This method involves iterating through each character of the string and using a dictionary to store the occurrence count. Then, we rebuild the string by capitalizing characters that have an occurrence count of more than one.
Here’s an example:
def capitalize_repeated(s): char_count = {} for char in s: char_count[char] = char_count.get(char, 0) + 1 return "".join(char.upper() if char_count[char] > 1 else char for char in s) print(capitalize_repeated('programming'))
Output: 'pRogRamming'
This snippet defines a function capitalize_repeated
that takes a string and returns a new string with all the repeated characters capitalized. It uses dictionary comprehension to count the occurrences of each character and a generator expression to reconstruct the string with the necessary capitalizations.
Method 2: Iterating with Count Function
In this method, the count()
function of strings is used to find the occurrence of each character. Characters are capitalized if their count is greater than one during the string iteration.
Here’s an example:
def capitalize_repeated(s): return "".join(char.upper() if s.count(char) > 1 else char for char in s) print(capitalize_repeated('balloon'))
Output: 'baLLoon'
This function capitalize_repeated
utilizes the built-in count()
method to check the frequency of each character. This concise one-liner transforms the input string into the desired output with a simple generator expression.
Method 3: Using Collections Counter
Python’s Collections module provides a Counter class specifically for counting hashable objects. It can be used to count occurrences of characters in a string efficiently and then capitalize the characters accordingly.
Here’s an example:
from collections import Counter def capitalize_repeated(s): counter = Counter(s) return "".join(char.upper() if counter[char] > 1 else char for char in s) print(capitalize_repeated('mississippi'))
Output: 'mIssIssIppI'
After importing the Counter
class from collections
, the capitalize_repeated
function creates a Counter
object for the string, then builds the output string with capitalized characters for those that have counts over one.
Method 4: Using Regular Expressions
Regular Expressions (regex) can be used to detect repeated characters in a string. With a regex pattern, we can search for occurrences and replace them with their uppercase counterpart.
Here’s an example:
import re def capitalize_repeated(s): return re.sub(r'(\w)(?=.*\1)', lambda match: match.group(1).upper(), s) print(capitalize_repeated('abracadabra'))
Output: 'ABrAcAdABrA'
The capitalize_repeated
function uses the sub()
method from the re
module. The regex pattern finds duplicate characters and the lambda function in the replacement part capitalizes them, handling the capitalization in one pass through the string.
Bonus One-Liner Method 5: Using set and join
For a simple string without regard for order, a combination of set, list comprehension, and string join can be used. This method is less efficient but provides a clear one-liner solution.
Here’s an example:
s = 'conversation' print(''.join([char.upper() if s.count(char) > 1 else char for char in set(s)]))
Output: 'COnVERSAIT'
Here, set(s)
creates a set of unique characters, the list comprehension checks for occurrences of each character in the original string, and join
concatenates the characters back to form a string. Note that the order of characters in the resulting string may not match the original.
Summary/Discussion
- Method 1: Using a Dictionary. Very efficient and preserves order. Complexity can be a downside for newcomers.
- Method 2: Iterating with Count Function. Straightforward and readable, but less efficient due to repeated calls to
count()
. - Method 3: Using Collections Counter. Offers good performance and is clean, but requires importing an additional module.
- Method 4: Using Regular Expressions. Powerful and can be efficient for complex patterns, but may be overkill for simple tasks and is harder to read for those less familiar with regex.
- Bonus One-Liner Method 5: Using set and join. Provides a quick one-liner but does not preserve the order of characters, which could be a significant limitation.