python2ruby/test/ruby/28.in
2025-12-06 12:16:48 +01:00

31 lines
No EOL
793 B
Text

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] = 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")