Created
December 7, 2012 20:46
-
-
Save sheenobu/4236365 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| licenses = [ 20,5,5,1,1,10,1,10,1,1,1,1,1,1,1].sort.reverse | |
| # simulates a sorted SQL select operation on the license table. | |
| def licenses.pop_eq_to_or_less_than(i) | |
| idx = 0 | |
| to_slice = -1 | |
| for x in self | |
| if x <= i | |
| to_slice = idx | |
| break | |
| end | |
| idx = idx + 1 | |
| end | |
| self.slice(idx) | |
| end | |
| # number of users | |
| enum = 18.times | |
| offset = 0 | |
| remaining_seats = 18 | |
| a = enum.map do |x| | |
| offset = offset - 1 unless offset < 1 | |
| if offset < 1 | |
| l = licenses.pop_eq_to_or_less_than(remaining_seats) | |
| raise "No More Licenses" if l.nil? | |
| offset = offset + l | |
| remaining_seats = remaining_seats - l | |
| l | |
| end | |
| end.compact.to_a | |
| p a |
This file contains hidden or 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
| [10, 5, 1, 1, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment