Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

Friends A, B, C, D go for a trip. They spend on various expenses. Cost of the expense is shared.

Example :

  • A spends 100 for breakfast for A, B, C and D
  • D spends 500 for cab for B and C
  • B spends 300 for lunch for A, B and C

Write a program to calculate how much each should get or each should give to one another. App should be scalable that number of friends can change

Friends A, B, C, D go for a trip. They spend on various expenses. Cost of the expense is shared.

Example :

  • A spends 100 for breakfast for A, B, C and D
  • D spends 500 for cab for B and C
  • B spends 300 for lunch for A, B and C

Write a program to calculate how much each should get or each should give to one another. App should be scalable that number of friends can change

Friends A, B, C, D go for a trip. They spend on various expenses. Cost of the expense is shared.

Example :

  • A spends 100 for breakfast for A, B, C and D
  • D spends 500 for cab for B and C
  • B spends 300 for lunch for A, B and C

Write a program to calculate how much each should get or each should give to one another. App should be scalable that number of friends can change

Combined all solutions into one that solves for every possible interpretation of the question.
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
using System;
using System.Collections.Generic;
namespace CodeReview
{
 class Transaction
 {
 static void Main(string[] args)
 {
 List<Tuple<char, int, List<char>>> transactions = new List<Tuple<char, int, List<char>>>()
 {
 Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }),
 Tuple.Create('D', 500, new List<char>() { 'B', 'C' }),
 Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' })
 };
 SolutionOne(transactions);
 Console.WriteLine();
 SolutionTwo(transactions);
 Console.WriteLine();
 SolutionThree(transactions);
 }
 // how much each person should get
 static void SolutionOne(List<Tuple<char, int, List<char>>>class transactions)Account
 {
 private int totalDebt = 0;
 private Dictionary<char, int> ledgerdebtors = new Dictionary<char, int>();
 foreachpublic Account(Tuple<char,char intinitialDebtor, List<char>> transaction inint transactionsinitialDebt)
 {
 int amountOwed = 0;
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 amountOwed += transaction.Item2;
 }
 }
 ledgerdebtors.Add(transaction.Item1initialDebtor, amountOwedinitialDebt);
 }
 foreachpublic (KeyValuePair<charDictionary<char, int> block in ledger)Debtors
 {
 Console.WriteLine("Userget
 " + block.Key + " is owed " + block.Value); {
 return debtors;
 }
 }

 Console.ReadKey();
 }

 // how much each person should give to one another
 static void SolutionTwo(List<Tuple<char, int, List<char>>> transactions)
 {
  Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char,public int, List<char>> transaction in transactions)TotalDebt
 {
 foreach (char beneficiary in transaction.Item3)get
 {
 ifforeach (!beneficiary.Equals(transactionchar debtor in debtors.Item1)Keys)
 {
 if (ledger.ContainsKey(beneficiary))
 {
 ledger[beneficiary]totalDebt += transaction.Item2;
 }
  else
 {
 ledger.Add(beneficiary, transaction.Item2);
 }debtors[debtor];
 }
 }
 }

 foreach (KeyValuePair<char, int> block in ledger)
  return {totalDebt;
 Console.WriteLine("User " + block.Key + " owes " + block.Value);}
 }

 Console.ReadKey();
 }
 // solution desired by @JanDotNet
 static void SolutionThreeMain(List<Tuple<char, int, List<char>>>string[] transactionsargs)
 {
 Dictionary<charQueue<Tuple<char, Dictionary<charint, int>>List<char>>> ledgertransactions = new Queue<Tuple<char, int, List<char>>>();
  Dictionary<char, Account> ledger = new Dictionary<char, int>>Account>();
 foreachtransactions.Enqueue(Tuple.Create('A', 100, new List<char>(Tuple<char) { 'A', int'B', List<char>>'C', transaction'D' in}));
 transactions.Enqueue(Tuple.Create('D', 500, new List<char>() { 'B', 'C' }));
 transactions.Enqueue(Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' }));
 while (transactions.Count > 0)
 {
 Tuple<char, int, List<char>> transaction = transactions.Dequeue();
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 Dictionary<char, int> debtors = ledger[beneficiary];ledger[beneficiary].Debtors;
 if (debtors.ContainsKey(transaction.Item1))
 {
 debtors[transaction.Item1] += transaction.Item2;
 }
 else
 {
 debtors.Add(transaction.Item1, transaction.Item2);
 }
 }
 else
 {
 Dictionary<charledger.Add(beneficiary, int> debtors = new Dictionary<char, int>();
  debtors.AddAccount(transaction.Item1, transaction.Item2);
 ledger.Add(beneficiary, debtors);
 }
 }
 }
 }
 foreach (KeyValuePair<char, Dictionary<char, int>>char blockbeneficiary in ledger.Keys)
 {
 Console.WriteLine("User " + block.Key + " owesAccount theaccount following:= ");ledger[beneficiary];
 Console.WriteLine("User " + beneficiary + " owes $" + account.TotalDebt + ":");
 foreach (KeyValuePair<char, int>char entrydebtor in blockaccount.ValueDebtors.Keys)
 {
 int debt = account.Debtors[debtor];
 Console.WriteLine(" - "$" + entry.Valuedebt + " to " + entry.Keydebtor);
 }
 }
 Console.ReadKey();
 }
 }
}
using System;
using System.Collections.Generic;
namespace CodeReview
{
 class Transaction
 {
 static void Main(string[] args)
 {
 List<Tuple<char, int, List<char>>> transactions = new List<Tuple<char, int, List<char>>>()
 {
 Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }),
 Tuple.Create('D', 500, new List<char>() { 'B', 'C' }),
 Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' })
 };
 SolutionOne(transactions);
 Console.WriteLine();
 SolutionTwo(transactions);
 Console.WriteLine();
 SolutionThree(transactions);
 }
 // how much each person should get
 static void SolutionOne(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 int amountOwed = 0;
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 amountOwed += transaction.Item2;
 }
 }
 ledger.Add(transaction.Item1, amountOwed);
 }
 foreach (KeyValuePair<char, int> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " is owed " + block.Value);
 }

 Console.ReadKey();
 }

 // how much each person should give to one another
 static void SolutionTwo(List<Tuple<char, int, List<char>>> transactions)
 {
  Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 ledger[beneficiary] += transaction.Item2;
 }
  else
 {
 ledger.Add(beneficiary, transaction.Item2);
 }
 }
 }
 }

 foreach (KeyValuePair<char, int> block in ledger)
  {
 Console.WriteLine("User " + block.Key + " owes " + block.Value);
 }

 Console.ReadKey();
 }
 // solution desired by @JanDotNet
 static void SolutionThree(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, Dictionary<char, int>> ledger = new Dictionary<char, Dictionary<char, int>>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 Dictionary<char, int> debtors = ledger[beneficiary];
 if (debtors.ContainsKey(transaction.Item1))
 {
 debtors[transaction.Item1] += transaction.Item2;
 }
 else
 {
 debtors.Add(transaction.Item1, transaction.Item2);
 }
 }
 else
 {
 Dictionary<char, int> debtors = new Dictionary<char, int>();
  debtors.Add(transaction.Item1, transaction.Item2);
 ledger.Add(beneficiary, debtors);
 }
 }
 }
 }
 foreach (KeyValuePair<char, Dictionary<char, int>> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " owes the following: ");
 foreach (KeyValuePair<char, int> entry in block.Value)
 {
 Console.WriteLine(" - " + entry.Value + " to " + entry.Key);
 }
 }
 Console.ReadKey();
 }
 }
}
using System;
using System.Collections.Generic;
namespace CodeReview
{
 class Transaction
 {
 class Account
 {
 private int totalDebt = 0;
 private Dictionary<char, int> debtors = new Dictionary<char, int>();
 public Account(char initialDebtor, int initialDebt)
 {
 debtors.Add(initialDebtor, initialDebt);
 }
 public Dictionary<char, int> Debtors
 {
 get
  {
 return debtors;
 }
 }
 public int TotalDebt
 {
 get
 {
 foreach (char debtor in debtors.Keys)
 {
 totalDebt += debtors[debtor];
 }
 return totalDebt;
 }
 }
 }
 static void Main(string[] args)
 {
 Queue<Tuple<char, int, List<char>>> transactions = new Queue<Tuple<char, int, List<char>>>();
  Dictionary<char, Account> ledger = new Dictionary<char, Account>();
 transactions.Enqueue(Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }));
 transactions.Enqueue(Tuple.Create('D', 500, new List<char>() { 'B', 'C' }));
 transactions.Enqueue(Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' }));
 while (transactions.Count > 0)
 {
 Tuple<char, int, List<char>> transaction = transactions.Dequeue();
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 Dictionary<char, int> debtors = ledger[beneficiary].Debtors;
 if (debtors.ContainsKey(transaction.Item1))
 {
 debtors[transaction.Item1] += transaction.Item2;
 }
 else
 {
 debtors.Add(transaction.Item1, transaction.Item2);
 }
 }
 else
 {
 ledger.Add(beneficiary, new Account(transaction.Item1, transaction.Item2));
 }
 }
 }
 }
 foreach (char beneficiary in ledger.Keys)
 {
 Account account = ledger[beneficiary];
 Console.WriteLine("User " + beneficiary + " owes $" + account.TotalDebt + ":");
 foreach (char debtor in account.Debtors.Keys)
 {
 int debt = account.Debtors[debtor];
 Console.WriteLine(" - $" + debt + " to " + debtor);
 }
 }
 Console.ReadKey();
 }
 }
}
Fixed loops for other solutions; fixed some indentation
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeReview
{
 class Transaction
 {
 static void Main(string[] args)
 {
 List<Tuple<char, int, List<char>>> transactions = new List<Tuple<char, int, List<char>>>()
 {
 Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }),
 Tuple.Create('D', 500, new List<char>() { 'B', 'C' }),
 Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' })
 };
 SolutionOne(transactions);
 Console.WriteLine();
 SolutionTwo(transactions);
 Console.WriteLine();
 SolutionThree(transactions);
 }
 // how much each person should get
 static void SolutionOne(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 int amountOwed = 0;
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 amountOwed += transaction.Item2;
 }
 }
 ledger.Add(transaction.Item1, amountOwed);
 }
 forforeach (int i =KeyValuePair<char, 0;int> iblock <in ledger.Count; ++i)
 {
 KeyValuePair<char, int> entry = ledger.ElementAt(i);
 Console.WriteLine("User " + entryblock.Key + " is owed " + entryblock.Value);
 }
 Console.ReadKey();
 }
 // how much each person should give to one another
 static void SolutionTwo(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 ledger[beneficiary] += transaction.Item2;
 }
 else
 {
 ledger.Add(beneficiary, transaction.Item2);
 }
 }
 }
 }
 forforeach (int i =KeyValuePair<char, 0;int> iblock <in ledger.Count; ++i)
 {
 KeyValuePair<char, int> entry = ledger.ElementAt(i);
 Console.WriteLine("User " + entryblock.Key + " owes " + entryblock.Value);
 }
 Console.ReadKey();
 }
 // solution desired by @JanDotNet
 static void SolutionThree(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, Dictionary<char, int>> ledger = new Dictionary<char, Dictionary<char, int>>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 Dictionary<char, int> debtors = ledger[beneficiary];
 if (debtors.ContainsKey(transaction.Item1))
 {
 debtors[transaction.Item1] += transaction.Item2;
 } else
 {
 debtors.Add(transaction.Item1, transaction.Item2);
 }
 } else
 {
 Dictionary<char, int> debtors = new Dictionary<char, int>();
 debtors.Add(transaction.Item1, transaction.Item2);
 ledger.Add(beneficiary, debtors);
 }
 }
 }
 }
 foreach (KeyValuePair<char, Dictionary<char, int>> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " owes the following: ");
 foreach (KeyValuePair<char, int> entry in block.Value)
 {
 Console.WriteLine(" - " + entry.Value + " to " + entry.Key);
 }
 }
 Console.ReadKey();
 }
 }
}
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeReview
{
 class Transaction
 {
 static void Main(string[] args)
 {
 List<Tuple<char, int, List<char>>> transactions = new List<Tuple<char, int, List<char>>>()
 {
 Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }),
 Tuple.Create('D', 500, new List<char>() { 'B', 'C' }),
 Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' })
 };
 SolutionOne(transactions);
 Console.WriteLine();
 SolutionTwo(transactions);
 Console.WriteLine();
 SolutionThree(transactions);
 }
 // how much each person should get
 static void SolutionOne(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 int amountOwed = 0;
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 amountOwed += transaction.Item2;
 }
 }
 ledger.Add(transaction.Item1, amountOwed);
 }
 for (int i = 0; i < ledger.Count; ++i)
 {
 KeyValuePair<char, int> entry = ledger.ElementAt(i);
 Console.WriteLine("User " + entry.Key + " is owed " + entry.Value);
 }
 Console.ReadKey();
 }
 // how much each person should give to one another
 static void SolutionTwo(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 ledger[beneficiary] += transaction.Item2;
 }
 else
 {
 ledger.Add(beneficiary, transaction.Item2);
 }
 }
 }
 }
 for (int i = 0; i < ledger.Count; ++i)
 {
 KeyValuePair<char, int> entry = ledger.ElementAt(i);
 Console.WriteLine("User " + entry.Key + " owes " + entry.Value);
 }
 Console.ReadKey();
 }
 static void SolutionThree(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, Dictionary<char, int>> ledger = new Dictionary<char, Dictionary<char, int>>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 Dictionary<char, int> debtors = ledger[beneficiary];
 if (debtors.ContainsKey(transaction.Item1))
 {
 debtors[transaction.Item1] += transaction.Item2;
 } else
 {
 debtors.Add(transaction.Item1, transaction.Item2);
 }
 } else
 {
 Dictionary<char, int> debtors = new Dictionary<char, int>();
 debtors.Add(transaction.Item1, transaction.Item2);
 ledger.Add(beneficiary, debtors);
 }
 }
 }
 }
 foreach (KeyValuePair<char, Dictionary<char, int>> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " owes the following: ");
 foreach (KeyValuePair<char, int> entry in block.Value)
 {
 Console.WriteLine(" - " + entry.Value + " to " + entry.Key);
 }
 }
 Console.ReadKey();
 }
 }
}
using System;
using System.Collections.Generic;
namespace CodeReview
{
 class Transaction
 {
 static void Main(string[] args)
 {
 List<Tuple<char, int, List<char>>> transactions = new List<Tuple<char, int, List<char>>>()
 {
 Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }),
 Tuple.Create('D', 500, new List<char>() { 'B', 'C' }),
 Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' })
 };
 SolutionOne(transactions);
 Console.WriteLine();
 SolutionTwo(transactions);
 Console.WriteLine();
 SolutionThree(transactions);
 }
 // how much each person should get
 static void SolutionOne(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 int amountOwed = 0;
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 amountOwed += transaction.Item2;
 }
 }
 ledger.Add(transaction.Item1, amountOwed);
 }
 foreach (KeyValuePair<char, int> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " is owed " + block.Value);
 }
 Console.ReadKey();
 }
 // how much each person should give to one another
 static void SolutionTwo(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, int> ledger = new Dictionary<char, int>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 ledger[beneficiary] += transaction.Item2;
 }
 else
 {
 ledger.Add(beneficiary, transaction.Item2);
 }
 }
 }
 }
 foreach (KeyValuePair<char, int> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " owes " + block.Value);
 }
 Console.ReadKey();
 }
 // solution desired by @JanDotNet
 static void SolutionThree(List<Tuple<char, int, List<char>>> transactions)
 {
 Dictionary<char, Dictionary<char, int>> ledger = new Dictionary<char, Dictionary<char, int>>();
 foreach (Tuple<char, int, List<char>> transaction in transactions)
 {
 foreach (char beneficiary in transaction.Item3)
 {
 if (!beneficiary.Equals(transaction.Item1))
 {
 if (ledger.ContainsKey(beneficiary))
 {
 Dictionary<char, int> debtors = ledger[beneficiary];
 if (debtors.ContainsKey(transaction.Item1))
 {
 debtors[transaction.Item1] += transaction.Item2;
 } else
 {
 debtors.Add(transaction.Item1, transaction.Item2);
 }
 } else
 {
 Dictionary<char, int> debtors = new Dictionary<char, int>();
 debtors.Add(transaction.Item1, transaction.Item2);
 ledger.Add(beneficiary, debtors);
 }
 }
 }
 }
 foreach (KeyValuePair<char, Dictionary<char, int>> block in ledger)
 {
 Console.WriteLine("User " + block.Key + " owes the following: ");
 foreach (KeyValuePair<char, int> entry in block.Value)
 {
 Console.WriteLine(" - " + entry.Value + " to " + entry.Key);
 }
 }
 Console.ReadKey();
 }
 }
}
Added third solution for @JanDotNet
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
Loading
Use Tuple.Create() over explicit declaration
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
Loading
Unified shared data into a "static" list as opposed to separate "dynamic" queues
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
Loading
added problem descriptions, removed noise
Source Link
t3chb0t
  • 44.6k
  • 9
  • 84
  • 190
Loading
Revised design block, since we're answering both questions now
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
Loading
Source Link
T145
  • 3.1k
  • 2
  • 23
  • 44
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /