Skip to content

Instantly share code, notes, and snippets.

@jenniferthompson
Created June 28, 2018 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenniferthompson/1b733bbaf6e6cb01077a3f6343870f5c to your computer and use it in GitHub Desktop.
Save jenniferthompson/1b733bbaf6e6cb01077a3f6343870f5c to your computer and use it in GitHub Desktop.
One time I went on a cruise with eight other people, and we got a refund, and we had to figure out how to split it under multiple scenarios. It was complicated.
jmj.org <- 3975.77
ja.org <- 3250
gf.org <- 4139.18
gf.refund <- 2874.00
jmj.refund <- 2694.75
total.org <- jmj.org + ja.org + gf.org
total.refund <- gf.refund + jmj.refund
## If we split the whole refund evenly, what do we each get?
refund.pp <- total.refund / 7
## If we split the refund by percentage, what do we each get?
jmj.pct <- (jmj.org / total.org) / 3
ja.pct <- (ja.org / total.org) / 2
gf.pct <- (gf.org / total.org) / 2
refund.jmj <- total.refund * jmj.pct
refund.ja <- total.refund * ja.pct
refund.gf <- total.refund * gf.pct
## Check
(refund.jmj * 3) + (refund.ja * 2) + (refund.gf * 2)
## matrix of who owes who what: columns = is owed, rows = who pays
who.owes.who <- matrix(0, nrow = 5, ncol = 5)
rownames(who.owes.who) <- colnames(who.owes.who) <- c('ab', 'jl', 'jr', 'jt', 'mr')
## Total onboard credit, to be divided equally
ab.credit <- 0
jl.credit <- 25
jr.credit <- mr.credit <- 50
jt.credit <- 75
total.credit <- c(ab.credit, jl.credit, jr.credit, mr.credit, jt.credit)
credit.pp <- sum(total.credit) / 5
owes.credit <- total.credit - credit.pp
names(owes.credit) <- c('ab', 'jl', 'jr', 'mr', 'jt')
who.owes.who['jt', 'ab'] <- owes.credit['jt']
who.owes.who['mr', 'ab'] <- credit.pp - owes.credit['jt']
who.owes.who['mr', 'jl'] <- owes.credit['mr'] - who.owes.who['mr', 'ab']
who.owes.who['jr', 'jl'] <- owes.credit['jr']
## shuttle bus tickets
aaron.owes.jon <- 14
maegan.owes.jen <- 14
jamie.owes.jen <- 14
who.owes.who['ab', 'jl'] <- who.owes.who['mr', 'jt'] <- who.owes.who['jr', 'jt'] <- 14
## refund for port taxes
who.owes.who['jl', 'ab'] <- 27.19
who.owes.who['jt', 'mr'] <- who.owes.who['jt', 'jr'] <- 13.86
## add in splitwise
who.owes.who['jr', 'jt'] <- who.owes.who['jr', 'jt'] + 14.68
who.owes.who['jt', 'ab'] <- who.owes.who['jt', 'ab'] + 5.09
who.owes.who['jl', 'jt'] <- who.owes.who['jl', 'jt'] + 9.5
who.owes.who['mr', 'jt'] <- who.owes.who['mr', 'jt'] + 9.5
who.owes.who['jr', 'ab'] <- who.owes.who['jr', 'ab'] + 16.67
who.owes.who['jl', 'ab'] <- who.owes.who['jl', 'ab'] + 8.5
who.owes.who['mr', 'ab'] <- who.owes.who['mr', 'ab'] + 24.16
## Add in refunds ##
## Jamie has left to give...
jr.refund.left <- gf.refund - (refund.gf*2) - refund.jmj
who.owes.who['jr', 'ab'] <- who.owes.who['jr', 'ab'] + jr.refund.left
## Jen has left to give...
jt.refund.left <- jmj.refund - refund.jmj
who.owes.who['jt', 'ab'] <- who.owes.who['jt', 'ab'] + (refund.ja - jr.refund.left)
who.owes.who['jt', 'jl'] <- who.owes.who['jt', 'jl'] + refund.ja
who.owes.who['jt', 'mr'] <- who.owes.who['jt', 'mr'] + refund.jmj
## -- Final numbers -- ##
ab.owes.jl <- who.owes.who['ab', 'jl'] - who.owes.who['jl', 'ab']
ab.owes.jr <- who.owes.who['ab', 'jr'] - who.owes.who['jr', 'ab']
ab.owes.jt <- who.owes.who['ab', 'jt'] - who.owes.who['jt', 'ab']
ab.owes.mr <- who.owes.who['ab', 'mr'] - who.owes.who['mr', 'ab']
jl.owes.ab <- who.owes.who['jl', 'ab'] - who.owes.who['ab', 'jl']
jl.owes.jr <- who.owes.who['jl', 'jr'] - who.owes.who['jr', 'jl']
jl.owes.jt <- who.owes.who['jl', 'jt'] - who.owes.who['jt', 'jl']
jl.owes.mr <- who.owes.who['jl', 'mr'] - who.owes.who['mr', 'jl']
jr.owes.ab <- who.owes.who['jr', 'ab'] - who.owes.who['ab', 'jr']
jr.owes.jl <- who.owes.who['jr', 'jl'] - who.owes.who['jl', 'jr']
jr.owes.jt <- who.owes.who['jr', 'jt'] - who.owes.who['jt', 'jr']
jr.owes.mr <- who.owes.who['jr', 'mr'] - who.owes.who['mr', 'jr']
jt.owes.ab <- who.owes.who['jt', 'ab'] - who.owes.who['ab', 'jt']
jt.owes.jl <- who.owes.who['jt', 'jl'] - who.owes.who['jl', 'jt']
jt.owes.jr <- who.owes.who['jt', 'jr'] - who.owes.who['jr', 'jt']
jt.owes.mr <- who.owes.who['jt', 'mr'] - who.owes.who['mr', 'jt']
mr.owes.ab <- who.owes.who['mr', 'ab'] - who.owes.who['ab', 'mr']
mr.owes.jl <- who.owes.who['mr', 'jl'] - who.owes.who['jl', 'mr']
mr.owes.jr <- who.owes.who['mr', 'jr'] - who.owes.who['jr', 'mr']
mr.owes.jt <- who.owes.who['mr', 'jt'] - who.owes.who['jt', 'mr']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment