Skip to content

Instantly share code, notes, and snippets.

@MBM1607
Created February 25, 2021 06:12
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 MBM1607/1eb90d49d47a008c16faae57c55a89f6 to your computer and use it in GitHub Desktop.
Save MBM1607/1eb90d49d47a008c16faae57c55a89f6 to your computer and use it in GitHub Desktop.
Ruby substrings exercise
# Return a hash listing each substring present in the dictionary found in the string
def substrings(string, dictionary)
count = Hash.new(0)
dictionary.each do |word|
count[word] = string.scan(/#{word}/).length if string.scan(/#{word}/).length != 0
end
puts count
end
dictionary = ["below", "down", "go", "going", "horn", "how", "howdy", "it", "i",
"low", "own", "part", "partner", "sit"]
substrings("below", dictionary)
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