I want to resolve this R computing problem but im not getting what to do

Viewed 18

Splitwise is a popular app that allows a group of people to settle debts to one another after multiple transactions where, in each transaction, one person pays for the group. Suppose Ross and Lily are joined by Alice and the three people go on a trip. Alice, Lily, and Ross each pay for separate meals that the three agree to split evenly. I.e., Alice pays X for meal 1, Lily pays Y for meal 2, and Ross pays Z for meal 3, and they agree to settle debts so that everyone pays (X+Y+Z)/3 by the end of the trip. Write a function named splitwise that takes in arguments for how much Alice, Lily, and Ross spent on their meals and outputs the withdraws and transfers such that the three settle up their debts to each other in the most efficient way possible.

First, you will need to withdraw the amount each have spent on meals. Then, transfer money around in the least number of transactions such that debts are settled. Kudos for efficient coding here! Use of many if statements is OK.

Note: You will need to consider the following 3 cases:

  1. Where only one person needs to pay one other person.
  2. Where one person needs to pay two people.
  3. Where two people need to pay one person.
splitwise <- function(Alice, Lily, Ross) {
  x<-((Alice+Lily+Ross)/3)- Alice #Alice withdraw x from her account
  y<-((Alice+Lily+Ross)/3)- Lily  #Lily withdraw y from her account
  z<-((Alice+Lily+Ross)/3)- ross  #Ross withdraw z from his account
  
}
0 Answers
Related