tests
This commit is contained in:
parent
f92f5ef7f6
commit
307c51539c
10 changed files with 252 additions and 1 deletions
31
test/ruby/28.in
Normal file
31
test/ruby/28.in
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
def count_words(text):
|
||||
words = text.lower().split()
|
||||
word_count = {}
|
||||
|
||||
for word in words:
|
||||
word = word.strip('.,!?')
|
||||
if word in word_count:
|
||||
word_count[word] += 1
|
||||
else:
|
||||
word_count[word] = 1
|
||||
|
||||
return word_count
|
||||
|
||||
def find_most_common(word_count):
|
||||
most_common = None
|
||||
max_count = 0
|
||||
|
||||
for word, count in word_count.items():
|
||||
if count > max_count:
|
||||
most_common = word
|
||||
max_count = count
|
||||
|
||||
return most_common, max_count
|
||||
|
||||
text = "Hello world hello there world hello"
|
||||
counts = count_words(text)
|
||||
common_word, frequency = find_most_common(counts)
|
||||
|
||||
print(f"Text: {text}")
|
||||
print(f"Word counts: {counts}")
|
||||
print(f"Most common: '{common_word}' appears {frequency} times")
|
||||
Loading…
Add table
Add a link
Reference in a new issue