Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created December 7, 2012 20:46
Show Gist options
  • Select an option

  • Save sheenobu/4236365 to your computer and use it in GitHub Desktop.

Select an option

Save sheenobu/4236365 to your computer and use it in GitHub Desktop.
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
[10, 5, 1, 1, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment