20
2
First puzzle from me, suggestions for improvement gladly received!
The scenario is; You work as a manager for a whitewater rafting company. Every morning, you are given a list of bookings, and you have to sort them into raft loads. Write a program or function in your chosen language that does this for you.
Each raft holds a maximum of n
clients, and each booking is for a group of between 1 and n
people (inclusive).
The following rules must be observed;
No groups may be split up. If they booked together, they must all be in the same raft.
The number of rafts must be minimised.
Subject to the two preceeding rules, the groups must be spread as equally as possible between the rafts.
Inputs.
The number n
(you may assume that this is a positive integer), and the size of all the bookings. This may be an array, list or similar data structure if your language supports such things. All of these will be positive integers between 1 and n
. The order of the bookings is not defined, nor is it important.
Output. A list of the booking numbers, grouped into raft loads. The grouping must be indicated unambiguously, such as;
- a list, or array of arrays.
- a comma separated list for each raft. Newline between each raft.
How you implement the third rule is up to you, but this could involve finding the average raft occupancy, and minimising deviations from it as much as possible. Here are some test cases.
n Bookings Output
6 [2,5] [5],[2]
4 [1,1,1,1,1] [1,1,1],[1,1]
6 [2,3,2] [2,2],[3]
6 [2,3,2,3] [2,3],[2,3]
6 [2,3,2,3,2] [2,2,2],[3,3]
12 [10,8,6,4,2] [10],[8,2],[6,4]
6 [4,4,4] [4],[4],[4]
12 [12,7,6,6] [12],[7],[6,6]
Standard rules apply, shortest code wins. Have fun!
Edited; A suggested way to define as equally as possible for the third rule.
Once the number of rafts r
has been determined (subject to the second rule), the average occupancy a
can be calculated by summing over the bookings, and dividing by r
. For each raft, the deviation from the average occupancy can be found using d(x) = abs(n(x)-a)
, where n(x)
is the number of people in each raft and 1 <= x <= r
.
For some continuous, single-valued function f(y)
, which is strictly positive and has a strictly positive first and non-negative second derivatives for all positive y
, we define a non-negative quantity F
, as the sum of all the f(d(x)), 1 <= x <= r
. Any choice of raft allocation that satisfies the first two rules, and where F
is equal to the global minimum will satisfy the third rule also.
3
For future reference you can post to our sandbox to get feedback on a challenge before you post.
– Post Rock Garf Hunter – 2017-02-12T03:17:49.923Welcome to Programming Puzzles & Code Golf! This looks like a nice challenge, knowing that its your first challenge. Next time, however, it might be better to post the challenge in the Sandbox first, so people can give suggestions there. Then when you think the challenge is done, you can post it on the main site. Thank you for reading, and have a nice day!
– Matthew Roh – 2017-02-12T03:24:23.363How is as equally as possible measured? – Dennis – 2017-02-12T05:50:46.087
@Dennis; I will put a suggested way to define this in an edit. However if you have a different method, and can justify it for your answer, that is fine. – Gwyn – 2017-02-12T06:11:18.427
1Leaving things up to the implementation is fine, as long as it's clear what is valid and what not, and your latest edit achieves that imo. I'm a bit surpised that we can't use
g(y) = y
(second derivate zero) org(y) = y²
(first derviate zero wheny = 0
) though. – Dennis – 2017-02-12T06:52:32.937@Dennis: I indended that g(y) = y² be allowed, as the requirement is only for positive y, which excludes y=0 - apologies for not being clearer. – Gwyn – 2017-02-12T07:19:15.113
If
f(y) = y
is disallowed, you should add a test-case where it doesn't give the required result. Because for all the current test-cases I get the same result withf(y) = y
andf(y) = y²
. – smls – 2017-02-12T12:27:23.180@smls; You are right. I have edited to allow a larger class of functions. – Gwyn – 2017-02-12T16:34:44.563
I've edited the title to improve searchability, because I suspect this will be used in the future as a dupe target. – Peter Taylor – 2017-02-13T08:36:10.343