Finished
1import re
2from collections import Counter
3
14def count_words(sentence):
2 pass
5 # Edge Case: empty input string
6 if not sentence:
7 return {}
8
9 # Convert to lowercase to handle case insensitivity
10 sentence = sentence.lower()
11
12 # Find all sequences that qualify as words
13 # This includes alphanumeric sequences and contractions with apostrophes
14 # Edge Case: handling contractions with apostrophes as single words
15 # Edge Case: treating numbers as words
16 # Edge Case: handling mixed case words
17 words = re.findall(r"[a-z0-9]+(?:'[a-z0-9]+)*", sentence)
18
19 # Count occurrences of each word
20 word_counts = Counter(words)
21
22 # Convert Counter to regular dictionary
23 return dict(word_counts)
24# Handled Edge Cases: empty input string, contractions with apostrophes, numbers as words, mixed case words
Test NameStatus
test_alternating_word_separators_not_detected_as_a_word
Pass
test_count_one_of_each_word
Pass
test_count_one_word
Pass
test_handles_cramped_lists
Pass
test_handles_expanded_lists
Pass
test_ignore_punctuation
Pass
test_include_numbers
Pass
test_multiple_apostrophes_ignored
Pass
test_multiple_occurrences_of_a_word
Pass
test_multiple_spaces_not_detected_as_a_word
Pass
test_non_alphanumeric
Pass
test_normalize_case
Pass
test_quotation_for_word_with_apostrophe
Pass
test_substrings_from_the_beginning
Pass
test_tabs
Pass
test_with_apostrophes
Pass
test_with_quotations
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.