π‘ Problem Formulation: In Python, we may encounter the task of determining if a given number contains a pair of identical digits alongside any number of digit triplets. For instance, with the input 11533221
, the desired output would be True because the digit pair 11
and the digit triplet 222
are present.
Method 1: Using Default Dictionary
Implementing a digit pair and triplet checker in Python using a Default Dictionary tracks the frequency of each digit. The function checks for at least one pair and any number of triplets.
Here’s an example:
from collections import defaultdict def check_pairs_triplets(number): digit_count = defaultdict(int) for digit in str(number): digit_count[digit] += 1 pair_found = any(count == 2 for count in digit_count.values()) triplet_or_more_found = any(count >= 3 for count in digit_count.values()) return pair_found and triplet_or_more_found print(check_pairs_triplets(11533221))
Output: True
This function converts the number to a string, iterates through each digit, counting occurrences with a default dictionary. It then checks for at least one occurrence of a digit pair and any number of triplets or more using Python’s any()
function.
Method 2: Using Counter from Collections
Python’s Counter
object from the collections
module can efficiently count digit occurrences and verify the presence of pairs and triplets.
Here’s an example:
from collections import Counter def has_pair_and_triplets(number): counts = Counter(str(number)) return 2 in counts.values() and any(count >= 3 for count in counts.values()) print(has_pair_and_triplets(11533221))
Output: True
The has_pair_and_triplets
function creates a Counter
object for the digits and uses it to confirm the existence of at least one pair (count of 2) and one or more triplets (count of 3 or more).
Method 3: Regular Expressions
Regular expressions can be an efficient solution for matching patterns, such as finding a digit pair followed by any number of triplets in a string representation of a number.
Here’s an example:
import re def matches_pair_and_triplets(number): string_number = str(number) pair_match = re.search(r'(\\d)\\1', string_number) triplet_matches = len(re.findall(r'(\\d)\\1{2,}', string_number)) return bool(pair_match) and triplet_matches >= 1 print(matches_pair_and_triplets(11533221))
Output: True
This method uses Python’s regular expression module to check for adjacent identical digits forming pairs and triplets within the number’s string representation. It considers patterns where a digit (\\d
) is followed by the same digit (\\1
).
Method 4: Iterative Approach
An iterative method to count digit occurrences without additional modules can also determine the presence of pairs and triplets in a number.
Here’s an example:
def check_digits_iteratively(number): digit_count = {} for digit in str(number): if digit in digit_count: digit_count[digit] += 1 else: digit_count[digit] = 1 pair_found = 2 in digit_count.values() triplet_found = any(count >= 3 for count in digit_count.values()) return pair_found and triplet_found print(check_digits_iteratively(11533221))
Output: True
By iterating through each digit in the number string, this function maintains a count of each digit’s occurrences. It then checks for the presence of a pair and triplet directly within the counts.
Bonus One-Liner Method 5: Functional Approach
A one-liner functional approach using Python’s filter and map functions for the tech-savvy who prefer concise code.
Here’s an example:
print((lambda n: any(map(lambda x: x == 2, n.values())) and any(map(lambda x: x >= 3, n.values())))(__import__('collections').Counter(str(11533221))))
Output: True
This one-liner uses a lambda function to instantiate a Counter for the digits, then applies the map
and any
functions to check for the pair and triplets conditions in a functional programming style.
Summary/Discussion
- Method 1: Using Default Dictionary. Advantages: Readable and straightforward. Disadvantages: Requires additional import from
collections
. - Method 2: Using Counter from Collections. Advantages: Clean and efficient. Disadvantages: Less transparent for beginners.
- Method 3: Regular Expressions. Advantages: Powerful pattern matching. Disadvantages: Can be complex and less efficient for larger numbers.
- Method 4: Iterative Approach. Advantages: Simple, no imports necessary. Disadvantages: Potentially less performant with the increasing length of the number.
- Method 5: Functional Approach. Advantages: Compact one-liner. Disadvantages: Less readable, especially for those not familiar with functional programming paradigms.