Advertisement
Filkolev

Vladko's Notebook (C#)

May 10th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. public class VladkosNotebook
  6. {
  7.     public static void Main()
  8.     {
  9.         var notebook = new SortedDictionary<string, Dictionary<string, dynamic>>();
  10.         string input = Console.ReadLine();
  11.  
  12.         while (input != "END")
  13.         {
  14.             string[] tokens = input.Split('|');
  15.  
  16.             string color = tokens[0];
  17.             string property = tokens[1];
  18.             string value = tokens[2];
  19.  
  20.             if (!notebook.ContainsKey(color))
  21.             {
  22.                 notebook.Add(color, new Dictionary<string, dynamic>());
  23.                 notebook[color].Add("opponents", new List<string>());
  24.                 notebook[color].Add("wins", 0);
  25.                 notebook[color].Add("losses", 0);
  26.             }
  27.  
  28.             if (property == "win")
  29.             {
  30.                 notebook[color]["wins"]++;
  31.                 notebook[color]["opponents"].Add(value);
  32.             }
  33.             else if (property == "loss")
  34.             {
  35.                 notebook[color]["losses"]++;
  36.                 notebook[color]["opponents"].Add(value);
  37.             }
  38.             else if (property == "age" && !notebook[color].ContainsKey("age"))
  39.             {
  40.                 notebook[color].Add("age", value);
  41.             }
  42.             else if(property == "name" && !notebook[color].ContainsKey("name"))
  43.             {
  44.                 notebook[color].Add("name", value);
  45.             }
  46.  
  47.             input = Console.ReadLine();
  48.         }
  49.  
  50.         var result = new SortedDictionary<string, Dictionary<string, dynamic>>();
  51.  
  52.         foreach (var entry in notebook)
  53.         {
  54.             if (!entry.Value.ContainsKey("name") || !entry.Value.ContainsKey("age"))
  55.             {
  56.                 continue;
  57.             }
  58.  
  59.             var rank = ((double)entry.Value["wins"] + 1) / (entry.Value["losses"] + 1);
  60.             rank = string.Format("{0:F2}", rank);
  61.  
  62.             var opponents = entry.Value["opponents"].ToArray();
  63.             Array.Sort(opponents, StringComparer.Ordinal);
  64.  
  65.             result.Add(entry.Key, new Dictionary<string, dynamic>());
  66.  
  67.             result[entry.Key].Add("age", entry.Value["age"]);
  68.             result[entry.Key].Add("name", entry.Value["name"]);
  69.             result[entry.Key].Add("opponents", opponents);
  70.             result[entry.Key].Add("rank", rank.ToString());
  71.         }
  72.  
  73.         StringBuilder output = new StringBuilder();
  74.         output.Append("{");
  75.  
  76.         foreach (var pair in result)
  77.         {
  78.             output.AppendFormat("\"{0}\":{{", pair.Key);
  79.             output.AppendFormat("\"age\":\"{0}\",", pair.Value["age"]);
  80.             output.AppendFormat("\"name\":\"{0}\",", pair.Value["name"]);
  81.  
  82.             var opponents = string.Join(",", pair.Value["opponents"]);
  83.             opponents = opponents.Replace(",", "\",\"");
  84.  
  85.             if (opponents.Length > 0)
  86.             {
  87.                 opponents = string.Format("[\"{0}\"]", opponents);
  88.             }
  89.             else
  90.             {
  91.                 opponents = "[]";
  92.             }
  93.  
  94.             output.AppendFormat("\"opponents\":{0},", opponents);
  95.             output.AppendFormat("\"rank\":\"{0}\"", pair.Value["rank"]);
  96.             output.Append("},");
  97.         }
  98.  
  99.         if (output.Length > 1)
  100.         {
  101.             output.Remove(output.Length - 1, 1);
  102.         }
  103.        
  104.         output.Append("}");
  105.  
  106.         Console.WriteLine(output);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement