Advertisement
knoteva

Untitled

Sep 29th, 2019
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._The_Final_Quest
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var list = Console.ReadLine()
  12.                 .Split(" ")
  13.                 .Where(s => !string.IsNullOrWhiteSpace(s))
  14.                 .ToList();
  15.             string commandLine = "";
  16.             while ((commandLine = Console.ReadLine()) != "Stop")
  17.             {
  18.                 string command = commandLine.Split(" ")[0];
  19.  
  20.                 if (command == "Delete")
  21.                 {
  22.                     int desiredIndex = int.Parse(commandLine.Split(" ")[1]) + 1;
  23.                     if (list.Count > desiredIndex && desiredIndex >= 0)
  24.                     {
  25.                        list.RemoveAt(desiredIndex);
  26.                     }
  27.                 }
  28.                 if (command == "Sort")
  29.                 {
  30.                     list.Sort((x, y) => y.CompareTo(x));
  31.                 }
  32.                 if (command == "Put")
  33.                 {
  34.                     string word = commandLine.Split(" ")[1];
  35.                     int desiredIndex = int.Parse(commandLine.Split(" ")[2]) - 1;
  36.                     if (list.Count >= desiredIndex  && desiredIndex >= 0)
  37.                     {
  38.                        list.Insert(desiredIndex, word);
  39.                     }
  40.                 }
  41.                 if (command == "Replace")
  42.                 {
  43.                     string word1 = commandLine.Split(" ")[1];
  44.                     string word2 = commandLine.Split(" ")[2];
  45.                     if (list.Contains(word2))
  46.                     {
  47.                         int index = list.IndexOf(word2);
  48.                         list.Remove(word2);
  49.                         list.Insert(index, word1);
  50.                     }
  51.                 }
  52.                 if (command == "Swap")
  53.                 {
  54.                     string word1 = commandLine.Split(" ")[1];
  55.                     string word2 = commandLine.Split(" ")[2];
  56.                     if (list.Contains(word1) && list.Contains(word2))
  57.                     {
  58.                         int index1 = list.IndexOf(word1);
  59.                         int index2 = list.IndexOf(word2);
  60.                         list.RemoveAt(index1);
  61.                         list.Insert(index1, word2);
  62.                         list.RemoveAt(index2);
  63.                         list.Insert(index2, word1);  
  64.                     }
  65.                 }
  66.             }
  67.             Console.WriteLine(string.Join(" ", list));
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement