Wednesday, October 21, 2015

Splitting Up: A Python Program

I've been discussing the common, but not quite trivial problem of settling bills among a bunch of friends, in a couple of posts.

Here is a rudimentary (but fully functional) Python program which implements the algorithms.

One needs to create a 2 or 3 column csv file (INPUTFILE), and send it to the program. Here is the header portion of the program, which describes how one interacts with the program, in more detail.

# Given a set of contributions (and optionally expemptions) creates a list of pair-wise
# transactions which "settles" all accounts
#
# example : python splitup.py INPUTFILENAME
#
# : if INPUTFILENAME not provided then looks for csv file "input_table"
#
# : INPUTFILE = csv file containing 2 or three columns, for example
#
# Two column example: NAME, CONTRIBUTION
# Person1, 10
# Person2, 20
# Person3, 5
# Person4, 15
# Person5, 10
#
#
# Three column example: NAME, CONTRIBUTION, AND EXPEMPTION
# Person1, 10, 0
# Person2, 20, 5
# Person3, 5, 0
# Person4, 15, 5
# Person5, 10, 0

Try it out!

PS: I am still in the process of learning Python, and my coding style is admittedly funky.

No comments: