Advertisement
grubcho

ShellBound - Nested dict

Jul 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /*  Vladi is a crab. Crabs are scared from almost anything, which is why they usually hide in their shells. Vladi is a rich crab //though.
  7.         //He has acquired many outer shells, in different regions, in which he can hide – each with a different size.
  8.         //However, it is really annoying to look after all your shells when they are so spread. That is why, Vladi decided to merge all //shells
  9.         //in each region, so that he has one big shell for every region. He needs your help to do that.
  10.         //You will be given several input lines containing a string and an integer, separated by a space. The string will represent the //region’s
  11.         //name, and the integer – the shell, in the given region, size.
  12.         //You must store all shells in their corresponding regions.
  13.         //If the region already exists, add the new shell to it. Make sure you have NO duplicate shells (shells with same size). Vladi //doesn’t like shells with the same size.
  14.         //When you receive the command “Aggregate”, you must stop reading input lines, and you must print every region, all of the //shells in that
  15.         //region, and the size of the giant shell after you’ve merged them in the following format:
  16.         //{regionName} -> {shell1, shell2, shell3…} ({giantShell})
  17.         //The giant shell size is calculated when you subtract the average of the shells from the sum of the shells.
  18.         //Or in other words: (sum of shells) – (sum of shells) / (count of shells). */
  19. namespace ShellBound
  20. {
  21.     class Program
  22.     {
  23.         static void Main(string[] args)
  24.         {
  25.             Dictionary<string, List<int>> register = new Dictionary<string, List<int>>();
  26.             string inputline = Console.ReadLine();
  27.             while (inputline != "Aggregate")
  28.             {
  29.                 var inputData = inputline.Split(' ');
  30.                 var currentKey = inputData[0];
  31.                 var currentValue = int.Parse(inputData[1]);
  32.                 if (!register.ContainsKey(currentKey))
  33.                 {
  34.                     register[currentKey] = new List<int>();
  35.                     register[currentKey].Add(currentValue);
  36.                 }
  37.                 else
  38.                 {
  39.                     if (!register[currentKey].Contains(currentValue))
  40.                     {
  41.                         register[currentKey].Add(currentValue);
  42.                     }
  43.                 }
  44.                 inputline = Console.ReadLine();
  45.             }
  46.             foreach (var entry in register)
  47.             {
  48.                 var entryValues = entry.Value;
  49.                 var sum = 0;
  50.                 for (int i = 0; i < entryValues.Count; i++)
  51.                 {
  52.                     sum += entryValues[i];
  53.                 }
  54.                 var bigShell = 0;
  55.                 if (entryValues.Count < 2)
  56.                 {
  57.                     bigShell = sum;
  58.                 }
  59.                 else
  60.                 {
  61.                     bigShell = sum - (sum / entryValues.Count);
  62.                 }
  63.  
  64.                 Console.WriteLine($"{entry.Key} -> {string.Join(", ", entryValues)} ({bigShell})");
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement