Skip to content

Instantly share code, notes, and snippets.

@fractalatcarf
Created April 19, 2019 20:47
Show Gist options
  • Save fractalatcarf/f6cada0610ca397323d63acac356693f to your computer and use it in GitHub Desktop.
Save fractalatcarf/f6cada0610ca397323d63acac356693f to your computer and use it in GitHub Desktop.
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}
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