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

18 lines
No EOL
424 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
text = "Hello world hello there world hello"
counts = count_words(text)
print(f"Text: {text}")
print(f"Word counts: {counts}")