Advertisement
Guest User

Untitled

a guest
Mar 12th, 2022
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Wardrobe
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int inputCount = int.Parse(Console.ReadLine());
  12.  
  13.             // key - color
  14.             // value - Dictionary<item, itemCount>
  15.             Dictionary<string, Dictionary<string, int>> wardrobe = new Dictionary<string, Dictionary<string, int>>();
  16.  
  17.             for (int i = 0; i < inputCount; i++)
  18.             {
  19.                 string[] colorAndClothes = Console.ReadLine()
  20.                                                   .Split(" -> ", StringSplitOptions.RemoveEmptyEntries)
  21.                                                   .ToArray();
  22.  
  23.                 string[] clothes = colorAndClothes[1].Split(",", StringSplitOptions.RemoveEmptyEntries)
  24.                                                      .ToArray();
  25.  
  26.                 if (wardrobe.ContainsKey(colorAndClothes[0]) == false) // if given color doesn't exist
  27.                 {
  28.                     wardrobe.Add(colorAndClothes[0], new Dictionary<string, int>()); // add color
  29.  
  30.                     for (int j = 0; j < clothes.Length; j++) // add items with this color
  31.                     {
  32.                          if (!wardrobe[colorAndClothes[0]].ContainsKey(clothes[j])) //here
  33.                         {                                                           //here
  34.                             wardrobe[colorAndClothes[0]].Add(clothes[j], 1);        //here
  35.                             continue;                                               //here
  36.                         }                                                           //here
  37.                         wardrobe[colorAndClothes[0]][clothes[j]]++;                 //here
  38.                     }
  39.                 }
  40.                 else
  41.                 {
  42.                     for (int j = 0; j < clothes.Length; j++)
  43.                     {
  44.                         if (wardrobe[colorAndClothes[0]].ContainsKey(clothes[j]) == false) // if current item doesn't exist - add item
  45.                         {
  46.                             wardrobe[colorAndClothes[0]].Add(clothes[j], 1);
  47.                         }
  48.                         else
  49.                         {
  50.                             wardrobe[colorAndClothes[0]][clothes[j]] += 1; // else - itemCount++
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             // Receive from user item to search for in the wardrobe
  57.             string[] searchFor = Console.ReadLine()
  58.                                         .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  59.                                         .ToArray();
  60.  
  61.             // Print result
  62.             foreach (var kvp in wardrobe)
  63.             {
  64.                 Console.WriteLine($"{kvp.Key} clothes:");
  65.                 foreach (var internalKVP in kvp.Value)
  66.                 {
  67.                     if (kvp.Key == searchFor[0] && internalKVP.Key == searchFor[1])
  68.                     {
  69.                         Console.WriteLine($"* {internalKVP.Key} - {internalKVP.Value} (found!)");
  70.                     }
  71.                     else
  72.                     {
  73.                         Console.WriteLine($"* {internalKVP.Key} - {internalKVP.Value}");
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement