-
-
Save jonathanyee/2191903 to your computer and use it in GitHub Desktop.
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
unless ARGV.size == 2 | |
puts "USAGE: ruby guess.rb ACKRCKOOLCWZ 5" | |
exit 1 | |
end | |
letters = ARGV[0].downcase.split(//).sort | |
length = ARGV[1].to_i | |
words = [] | |
File.open('/usr/share/dict/words') do |file| | |
file.each do |line| | |
word = line.strip.downcase | |
words << word if word.length == length | |
end | |
end | |
def valid?(word, letters) | |
word_index = letters_index = 0 | |
while word_index < word.length && letters_index < letters.length | |
if word[word_index] == letters[letters_index] | |
word_index = word_index + 1 | |
end | |
letters_index = letters_index + 1 | |
end | |
return word_index == word.length | |
end | |
guesses = [] | |
words.each do |word| | |
guesses << word if valid?(word.split(//).sort, letters) | |
end | |
guesses.each do |guess| | |
puts guess | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment