Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the best "deal" in a "group buy", given a table of values

Tags:

python

php

math

Using PHP or Python, but I'm sure the basic functions are agnostic.

I am unsure what the proper term, mathematical theory, or algorithm is, otherwise I'm sure Google would have fixed this for me in minutes.

I have a data set similar to the following:

cost | qty | ppl | store
------------------------
   30|  500|   10|     1
   40|  600|   12|     2
   35|  500|   14|     3
   50|  700|   10|     1
   30|  700|   12|     1
   40|  250|   14|     2

What I'm trying to do is find the "optimal" row, based on these qualifiers:

  • cost: lower is better.
  • qty: higher is better.
  • ppl: lower is better.
  • store: Doesn't matter in this case, but used later to find "best" depending on 'store'.

Essentially, I'm trying to find the best particular "deal" in a "group buy"-like situation, where the fewest amount of people are required to get the best "value" (quantity -vs- cost).

To my eye, it appears that the best-overall would be Row #5, because of the jump in quantity.

If there is a name for this, and a good (Wikipedia?) article on the subject, I'd be happy to finish this myself. Thanks for your time!

like image 335
anonymous coward Avatar asked Nov 22 '25 03:11

anonymous coward


1 Answers

Compute qty / (cost * ppl) and sort the list by that number. This number will be higher for higher qty and lower cost and ppl.

You might want to use something like this (python):

def cmp(a, b):
    return (a["qty"] / (a["cost"] * a["ppl"])) - (b["qty"] / (b["cost"] * b["ppl"]))

list = sorted(list, cmp)

Explanation: think what happens if qty is getting bigger when cost * ppl are constant. The ratio will increase, because a/x > b>x if a > b. Now with the other two values it's the other way around; if x/a > x/b, then a < b, so the ratio would actually get decreased when cost or ppl increases (think what happens if you split 100$ to two people vs three people; if you split it to two, each will get 100/2 = 50$. If you split it to three, each will get 100/3 ~= 33$, which is less). (Sorry if i'm not making it clear enough; i'm tired)

like image 90
Gabi Purcaru Avatar answered Nov 23 '25 19:11

Gabi Purcaru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!