-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstring.rb
More file actions
24 lines (18 loc) · 764 Bytes
/
substring.rb
File metadata and controls
24 lines (18 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true
require './utils'
def substrings(text, substr_array)
text.downcase.split.each_with_object(Hash.new(0)) do |item, res|
substr_array.each do |w|
res[w] += 1 if item.include?(w)
end
end
end
dictionary = %w[below down go going horn how howdy it i low own part partner sit]
p substrings('below', dictionary)
p substrings("Howdy partner, sit down! How's it going?", dictionary)
assert(substrings('below', dictionary) == { 'below' => 1, 'low' => 1 })
assert(
substrings("Howdy partner, sit down! How's it going?",
dictionary) == { 'down' => 1, 'go' => 1, 'going' => 1, 'how' => 2, 'howdy' => 1, 'it' => 2, 'i' => 3, 'own' => 1,
'part' => 1, 'partner' => 1, 'sit' => 1 }
)