Created
October 27, 2012 21:24
-
-
Save yarinb/3966361 to your computer and use it in GitHub Desktop.
scrabble solve
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 | |
dict = File.open('/usr/share/dict/words', 'r').read | |
letters = ARGV[0] unless ARGV.empty? | |
def can_make?(word, letters) | |
return letters.include? word if word.size == 1 | |
letters.include? word[0] and | |
can_make?(word[1..-1], letters.sub(/#{word[0]}/, "")) | |
end | |
def work(dict, letters) | |
words = dict.split("\n").map { |w| w if can_make?(w, letters) }.compact | |
puts words.sort {|a,b| a.length <=> b.length} | |
end | |
work(dict, letters) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment