elena1234

Wardrobe-with ternary operator

Jan 5th, 2021 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Wardrobe
  5. {
  6.     static class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int numberOfLines = int.Parse(Console.ReadLine());
  11.             var dictColorClothesCount = new Dictionary<string, Dictionary<string, int>>();
  12.             for (int i = 0; i < numberOfLines; i++)
  13.             {
  14.                 string[] input = Console.ReadLine().Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
  15.                 string colour = input[0];
  16.                 string[] secondPartWithClothes = input[1].Split(',');
  17.                 foreach (var cloth in secondPartWithClothes)
  18.                 {
  19.                     if (!dictColorClothesCount.ContainsKey(colour))
  20.                     {
  21.                         dictColorClothesCount[colour] = new Dictionary<string, int>();
  22.                         dictColorClothesCount[colour].Add(cloth, 1);
  23.                     }
  24.  
  25.                     else if (dictColorClothesCount.ContainsKey(colour))
  26.                     {
  27.                         if (!dictColorClothesCount[colour].ContainsKey(cloth))
  28.                         {
  29.                             dictColorClothesCount[colour].Add(cloth, 0);
  30.                         }
  31.  
  32.                         dictColorClothesCount[colour][cloth]++;
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             string[] inputLookingFor = Console.ReadLine().Split();
  38.             string colorLookingFor = inputLookingFor[0];
  39.             string clothLookingFor = inputLookingFor[1];
  40.             foreach (var (color, clothesCount) in dictColorClothesCount)
  41.             {
  42.                 Console.WriteLine($"{color} clothes:");
  43.                 foreach (var (cloth, count) in clothesCount)
  44.                 {
  45.                     Console.WriteLine(color != colorLookingFor || cloth != clothLookingFor
  46.                         ? $"* {cloth} - {count}"
  47.                         : $" * {cloth} - {count} (found!)");
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
Add Comment
Please, Sign In to add comment