-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdictionary_finder.rb
More file actions
39 lines (27 loc) · 956 Bytes
/
dictionary_finder.rb
File metadata and controls
39 lines (27 loc) · 956 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
25
26
27
28
29
30
31
32
33
34
# #Given a string of characters and a dictionary that will
# #return true if a string is a real word, find the words in the string
# #and return the string with a space in between each of the real words.
# #Example: string = "applepie", dictionary("apple") = true,
# #dictionary("pie") = true, dictionary("a") = true,
# #dictionary returns false for all other substrings in "applepie" answer = "apple pie"
# # 1) There are two words in the string
# # 2) There are n words in the string
def dictionary(word)
dictionary = ["apple", "pie", "a",
"hello", "world", "he"]
dictionary.include?(word)
end
def dictionary_finder(string)
output = []
(0...string.length).each do |i|
(i...string.length).each do |j|
if dictionary(string[i..j])
output << string[i..j]
end
end
end
puts output.join(' ')
end
dictionary_finder("applepie")
dictionary_finder("helloworldapplepie")
dictionary_finder("apple")