Created
October 11, 2019 17:13
-
-
Save arthur-littm/ce12d4ad702a2ee12864a87f10089b71 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) | |
# intro downcase the string, and create an empty hash | |
frequency = {} | |
text = text.downcase | |
# 1. split the string into an array of words | |
words = text.split | |
# words => ["the", "lazy", ...] | |
# 2. iterate over the array | |
words.each do |word| | |
# each: count how many times it is in the text | |
if frequency.key?(word) | |
# in our hash update the word and amount | |
frequency[word] += 1 | |
# frequency['the'] += 1 | |
else | |
# set to one | |
frequency[word] = 1 | |
# frequency['the'] = 1 | |
end | |
end | |
# 3. return the hash | |
return frequency | |
end | |
p frequencies("the lazy dog jumped over the brown fox") | |
# {"the" => 2, "fox" => 1, ...} | |
# OTHER ADVANCED SOLUTION👇 | |
def frequencies(text) | |
# intro downcase the string, and create an empty hash | |
frequency = Hash.new(0) | |
text = text.downcase | |
# 1. split the string into an array of words | |
words = text.split | |
# words => ["the", "lazy", ...] | |
# 2. iterate over the array | |
words.each do |word| | |
# each: count how many times it is in the text | |
frequency[word] += 1 | |
end | |
# 3. return the hash | |
return frequency | |
end | |
p frequencies("the lazy dog jumped over the brown fox") | |
# {"the" => 2, "fox" => 1, ...} |
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 "returns an empty Hash when passed an empty string" do | |
# TODO | |
frequencies_hash = frequencies("") | |
expect(frequencies_hash).to eq({}) | |
end | |
it "counts multiple words" do | |
frequencies_hash = frequencies("the fox the") | |
expect(frequencies_hash).to eq({"the" => 2, "fox" => 1}) | |
end | |
end |
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
# Q8 | |
grades = [19, 8, 11, 15, 13] | |
# TODO: compute and store the result in a variable `average` | |
sum = 0 | |
grades.each do |grade| | |
sum += grade | |
end | |
average = sum / grades.size.to_f | |
# p sum | |
# Q.9 | |
fruits = ["banana", "peach", "watermelon", "orange"] | |
# 0 1 2 3 | |
# Print out "peach" from the fruits array in the terminal | |
# puts fruits[1] | |
# Add an "apple" to the fruits array # Replace "watermelon" by "pear" | |
fruits << "apple" | |
# Delete "orange" | |
fruits.delete_at(3) | |
# Q.10 | |
city = { name: "Paris", population: 2000000 } | |
# Print out the name of the city | |
# puts city[:name] | |
# Add the Eiffel Tower to city with the `:monument` key | |
city[:monument] = "Eiffel Tower" | |
# Update the population to 2000001 | |
city[:population] = 2000001 | |
# What will the following code return? | |
city[:mayor] | |
# => nil | |
# Q.11 | |
# Use the map iterator to convert the variable students, | |
# an array of arrays, into an array of hashes. | |
# Those hashes should have two keys: `:name` and `:age` | |
students = [ [ "john", 28 ], [ "mary", 25 ], [ "paul", 21 ] ] | |
new_arr = students.map do |student| | |
{name: student[0], age: student[1]} | |
end | |
p new_arr | |
# students = [ { name: "john", age: 28 ]}, {...} ] | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment