Created
June 3, 2014 23:55
-
-
Save mayfer/5d1fa7c9cfe6c37371dc to your computer and use it in GitHub Desktop.
Functions that find most common prey, and see if words rhyme
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
r = { | |
"shrimp" => [ | |
"algae", | |
"other_shrimps", | |
"seaweed", | |
], | |
"toad" => [ | |
"children", | |
"flies", | |
], | |
"lion" => [ | |
"donut", | |
"tourists", | |
"children", | |
], | |
} | |
def largest_hash_key(hash) | |
hash.max_by{|k,v| v}[0] | |
end | |
def most_common_prey(animals_hash) | |
food_counts = {} | |
animals_hash.each do |animal, foods| | |
foods.each do |food| | |
if !food_counts.has_key? food | |
food_counts[food] = 0 | |
end | |
food_counts[food] += 1 | |
end | |
end | |
largest_hash_key(food_counts) | |
end | |
puts "Most common prey: #{most_common_prey(r)}" | |
def rhymes?(word1, word2, num_letters) | |
(1..num_letters).each do |letter_index| | |
if word1.chars[-letter_index] != word2.chars[-letter_index] | |
return false | |
end | |
end | |
return true | |
end | |
word1 = "murat" | |
word2 = "meercat" | |
puts "Do #{word1} and #{word2} rhyme? #{rhymes?(word1, word2, 100)}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment