I share a flat with two other students. When someone bought something we all (or two of us) will benefit of like food or household articles, we don’t split up the price immediately. Instead, we put it on a (paper-based) list. When this list is full, someone (me…) needs to do the math and figure out who needs to pay how much money to whom. This is where computer science comes in handy 😉 So I finally wrote a small program which takes such a list and prints something like “A needs to pay B 23.42 Euro”.
First, let’s have a look at a short list:
x 1337 xy 235 z 42 xyz y 1234 yz
In this example, Person x has paid for three things. The first one cost 13.37 Euro/$/credits/whatever and will be used by x and y. The second one cost 2.35 and will be exclusively used by z. The third one cost 0.42 and is used by all three persons. You get the idea…
For each person that bought something, there is a section, started by a line containing only its identifying letter. You can’t use words as identifiers for persons; it need to be single letters! Followed by the letters of all the persons that should pay for this entry, there is the price that was paid, written without a decimal point.
What the program – I named it “Show me the bill!” or “showbill” – now does for each entry is splitting it up and adding it to virtual bank accounts. For “42 xyz” in x’s section, this means it will calculate something like this (42/3 = 14):
y pays 0.14 Euro to x z pays 0.14 Euro to x
You’ll need PHP5 (command line version, this is not web based!) to run it. For Windows, get it at PHP.net. If you use Linux, your distribution will already include it in its package management system (Debian/Ubuntu: aptitude install php5-cli takes care of the installation).
Just change to the directory containing the script and type php showbill.php list.txt in your command line, where list.txt is the file containing the list discussed above. That’s it!