Last active
August 6, 2020 23:33
-
-
Save catskull/3bf3cb718b8d8578f9e7d0175f3f8057 to your computer and use it in GitHub Desktop.
This file contains 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
# mostly taken from this stack overflow post: https://stackoverflow.com/a/39212997 | |
class Array | |
def difference(other) | |
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 } | |
reject { |e| h[e] > 0 && h[e] -= 1 } | |
end | |
end | |
def scrabble(letters) | |
f = File.open('/usr/share/dict/words') | |
words = f.read.downcase.split("\n").uniq | |
longest = '' | |
words.each do |word| | |
if word.length <= letters.length && word.chars.difference(letters.chars).empty? && word.length > longest.length | |
longest = word | |
end | |
end | |
longest | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment