KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I implemented a function to group anagrams. In a nutshell: input: ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', scream'] output: [["cars", "racs", "scar"], ["four"], ["for"], ["potatoes"],["creams", "scream"]] I would like to know if there is a better way to do this. I really think I used too much repetition statements: until , select , delete_if . Is there any way to combine the select and delete_if statement? That means, can selected items be automatically deleted? Code: def group_anagrams(words) array = [] until words.empty? word = words.first array.push( words.select { |match| word.downcase.chars.sort.join.eql?(match.downcase.chars.sort.join ) } ) words.delete_if { |match| word.downcase.chars.sort.join.eql?(match.downcase.chars.sort.join ) } end array end Thanks in advance,
Tags (comma-separated)
Save Edits
Cancel