Skip to content

Instantly share code, notes, and snippets.

@catskull
Last active August 6, 2020 23:33
Show Gist options
  • Save catskull/3bf3cb718b8d8578f9e7d0175f3f8057 to your computer and use it in GitHub Desktop.
Save catskull/3bf3cb718b8d8578f9e7d0175f3f8057 to your computer and use it in GitHub Desktop.
# 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