-
-
Save ngw/54a544b7f9dcd8964d7d27ae84c754ac 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
module Anagrams | |
class Dictionary | |
extend Forwardable | |
def_delegators :@data, :[] | |
def initialize(source:) | |
@data = Hash.new { |hash, key| hash[key] = [] } | |
parse(File.open(source, 'r')) | |
end | |
private | |
def parse(source) | |
source.each_line do |line| | |
word = line.downcase.strip | |
@data[normalize(word)] << word | |
end | |
end | |
def normalize(string) | |
string.chars.sort.join | |
end | |
end | |
end |
havenwood
commented
Jul 15, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment