This function requires you to log in. Please enter your login details
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
ruby lengths = {}
list.each do |x|
len = x.size
lengths[len] = (lengths[len] || [])
lengths[len] << x
end
list = ['one', 'two', 'three', 'four', 'five']
lengths = {}
list.each do |x|
len = x.size
lengths[len] = (lengths[len] || [])
lengths[len] << x
end
p lengths
Submit a new solution for
ruby
There are 15 other solutions in
additional languages (
cpp,
csharp,
erlang,
fsharp ...)