Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
letterpress word finder
# letters are either the first argument or something piped in
#
# e.g.
# ruby letter.rb KTVHROBDRBDLCYTPLEWAFZYMB
# or
# echo "KTVHROBDRBDLCYTPLEWAFZYMB" | ruby letter.rb
# or
# ruby -r ./board_parser -e "puts BoardParser.new('light.png').tiles.join" | ruby letter.rb
letters = (ARGV[0] || STDIN.read).downcase
words = File.read("/usr/share/dict/words").downcase.split("\n")
# via http://stackoverflow.com/questions/11349544/ruby-optimize-the-comparison-of-two-arrays-with-duplicates
def is_subset?(word, letters)
!word.chars.find{|char| word.count(char) > letters.count(char)}
end
matching_words = words.select do |word|
is_subset?(word, letters)
end
puts matching_words.sort_by(&:length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment