Last active
August 28, 2018 09:38
-
-
Save woodRock/53a028cbac4d0527cc488ffb6cefeacd to your computer and use it in GitHub Desktop.
Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix. For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal]. Hint: Try preprocessing the dictionary into a more efficient data structure to spe…
This file contains hidden or 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
#!/usr/bin/env ruby | |
dictionary = ["Dog", "Deer", "Deal"] | |
def autocomplete(search, dict) | |
result = Array.new | |
if dict.respond_to?("each") | |
dict.each do |d| | |
if search.eql? d[0..search.length-1] | |
result << d | |
end | |
end | |
end | |
return result | |
end | |
if __FILE__ == $0 | |
puts autocomplete("De", dictionary) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment