okokokokokook
This commit is contained in:
parent
61dc873489
commit
a47ef094af
9 changed files with 136 additions and 25 deletions
24
test/ruby/27.out
Normal file
24
test/ruby/27.out
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def __contains__(container, item)
|
||||
if container.is_a?(Hash)
|
||||
container.key?(item)
|
||||
else
|
||||
container.include?(item)
|
||||
end
|
||||
end
|
||||
def count_words(text)
|
||||
words = text.downcase().split()
|
||||
word_count = {}
|
||||
for word in words
|
||||
word = word.delete('.,!?')
|
||||
if __contains__(word_count, word)
|
||||
word_count[word] = word_count[word] + 1
|
||||
else
|
||||
word_count[word] = 1
|
||||
end
|
||||
end
|
||||
return word_count
|
||||
end
|
||||
text = "Hello world hello there world hello"
|
||||
counts = count_words(text)
|
||||
print("Text: #{text}", "\n")
|
||||
print("Word counts: #{counts}", "\n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue