Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##
- # This doesn't work! :-(
- ##
- def max_concat_number(list)
- # Find the "length" of the maximum number.
- length = list.max.to_s.length
- # Normalize the list by padding every number to the maximum length.
- padded = list.map(&:to_s).map { |n| n.ljust(length, "0") }
- # Create a hash with the padded numbers as keys and the "actual" numbers as values.
- arranged = padded.each_with_index.each_with_object({}) do |(element, index), arranged_hash|
- arranged_hash[element] = list[index]
- end
- # Sort the hash by the keys, then pick up just the numbers.
- sorted = arranged.sort.reverse.map { |a| a[1] }
- # Finally, join the sorted list.
- sorted.map(&:to_s).join
- end
- list = [50, 2, 99, 1, 958, 9, 6, 80]
- p max_concat_number(list)
- # => "9995898065021"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement