http://spark-university.s3.amazonaws.com/berkeley-saas/homework/hw1.pdf
尝试完成此作业的第 3 部分。以下代码似乎不起作用,即对于参数['HeLLo', 'hello']
,返回[["hello"], ["HeLLo"]]
而不是[["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
任何帮助,将不胜感激。(我是红宝石新手)