Skip to content

Instantly share code, notes, and snippets.

@yarinb
Created October 27, 2012 21:24
Show Gist options
  • Save yarinb/3966361 to your computer and use it in GitHub Desktop.
Save yarinb/3966361 to your computer and use it in GitHub Desktop.
scrabble solve
#!/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