Created
November 16, 2018 11:47
-
-
Save aslam/221dfa751dca890d9e19e3c7f4478509 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
# Implement a method #substrings that takes a word as the first argument and then | |
# an array of valid substrings (your dictionary) as the second argument. | |
# It should return a hash listing each substring (case insensitive) that was found | |
# in the original string and how many times it was found. | |
``` | |
> substrings("Howdy partner, sit down! How's it going?", dictionary) | |
=> { "down" => 1, "how" => 2, "howdy" => 1,"go" => 1, "going" => 1, "it" => 2, "i" => 3, "own" => 1,"part" => 1,"partner" => 1,"sit" => 1 } | |
``` | |
dictionary = ["below","down","go","going","horn","how","howdy","it","i","low","own","part","partner","sit"] | |
def substrings(phrase, dict) | |
matches = Hash.new(0) | |
word_list = phrase.split(' ') | |
word_list.each do |word| | |
dict.each do |w| | |
if word.downcase.include?(w) | |
matches[w] += 1 | |
end | |
end | |
end | |
matches | |
end | |
puts substrings("below", dictionary) | |
puts substrings("Howdy partner, sit down! How's it going?", dictionary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment