Last active
December 21, 2015 18:19
-
-
Save semanticart/6346135 to your computer and use it in GitHub Desktop.
letterpress word finder
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
# 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