Skip to content

Instantly share code, notes, and snippets.

@semanticart
Last active December 21, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semanticart/6346135 to your computer and use it in GitHub Desktop.
Save semanticart/6346135 to your computer and use it in GitHub Desktop.
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