Created
April 19, 2019 20:47
-
-
Save fractalatcarf/f6cada0610ca397323d63acac356693f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def frequencies(text) | |
result = {} | |
words = text.split(" ") | |
words.each do |word| | |
if result.key?(word) | |
result[word] +=1 | |
else | |
result[word] = 1 | |
end | |
end | |
return result | |
end | |
my_text = File.read("ruby.txt") | |
words_frequencies = frequencies(my_text) | |
p words_frequencies.sort_by {|_key, value| value} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../frequencies" | |
describe "#frequencies" do | |
it "simple test" do | |
actual = frequencies("hello boy hello") | |
expected = {"hello" => 2, "boy" => 1} | |
expect(actual).to eq(expected) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment