This function requires you to log in. Please enter your login details

View Problem

Categorise a list

Given the list [one, two, three, four, five] produce a map {3:[one, two], 4:[four, five], 5:[three]} which sorts elements into map entries based on their length
ExpandDiskEdit
ruby
lengths = {}
list.each do |x|
len = x.size
lengths[len] = (lengths[len] || [])
lengths[len] << x
end

Submit a new solution for ruby
There are 15 other solutions in additional languages (cpp, csharp, erlang, fsharp ...)