Skip to content

Instantly share code, notes, and snippets.

@woodRock
Last active August 28, 2018 09:38
Show Gist options
  • Save woodRock/53a028cbac4d0527cc488ffb6cefeacd to your computer and use it in GitHub Desktop.
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…
#!/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