Advertisement
kirilstanoev

Untitled

Aug 27th, 2021
1,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace B
  6. {
  7.     public class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             Dictionary<string, SortedSet<Pokemon>> pokemonTypes = new Dictionary<string, SortedSet<Pokemon>>();
  12.             // има нужда от 2 листа - 1 с всички покемони и един само с позициите
  13.             List<Pokemon> allPokemons = new List<Pokemon>();
  14.             List<int> positions = new List<int>();
  15.  
  16.             var sb = new StringBuilder();
  17.             var input = "";
  18.             while (input != "end")
  19.             {
  20.                 input = Console.ReadLine();
  21.  
  22.                 string[] inputTokens = input.Split();
  23.  
  24.                 string commandType = inputTokens[0];
  25.  
  26.                 if (commandType == "add")
  27.                 {
  28.                     string name = inputTokens[1];
  29.                     string type = inputTokens[2];
  30.                     int power = int.Parse(inputTokens[3]);
  31.                     int position = int.Parse(inputTokens[4]);
  32.  
  33.                     var pokemon = new Pokemon(name, type, power);
  34.  
  35.                     // намаляме позицията с 1 преди да я insert-нем в листа с позиции,
  36.                     // защото позициите са 1,2,3, а листът е 0-based
  37.                     positions.Insert(position - 1, allPokemons.Count);
  38.                     allPokemons.Add(pokemon);
  39.                    
  40.                     // нямаш нужда от това
  41.                     //if (position < allPokemons.Count)
  42.                     //{
  43.                     //    allPokemons.Insert(position - 1, pokemon);
  44.                     //}
  45.                     //else
  46.                     //{
  47.                     //    allPokemons.Add(pokemon);
  48.  
  49.                     //}
  50.  
  51.                     // TODO: Optimize this
  52.                     if (!pokemonTypes.ContainsKey(type))
  53.                     {
  54.                         pokemonTypes[type] = new SortedSet<Pokemon>();
  55.                     }
  56.                     pokemonTypes[type].Add(pokemon);
  57.  
  58.                     //result = $"Added pokemon {name} to position {position}";
  59.                     sb.AppendLine($"Added pokemon {name} to position {position}");
  60.                 }
  61.  
  62.                 else if (commandType == "find")
  63.                 {
  64.                     string type = inputTokens[1];
  65.  
  66.                     sb.Append($"Type {type}: ");
  67.  
  68.                     if (pokemonTypes.ContainsKey(type))
  69.                     {
  70.                         //var pokemonsByType = pokemons[type].Take(5)
  71.                         //.Select(x => $"{x.Name}({x.Power})")
  72.                         //.ToList();
  73.                         sb.Append(string.Join("; ", pokemonTypes[type].Take(5).ToList()));
  74.                     }
  75.  
  76.                     sb.AppendLine();
  77.  
  78.                     //result = pokemonsByType.Count == 0 ? $"Type {typeToFind}: " : $"Type {typeToFind}: {string.Join("; ", pokemonsByType)}";
  79.                 }
  80.  
  81.                 else if (commandType == "ranklist")
  82.                 {
  83.                     // К
  84.                     var start = int.Parse(inputTokens[1]);
  85.                     var end = int.Parse(inputTokens[2]);
  86.                     for (int i = start - 1; i < end; i++)
  87.                     {
  88.                         sb.Append($"{i + 1}. {allPokemons[positions[i]]}; ");
  89.                     }
  90.                     sb.Remove(sb.Length - 2, 2);
  91.                     sb.AppendLine();
  92.  
  93.                     // Б
  94.                     //int start = int.Parse(inputTokens[1]);
  95.                     //int end = int.Parse(inputTokens[2]);
  96.                     //StringBuilder sbPrintPositions = new StringBuilder();
  97.  
  98.                     //int counter = start;
  99.  
  100.                     //for (int i = start - 1; i <= end - 1; i++)
  101.                     //{
  102.                     //    sbPrintPositions.Append($"{counter}. {pokemonsByPositions[i].Name}({pokemonsByPositions[i].Power}); ");
  103.                     //    counter++;
  104.                     //}
  105.  
  106.                     //result = sbPrintPositions.ToString().TrimEnd(';', ' ');
  107.  
  108.                 }
  109.                 //sb.AppendLine(result);
  110.  
  111.             }
  112.             Console.WriteLine(sb.ToString());
  113.         }
  114.     }
  115.  
  116.     class Pokemon : IComparable<Pokemon>
  117.     {
  118.  
  119.         public Pokemon(string name, string type, int power)
  120.         {
  121.             this.Name = name;
  122.             this.Type = type;
  123.             this.Power = power;
  124.         }
  125.  
  126.         public string Name { get; set; }
  127.         public string Type { get; set; }
  128.         public int Power { get; set; }
  129.         public int CompareTo(Pokemon other)
  130.         {
  131.             if (other == null)
  132.                 return -1;
  133.  
  134.             int comparison = this.Name.CompareTo(other.Name);
  135.  
  136.             if (comparison == 0)
  137.             {
  138.                 comparison = this.Power.CompareTo(other.Power);
  139.                 comparison *= -1; // Descending order
  140.             }
  141.  
  142.             return comparison;
  143.         }
  144.  
  145.         public override string ToString()
  146.         {
  147.             return $"{this.Name}({this.Power})";
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement