http://spark-university.s3.amazonaws.com/berkeley-saas/homework/hw1.pdf
Trying to part 3 of this assignment. The following code does not seem to work, namely for the parameter ['HeLLo', 'hello']
, returning [["hello"], ["HeLLo"]]
instead of [["HeLLo", "hello"]]
def combine_anagrams(words)
#iterate through words, make hashmap with the sorted version
hash = {}
words.each do |x|
hash[x.chars.sort.join.downcase.gsub /\W/, ""] = []
end
#iterate through words, access hashmap and append curr to array
words.each do |x|
hash[x.chars.sort.join.downcase.gsub /\W/, ""] << x
end
hash.values #return array of values
end
Any help would be appreciated. (I'm new to Ruby)