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.
**Function to Find Repeated Words** ### Description This function uses a regular expression to find any word repeated 5 times or more in a given string. It returns the first word that meets this condition, or an empty string if no such word exists. ### Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import re from collections import Counter def match(s): """ Find any word repeated 5 times or more in a string. Args: s (str): The input string. Returns: str: The first word repeated 5 times or more, or an empty string. """ # Split the string into words words = re.findall(r'\b\w+\b', s.lower()) # Count the occurrences of each word word_counts = Counter(words) # Find the first word with 5 or more occurrences for word, count in word_counts.items(): if count >= 5: return word # If no word is repeated 5 times or more, return an empty string return "" # Example usage: print(match("test test test test test hello world")) # Output: test print(match("hello world hello world hello world hello world hello")) # Output: hello print(match("hello world")) # Output: (empty string) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import re from collections import Counter def match(s): """ Find any word repeated 5 times or more in a string. Args: s (str): The input string. Returns: str: The first word repeated 5 times or more, or an empty string. """ # Split the string into words words = re.findall(r'\b\w+\b', s.lower()) # Count the occurrences of each word word_counts = Counter(words) # Find the first word with 5 or more occurrences for word, count in word_counts.items(): if count >= 5: return word # If no word is repeated 5 times or more, return an empty string return "" # Example usage: print(match("test test test test test hello world")) # Output: test print(match("hello world hello world hello world hello world hello")) # Output: hello print(match("hello world")) # Output: (empty string) |
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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import re from collections import Counter def match(s): """ Find any word repeated 5 times or more in a string. Args: s (str): The input string. Returns: str: The first word repeated 5 times or more, or an empty string. """ # Split the string into words words = re.findall(r'\b\w+\b', s.lower()) # Count the occurrences of each word word_counts = Counter(words) # Find the first word with 5 or more occurrences for word, count in word_counts.items(): if count >= 5: return word # If no word is repeated 5 times or more, return an empty string return "" # Example usage: print(match("test test test test test hello world")) # Output: test print(match("hello world hello world hello world hello world hello")) # Output: hello print(match("hello world")) # Output: (empty string) 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') |
test
hello
All tests passed
Testing if the previous output contains the string All tests passed
: True