some solutions for projects on odin project
This commit is contained in:
24
substrings.rb
Normal file
24
substrings.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
def substrings(str_input, dictionary)
|
||||
unless str_input.is_a?(String) and dictionary.is_a?(Array)
|
||||
raise TypeError("Invalid type provided")
|
||||
end
|
||||
|
||||
result = {}
|
||||
|
||||
all_words = str_input.downcase.gsub(/[^0-9a-z ]/i, '').split(" ")
|
||||
|
||||
dictionary.each do |key|
|
||||
all_words.each do |word|
|
||||
if word.include?(key.downcase)
|
||||
result[key] = result.has_key?(key) ? result[key] + 1 : 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
|
||||
dictionary = ["below","down","go","going","horn","how","howdy","it","i","low","own","part","partner","sit"]
|
||||
|
||||
print substrings("Howdy partner, sit down! How's it going?", dictionary)
|
||||
Reference in New Issue
Block a user