Write a function called `match` in Python that uses a regex to find any word repeated 5 times or more. Return that word. If no word is repeated 5 times or more, return an empty string.
Write a function called `match` in Python that uses a regex to find any word repeated 5 times or more. Return that word. If no word is repeated 5 times or more, return an empty string.
To solve this problem, we need to write a function called `match` that uses a regular expression (regex) to identify any word that is repeated five times or more in a given text. The function should return the first such word found, or an empty string if no such word exists. ### Approach 1. **Regex Pattern Construction**: The regex pattern is designed to capture a word and then check if it appears at least four more times in the remaining text. This is achieved using a positive lookahead assertion in the regex. 2. **Word Boundaries**: The regex uses word boundaries (`\b`) to ensure that we match whole words and not parts of other words. 3. **Case Sensitivity**: The regex is case-sensitive, meaning "Word" and "word" are considered different words. 4. **Efficiency**: The regex uses non-greedy quantifiers to efficiently find subsequent occurrences of the word. ### Solution Code
1 2 3 4 5 6 | import re def match(text): pattern = r'\b(\w+)\b(?=(?:.*?\b\1\b){4})' match = re.search(pattern, text) return match.group(1) if match else '' |
I extracted the following code from that output:
1 2 3 4 5 6 | import re def match(text): pattern = r'\b(\w+)\b(?=(?:.*?\b\1\b){4})' match = re.search(pattern, text) return match.group(1) if match else '' |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import re def match(text): pattern = r'\b(\w+)\b(?=(?:.*?\b\1\b){4})' match = re.search(pattern, text) return match.group(1) if match else '' answer = match('hi hi hi hi hi') expected = "hi" assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = match('hi hi hi hi') expected = "" assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = match('hello this is is is is is a test') expected = "is" assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True