okokokokokook
This commit is contained in:
parent
61dc873489
commit
a47ef094af
9 changed files with 136 additions and 25 deletions
|
|
@ -5,27 +5,14 @@ def count_words(text):
|
|||
for word in words:
|
||||
word = word.strip('.,!?')
|
||||
if word in word_count:
|
||||
word_count[word] += 1
|
||||
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")
|
||||
print(f"Word counts: {counts}")
|
||||
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")
|
||||
|
|
@ -5,7 +5,7 @@ def count_words(text):
|
|||
for word in words:
|
||||
word = word.strip('.,!?')
|
||||
if word in word_count:
|
||||
word_count[word] += 1
|
||||
word_count[word] = word_count[word] + 1
|
||||
else:
|
||||
word_count[word] = 1
|
||||
|
||||
|
|
|
|||
31
test/ruby/34.in
Normal file
31
test/ruby/34.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