Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created December 7, 2012 18:12
Show Gist options
  • Select an option

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

Select an option

Save sheenobu/4235180 to your computer and use it in GitHub Desktop.
[5, ["1 x 5"]]
[7, ["1 x 7", "5 x 1 + 1 x 2"]]
[10, ["1 x 10", "2 x 5"]]
[13, ["1 x 13", "5 x 2 + 1 x 3", "10 x 1 + 1 x 3"]]
[15, ["1 x 15", "3 x 5", "10 x 1 + 1 x 5"]]
[18, ["1 x 18", "5 x 3 + 1 x 3", "10 x 1 + 1 x 8"]]
[20, ["1 x 20", "4 x 5", "10 x 2"]]
[25, ["1 x 25", "5 x 5", "10 x 2 + 1 x 5"]]
def convert_to_factors(seat_count,data)
data.map do |c,rem,div|
if c*div == seat_count
"#{c} x #{div}".split(/ x /).sort.join(" x ")
elsif c*div < seat_count and rem != 0 and div != 0
"#{c} x #{div} + 1 x #{rem}"
end
end.compact.uniq
end
def split_license_keys(seat_count)
convert_to_factors seat_count,
([1, 5, 10, 25].map do |c|
[ c, seat_count % c.to_f, seat_count / c.to_f ]
end.map do |c,rem,div|
[ c, rem.floor, div.floor ]
end)
end
p [5, split_license_keys(5)]
p [7, split_license_keys(7)]
p [10, split_license_keys(10)]
p [13, split_license_keys(13)]
p [15, split_license_keys(15)]
p [18, split_license_keys(18)]
p [20, split_license_keys(20)]
p [25, split_license_keys(25)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment