dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.1 KiB
2.1 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68b06e589bf227324381476f | Challenge 35: Word Frequency | 29 | challenge-35 |
--description--
Given a paragraph, return an array of the three most frequently occurring words.
- Words in the paragraph will be separated by spaces.
- Ignore case in the given paragraph. For example, treat
Helloandhelloas the same word. - Ignore punctuation in the given paragraph. Punctuation consists of commas (
,), periods (.), and exclamation points (!). - The returned array should have all lowercase words.
- The returned array should be in descending order with the most frequently occurring word first.
--hints--
get_words("Coding in Python is fun because coding Python allows for coding in Python easily while coding") should return ["coding", "python", "in"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_words("Coding in Python is fun because coding Python allows for coding in Python easily while coding"), ["coding", "python", "in"])`)
}})
get_words("I like coding. I like testing. I love debugging!") should return ["i", "like", "coding"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_words("I like coding. I like testing. I love debugging!"), ["i", "like", "coding"])`)
}})
get_words("Debug, test, deploy. Debug, debug, test, deploy. Debug, test, test, deploy!") should return ["debug", "test", "deploy"].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_words("Debug, test, deploy. Debug, debug, test, deploy. Debug, test, test, deploy!"), ["debug", "test", "deploy"])`)
}})
--seed--
--seed-contents--
def get_words(paragraph):
return paragraph
--solutions--
import re
def get_words(paragraph):
cleaned = re.sub(r'[.,?]', '', paragraph.lower())
words = cleaned.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
sorted_words = sorted(freq.keys(), key=lambda w: freq[w], reverse=True)
return sorted_words[:3]