Created
June 24, 2017 18:14
-
-
Save kaibrabo/726e11dad75486140ef50b6580a43fca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
=begin | |
Define a method called frequency_table that will take a String as an argument and return a frequency table (using the Hash class), | |
where each key is a word in the String and it's value is the number of times the word occurs in the sentence. | |
The method should not be case sensitive. Assume that the input will always be a String. | |
Return an empty Hash for an empty String. | |
=end | |
=begin | |
test: | |
describe "#frequency_table" do | |
it "returns a frequency table based on the argument" do | |
sentence = "This test is a TEST of the TesT" | |
expected_outcome = {"this"=>1, "test"=>3, "is"=>1, "a"=>1, "of"=>1, "the"=>1} | |
expect(frequency_table(sentence)).to eq(expected_outcome) | |
end | |
it "returns a frequency table based on the argument" do | |
sentence = "I am legend AM am Am" | |
expected_outcome = {"i"=>1, "am"=>4, "legend"=>1} | |
expect(frequency_table(sentence)).to eq(expected_outcome) | |
end | |
it "returns an empty hash when called with no input" do | |
expect(frequency_table('')).to eq({}) | |
end | |
end | |
=end | |
sentence = "This test is a TEST of the TesT" | |
def frequency_table(sentence) | |
new_string = sentence.downcase.split(" ") | |
# = ["this", "test", "is", "a", "test", "of", "the", "test"] | |
str = Hash.new | |
# str = {} | |
new_string.each do |key, value| | |
value = 0 | |
# if key === key | |
# value += 1 | |
# end | |
str[key] = value += 1 | |
end | |
# => {"this"=>1, "test"=>1, "is"=>1, "a"=>1, "of"=>1, "the"=>1} | |
str | |
# => {"this"=>0, "test"=>7, "is"=>2, "a"=>3, "of"=>5, "the"=>6} | |
end | |
frequency_table(sentence) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment