Ruby substrings exercise
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
# 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