1D Packing Problem - Recursive Solution
class Carousel
@@rectangles = [1]*5 + [2]*5 + [3]*5 + [4]*5 + [5]*5
attr_accessor :carousel
def initialize(total_size)
@carousel = []
fill_carousel(total_size, @carousel)
end
def get_rand_rectangle(size)
return @@rectangles.select{|r| r <= size}.sample
end
def fill_carousel(total_size, carousel)
room_left = total_size - carousel.inject(0, :+)
if room_left <= 0
return carousel
else
carousel << get_rand_rectangle(room_left)
fill_carousel(total_size, carousel)
end
end
end
Carousel.new(5).carousel