mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
25 lines
416 B
Ruby
25 lines
416 B
Ruby
|
class Array
|
||
|
def columns
|
||
|
x = map(&:length).max
|
||
|
Array.new(x) do |row|
|
||
|
Array.new(length) { |column| self[column][row] }.compact
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def bead_sort(array)
|
||
|
array
|
||
|
.map { |element| [1] * element }
|
||
|
.columns
|
||
|
.columns
|
||
|
.map(&:length)
|
||
|
.reverse
|
||
|
end
|
||
|
|
||
|
if $0 == __FILE__
|
||
|
puts 'Enter a list of numbers separated by space'
|
||
|
|
||
|
list = gets.split.map(&:to_i)
|
||
|
p bead_sort(list)
|
||
|
end
|