nikiman8888

CODE: Phoenix Oscar Romeo November - Exam

May 27th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace lab4.PhoenixOscarRomeoNovember
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();  
  14.            
  15.             var creatureData = new Dictionary<string, HashSet<string>>(); // with hashset we have only unique squads
  16.             var temp = new Dictionary<string, HashSet<string>>();
  17.  
  18.             while (input != "Blaze it!")  //start to collect the creature and squads
  19.             {
  20.                 string[] tokens = input.Split(new String[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
  21.  
  22.                 string creature = tokens[0];
  23.                 string squad = tokens[1];
  24.  
  25.                 if (creature == squad)
  26.                 {
  27.                     input = Console.ReadLine();
  28.                     continue;
  29.                 }
  30.  
  31.                  if (!creatureData.ContainsKey(creature))
  32.                 {
  33.                     creatureData.Add(creature, new HashSet<string>());
  34.                     temp.Add(creature, new HashSet<string>());
  35.                 }
  36.                 creatureData[creature].Add(squad);
  37.  
  38.  
  39.                 input = Console.ReadLine();
  40.             }
  41.  
  42.             foreach (var datas in creatureData)     //start to collect in the temp Dict only the squads we need
  43.             {
  44.                 string creatureName = datas.Key;
  45.  
  46.                 HashSet<string> squads = datas.Value;
  47.  
  48.                 var tempHash = new HashSet<string>();
  49.  
  50.                 foreach (var squad in squads)
  51.                 {
  52.                     if (SquadExistInCreatureKeys(squad,temp))
  53.                     {
  54.                         if (CreatureNameExistInSquadKey(creatureData[squad], creatureName))
  55.  
  56.                         {
  57.                             continue;
  58.                         }
  59.                         else
  60.                         {
  61.                             tempHash.Add(squad);
  62.                         }
  63.                        
  64.                     }
  65.                     else
  66.                     {
  67.                         tempHash.Add(squad);
  68.                     }
  69.                 }
  70.                 foreach (var item in tempHash)
  71.                 {
  72.                     temp[creatureName].Add(item);
  73.                 }
  74.                
  75.  
  76.             }
  77.                                    
  78.             foreach (var creature in temp.OrderByDescending(x =>x.Value.Count())) // start to output the result from temp Dict..
  79.             {
  80.                 string creatur = creature.Key;
  81.                 HashSet<string> squadCount = creature.Value;
  82.                
  83.                 Console.WriteLine("{0} : {1}",creatur,squadCount.Count());
  84.             }
  85.         }
  86.  
  87.         private static bool CreatureNameExistInSquadKey(HashSet<string> hashSet, string creatureName)
  88.         {
  89.             foreach (var squad in hashSet)
  90.             {
  91.                 if (squad == creatureName)
  92.                 {
  93.                     return true;
  94.                 }
  95.             }
  96.             return false;
  97.         }
  98.  
  99.         private static bool SquadExistInCreatureKeys(string squad, Dictionary<string, HashSet<string>> temp)
  100.         {
  101.             foreach (var key in temp.Keys)
  102.             {
  103.                 if (key == squad) return true;
  104.             }
  105.             return false;
  106.    
  107.         }
  108.     }
  109. }
Add Comment
Please, Sign In to add comment