3 ms·
Assuming the prices are in whole cents, here is a very silly bruteforce: #ap, bp - prices before/after lunch sell_distrib = [(a,b,c) for a in range(10)
by bluecalm 12y ago
Assuming the prices are in whole cents, here is a very silly bruteforce:
#ap, bp - prices before/after lunch
sell_distrib = [(a,b,c) for a in range(10) for b in range(16) for c in range(26) if a > b > c]
for bp in range(0, 3501):
for x in sell_distrib:
ap = (3500-x[0]*bp) // (10-x[0])
if x[0]*bp + (10-x[0])*ap == 3500 and \
x[1]*bp + (16-x[1])*ap == 3500 and \
x[2]*bp + (26-x[2])*ap == 3500:
print("Prices: {0} {1}".format(bp, ap))
print("Before lunch sales: {}, {}, {}".format(x[0], x[1], x[2]))
Note: it's Python 3 (the division!) and first line in the conditional eliminates cases where after lunch price wouldn't be a whole number because then it wouldn't add up to 3500.
It doesn't consider cases where one farmer sold everything before lunch (supposedly sales didn't go so well). It's two lines of code more if we want to check those cases.
Eliminating of 0 price and requirement that after lunch price is lower than before lunch one is covered by noticing that the solution then must have farmer A selling more than farmer B who in turn sell more than farmer C before lunch.
There is a unique solution in whole cents which is nice (no spoilers, you can copy-paste the code to Python interpreter to see it)
- SatvikBeri 12y agoAn even sillier bruteforce lets you reproduce the problem almost verbatim: let a and b be the daytime and nighttime prices, let x, y, and z be the number of chickens sold during the day by each farmer. [(a, b, x, y, z) for a in xrange(1, 500) for b in xrange(1, a) for x in xrange(1, 11) for y in xrange(1, x) for z in xrange(1, y) if 3500 == a*x + b*(10 - x) == a*y + b*(16 - y) == a*z + b*(26 - z)]
- bluecalm 12y agoVery nice, I need to practice my Python one liners :) It's significantly slower though as you are iterating over 2nd price as well (and I derive it from the fact that total profit is known so daytime price and amount of sold chickens sold implies nighttime price).