Skip to content

Instantly share code, notes, and snippets.

@aslam
Created November 16, 2018 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslam/221dfa751dca890d9e19e3c7f4478509 to your computer and use it in GitHub Desktop.
Save aslam/221dfa751dca890d9e19e3c7f4478509 to your computer and use it in GitHub Desktop.
# 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