Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PokemonCatchThemAll
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- LinkedList<Pokemon> pokemon = new LinkedList<Pokemon>();
- StringBuilder output = new StringBuilder();
- string input;
- while ((input = Console.ReadLine()) != "end")
- {
- var parameters = input.Split(' ');
- string command = parameters[0];
- if (command == "add")
- {
- string name = parameters[1];
- string type = parameters[2];
- int power = int.Parse(parameters[3]);
- int position = int.Parse(parameters[4]);
- Pokemon newPokemon = new Pokemon(name, type, power);
- if (position == 1)
- {
- pokemon.AddFirst(newPokemon);
- }
- else if (position == pokemon.Count + 1)
- {
- pokemon.AddLast(newPokemon);
- }
- else
- {
- if (position > pokemon.Count / 2)
- {
- var node = pokemon.Last;
- for (var i = pokemon.Count; i > position; i--)
- {
- node = node.Previous;
- }
- pokemon.AddBefore(node, newPokemon);
- }
- else
- {
- var node = pokemon.First;
- for (var i = 1; i < position; i++)
- {
- node = node.Next;
- }
- pokemon.AddBefore(node, newPokemon);
- }
- }
- output.AppendLine($"Added pokemon {name} to position {position}");
- }
- else if (command == "find")
- {
- string type = parameters[1];
- var findPokemon = pokemon.Where(x => x.Type == type).OrderBy(x => x.Name).ThenByDescending(x => x.Power).Take(5).Select(x => $"{x.Name}({x.Power})");
- output.AppendLine($"Type {type}: {string.Join("; ", findPokemon)}");
- }
- else if(command == "ranklist")
- {
- int start = int.Parse(parameters[1]);
- int end = int.Parse(parameters[2]);
- var findRankOfPokemons = pokemon
- .Select((pokemons, index) => new { Pokemon = pokemons, Index = index + 1 })
- .Where(item => item.Index >= start && item.Index <= end)
- .OrderBy(item => item.Index)
- .Select(item => $"{item.Index}. {item.Pokemon.Name}({item.Pokemon.Power})");
- output.AppendLine($"{string.Join("; ", findRankOfPokemons)}");
- }
- }
- Console.WriteLine(output);
- }
- public struct Pokemon
- {
- public Pokemon(string name, string type, int power)
- {
- this.Name = name;
- this.Type = type;
- this.Power = power;
- }
- public string Name { get; set; }
- public string Type { get; set; }
- public int Power { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement