Advertisement
dimipan80

Vladko's Notebook (using dynamic type)

May 14th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. /* You are given a list of colored sheets given as text table with the following columns:
  2.  *     Option 01 – [color of the sheet]|[win/loss]|[opponent name]
  3.  *     Option 02 – [color of the sheet]|[name]|[player name]
  4.  *     Option 03 – [color of the sheet]|[age]|[player age]
  5.  * The different columns will always be separated only by 'I' (there won't be any whitespaces). The rank of each player is calculated by the formula rank = (wins+1) / (losses+1). If a certain color sheet has no information about the name or the age of the player, you should not print it in the output. If there is no information about the opponents, you must print "(empty)" where the opponents list should be. There might be many opponents with the same name. If there was no information recovered (no colors containing name and age), print only "No data recovered." The input comes from the console on a variable number of lines and ends when the keyword "END" is received. Print at the console the information for each player (sorted by color name) that holds the age, the name, a list with the opponents (in alphabetical order) and rank of the player.The rank of the players should be rounded to 2 digits after the decimal point. */
  6.  
  7. namespace Vladkos_Notebook
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Globalization;
  12.     using System.Threading;
  13.  
  14.     class VladkosNotebook
  15.     {
  16.         private static Dictionary<string, Dictionary<string, dynamic>> notebook;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  21.             notebook = new Dictionary<string, Dictionary<string, dynamic>>();
  22.  
  23.             string input = Console.ReadLine();
  24.             while (input != "END")
  25.             {
  26.                 GetInfoFromInput(input);
  27.                 input = Console.ReadLine();
  28.             }
  29.  
  30.             List<string> usableKeys = new List<string>();
  31.             foreach (var color in notebook)
  32.             {
  33.                 if (color.Value["age"] != string.Empty && color.Value["name"] != string.Empty)
  34.                 {
  35.                     color.Value["opponents"].Sort(StringComparer.Ordinal);
  36.                     color.Value["rank"] =
  37.                         CalculateColorRank(color.Value["win"], color.Value["loss"]);
  38.                     usableKeys.Add(color.Key);
  39.                 }
  40.             }
  41.  
  42.             if (usableKeys.Count == 0)
  43.             {
  44.                 Console.WriteLine("No data recovered.");
  45.                 return;
  46.             }
  47.  
  48.             usableKeys.Sort();
  49.             PrintSortedInformation(usableKeys);
  50.         }
  51.  
  52.         private static void GetInfoFromInput(string input)
  53.         {
  54.             string[] inputs = input.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  55.             string color = inputs[0];
  56.             if (!notebook.ContainsKey(color))
  57.             {
  58.                 notebook[color] = new Dictionary<string, dynamic>();
  59.                 notebook[color]["age"] = string.Empty;
  60.                 notebook[color]["name"] = string.Empty;
  61.                 notebook[color]["opponents"] = new List<string>();
  62.                 notebook[color]["rank"] = string.Empty;
  63.                 notebook[color]["win"] = 1;
  64.                 notebook[color]["loss"] = 1;
  65.             }
  66.  
  67.             if (inputs[1] == "win" || inputs[1] == "loss")
  68.             {
  69.                 notebook[color]["opponents"].Add(inputs[2]);
  70.                 notebook[color][inputs[1]] += 1;
  71.             }
  72.             else
  73.             {
  74.                 notebook[color][inputs[1]] = inputs[2];
  75.             }
  76.         }
  77.  
  78.         private static string CalculateColorRank(dynamic wins, dynamic loss)
  79.         {
  80.             double rank = int.Parse(wins.ToString()) / double.Parse(loss.ToString());
  81.             return string.Format("{0:F2}", rank);
  82.         }
  83.  
  84.         private static void PrintSortedInformation(List<string> keys)
  85.         {
  86.             foreach (string color in keys)
  87.             {
  88.                 Console.WriteLine("Color: {0}", color);
  89.                 Console.WriteLine("-age: {0}", notebook[color]["age"]);
  90.                 Console.WriteLine("-name: {0}", notebook[color]["name"]);
  91.                 string oppons = "(empty)";
  92.                 if (notebook[color]["opponents"].Count > 0)
  93.                 {
  94.                     oppons = string.Join(", ", notebook[color]["opponents"]);
  95.                 }
  96.                 Console.WriteLine("-opponents: {0}", oppons);
  97.                 Console.WriteLine("-rank: {0}", notebook[color]["rank"]);
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement