Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TestMHT
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, double> owes = new Dictionary<string, double>
  12.             {
  13.                 { "Manta Ray", 84.86 },
  14.                 { "Peter", -91.15 },
  15.                 { "Dan O", -69.12 },
  16.                 { "Goose", -5.73 },
  17.                 { "Brandon", 41.11 },
  18.                 { "Big L", 40.02 }
  19.             };
  20.  
  21.             Dictionary<string, double> peopleOwed = new Dictionary<string, double>();
  22.             Dictionary<string, double> peopleWhoOwe = new Dictionary<string, double>();
  23.  
  24.             List<string> txnLog = new List<string>();
  25.            
  26.             foreach (var kvp in owes)
  27.             {
  28.                 if (kvp.Value > 0)
  29.                 {
  30.                     peopleOwed.Add(kvp.Key, kvp.Value);
  31.                 }
  32.                 else
  33.                 {
  34.                     peopleWhoOwe.Add(kvp.Key, -kvp.Value);
  35.                 }
  36.             }
  37.  
  38.             while (CalcFullOwes(ref peopleOwed, ref peopleWhoOwe, ref txnLog)) ;
  39.  
  40.             foreach (var s in txnLog)
  41.             {
  42.                 Console.WriteLine(s);
  43.             }
  44.  
  45.             foreach (var kvp in peopleOwed)
  46.             {
  47.                 Console.WriteLine("{0} still owed {1}", kvp.Key, kvp.Value);
  48.             }
  49.  
  50.             foreach (var kvp in peopleWhoOwe)
  51.             {
  52.                 Console.WriteLine("{0} still owes {1}", kvp.Key, kvp.Value);
  53.             }
  54.  
  55.             Console.ReadKey();
  56.         }
  57.  
  58.  
  59.         public static bool CalcFullOwes(ref Dictionary<string, double> peopleOwed, ref Dictionary<string, double> peopleWhoOwe, ref List<string> txnLog)
  60.         {
  61.             foreach (var owedKvp in peopleOwed.OrderByDescending(x => x.Value))
  62.             {
  63.                 // Take smallest owers that can insert their full value into the current owe
  64.                 foreach (var owerKvp in peopleWhoOwe.OrderBy(x => x.Value))
  65.                 {
  66.                     if (owerKvp.Value < owedKvp.Value)
  67.                     {
  68.                         txnLog.Add(string.Format("{0} pays {1} ${2}", owerKvp.Key, owedKvp.Key, owerKvp.Value));
  69.                         peopleOwed[owedKvp.Key] = peopleOwed[owedKvp.Key] - peopleWhoOwe[owerKvp.Key];
  70.                         peopleWhoOwe.Remove(owerKvp.Key);
  71.                         if (peopleOwed[owedKvp.Key] < 0.001)
  72.                         {
  73.                             peopleOwed.Remove(owedKvp.Key);
  74.                         }
  75.                         return true;
  76.                     }
  77.                 }
  78.             }
  79.  
  80.             // Now calculate splits
  81.             foreach (var owedKvp in peopleOwed.OrderByDescending(x => x.Value))
  82.             {
  83.                 // Take biggest owers and split
  84.                 foreach (var owerKvp in peopleWhoOwe.OrderByDescending(x => x.Value))
  85.                 {
  86.                     if (owerKvp.Value > owedKvp.Value)
  87.                     {
  88.                         double amountToPay = owedKvp.Value;
  89.                         txnLog.Add(string.Format("{0} pays {1} ${2}", owerKvp.Key, owedKvp.Key, amountToPay));
  90.                         peopleOwed.Remove(owedKvp.Key);
  91.                         peopleWhoOwe[owerKvp.Key] -= amountToPay;
  92.                         if (peopleWhoOwe[owerKvp.Key] < 0.001)
  93.                         {
  94.                             peopleWhoOwe.Remove(owerKvp.Key);
  95.                         }
  96.                         return true;
  97.                     }
  98.                 }
  99.             }
  100.  
  101.  
  102.             return false;
  103.         }
  104.     }
  105. }
  106.  
  107. // Console Output
  108. // Goose pays Manta Ray $5.73
  109. // Dan O pays Manta Ray $69.12
  110. // Peter pays Brandon $41.11
  111. // Peter pays Big L $40.02
  112. // Peter pays Manta Ray $10.01
  113. // Peter still owes 0.0100000000000122
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement