Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- public class VladkosNotebook
- {
- public static void Main()
- {
- var notebook = new SortedDictionary<string, Dictionary<string, dynamic>>();
- string input = Console.ReadLine();
- while (input != "END")
- {
- string[] tokens = input.Split('|');
- string color = tokens[0];
- string property = tokens[1];
- string value = tokens[2];
- if (!notebook.ContainsKey(color))
- {
- notebook.Add(color, new Dictionary<string, dynamic>());
- notebook[color].Add("opponents", new List<string>());
- notebook[color].Add("wins", 0);
- notebook[color].Add("losses", 0);
- }
- if (property == "win")
- {
- notebook[color]["wins"]++;
- notebook[color]["opponents"].Add(value);
- }
- else if (property == "loss")
- {
- notebook[color]["losses"]++;
- notebook[color]["opponents"].Add(value);
- }
- else if (property == "age" && !notebook[color].ContainsKey("age"))
- {
- notebook[color].Add("age", value);
- }
- else if(property == "name" && !notebook[color].ContainsKey("name"))
- {
- notebook[color].Add("name", value);
- }
- input = Console.ReadLine();
- }
- var result = new SortedDictionary<string, Dictionary<string, dynamic>>();
- foreach (var entry in notebook)
- {
- if (!entry.Value.ContainsKey("name") || !entry.Value.ContainsKey("age"))
- {
- continue;
- }
- var rank = ((double)entry.Value["wins"] + 1) / (entry.Value["losses"] + 1);
- rank = string.Format("{0:F2}", rank);
- var opponents = entry.Value["opponents"].ToArray();
- Array.Sort(opponents, StringComparer.Ordinal);
- result.Add(entry.Key, new Dictionary<string, dynamic>());
- result[entry.Key].Add("age", entry.Value["age"]);
- result[entry.Key].Add("name", entry.Value["name"]);
- result[entry.Key].Add("opponents", opponents);
- result[entry.Key].Add("rank", rank.ToString());
- }
- StringBuilder output = new StringBuilder();
- output.Append("{");
- foreach (var pair in result)
- {
- output.AppendFormat("\"{0}\":{{", pair.Key);
- output.AppendFormat("\"age\":\"{0}\",", pair.Value["age"]);
- output.AppendFormat("\"name\":\"{0}\",", pair.Value["name"]);
- var opponents = string.Join(",", pair.Value["opponents"]);
- opponents = opponents.Replace(",", "\",\"");
- if (opponents.Length > 0)
- {
- opponents = string.Format("[\"{0}\"]", opponents);
- }
- else
- {
- opponents = "[]";
- }
- output.AppendFormat("\"opponents\":{0},", opponents);
- output.AppendFormat("\"rank\":\"{0}\"", pair.Value["rank"]);
- output.Append("},");
- }
- if (output.Length > 1)
- {
- output.Remove(output.Length - 1, 1);
- }
- output.Append("}");
- Console.WriteLine(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement