Created
May 17, 2012 23:08
-
-
Save ootoovak/2722187 to your computer and use it in GitHub Desktop.
1D Packing Problem - Recursive Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment