I am new to Ruby and am working on Anagram. So in anagram for the input
below
# input: ['cars', 'four','scar', 'creams', 'scream', 'scream']
I want an output like this
# output: [ ["cars","scar"],
# ["four"],
# ["creams", "scream", "scream"] ]
But i am getting an output like
# output: [ ["cars","scar"],
# ["four"],
# ["creams", "scream"] ]
because of Hash property.
This is my code.
sorted_words = Hash.new(0)
words.each { |word| sorted_words[word]=word.downcase.chars.sort { |a, b|
a.casecmp(B)/> }.join
}
sorted_hash = {}
sorted_words.each_pair do |key, value|
(sorted_hash[value] ||= []) << key
end
anagram_array = sorted_hash.values
Please advice me how to solve this. Thankyou for taking
you time out and helping with this amazing language

New Topic/Question
Reply



MultiQuote



|