Created
October 10, 2013 04:31
-
-
Save TGOlson/6913079 to your computer and use it in GitHub Desktop.
Scan Dictionary for String Matches
This file contains 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
def get_input | |
puts "Type a word to find matches." | |
print '> ' | |
base_word = gets.chomp | |
scan_dictionary(base_word) | |
end | |
def scan_dictionary(word) | |
matches = read_dictionary.select { |line| line.include?(word.downcase) } | |
print_output(word, matches) | |
end | |
def read_dictionary | |
File.readlines("en_US.txt") | |
end | |
def print_output(word, matches) | |
puts "#{matches.length} matches for '#{word}'" | |
p matches | |
end | |
get_input | |
# anagrams :> ruby anagrams.rb | |
# Type a word to find matches. | |
# > String | |
# 14 matches for 'String' | |
# ["astringency/M\n", "astringent/MYS\n", "bowstring/SM\n", | |
# "drawstring/MS\n", "hamstring/SGM\n", "heartstrings/M\n", | |
# "restring/SG\n", "shoestring/MS\n", "string/MDRSZG\n", | |
# "stringency/M\n", "stringent/Y\n", "stringer/M\n", | |
# "stringiness/M\n", "stringy/PTR\n"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment