nevenailievaa

07. List Manipulation Advanced Edited

Feb 16th, 2023
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System.Globalization;
  2. using System.Security.Cryptography.X509Certificates;
  3.  
  4. List<int> numbers = Console.ReadLine()
  5.     .Split()
  6.     .Select(int.Parse)
  7.     .ToList();
  8.  
  9. string command = Console.ReadLine();
  10. bool hasChanges = false;
  11.  
  12. while (command != "end")
  13. {
  14.     string[] commandInfo = command.Split();
  15.  
  16.     if (commandInfo[0] == "Add")
  17.     {
  18.         int number = int.Parse(commandInfo[1]);
  19.         numbers.Add(number);
  20.         hasChanges = true;
  21.     }
  22.     else if (commandInfo[0] == "Remove")
  23.     {
  24.         int number = int.Parse(commandInfo[1]);
  25.         numbers.Remove(number);
  26.         hasChanges = true;
  27.     }
  28.     else if (commandInfo[0] == "RemoveAt")
  29.     {
  30.         int number = int.Parse(commandInfo[1]);
  31.         numbers.RemoveAt(number);
  32.         hasChanges = true;
  33.     }
  34.     else if (commandInfo[0] == "Insert")
  35.     {
  36.         int number = int.Parse(commandInfo[1]);
  37.         int index = int.Parse(commandInfo[2]);
  38.         numbers.Insert(index, number);
  39.         hasChanges = true;
  40.     }
  41.     else if (commandInfo[0] == "Contains")
  42.     {
  43.         int number = int.Parse(commandInfo[1]);
  44.        
  45.         if (numbers.Contains(number))
  46.         {
  47.             Console.WriteLine("Yes");
  48.         }
  49.         else
  50.         {
  51.             Console.WriteLine("No such number");
  52.         }
  53.  
  54.     }
  55.     else if (commandInfo[0] == "PrintOdd")
  56.     {
  57.         List<int> oddNumbersList = new List<int>();
  58.         for (int i = 0; i < numbers.Count; i++)
  59.         {
  60.             if (numbers[i] % 2 != 0)
  61.             {
  62.                 oddNumbersList.Add(numbers[i]);
  63.             }
  64.         }
  65.         Console.WriteLine(String.Join(" ", oddNumbersList));
  66.     }
  67.     else if (commandInfo[0] == "PrintEven")
  68.     {
  69.         List<int> evenNumbersList = new List<int>();
  70.         for (int i = 0; i < numbers.Count; i++)
  71.         {
  72.             if (numbers[i] % 2 == 0)
  73.             {
  74.                 evenNumbersList.Add(numbers[i]);
  75.             }
  76.         }
  77.         Console.WriteLine(String.Join(" ", evenNumbersList));
  78.     }
  79.     else if (commandInfo[0] == "GetSum")
  80.     {
  81.        
  82.         int sum = 0;
  83.         for (int i = 0; i < numbers.Count; i++)
  84.         {
  85.             sum += numbers[i];
  86.         }
  87.         Console.WriteLine(sum);
  88.     }
  89.     else if (commandInfo[0] == "Filter")
  90.     {
  91.         List<int> conditionList = new List<int>();
  92.         string condition = commandInfo[1];
  93.         int number = int.Parse(commandInfo[2]);
  94.         if (condition == "<")
  95.         {
  96.             foreach (var item in numbers)
  97.             {
  98.                 if (item < number)
  99.                 {
  100.                     conditionList.Add(item);
  101.                 }
  102.             }
  103.         }
  104.         if (condition == ">")
  105.         {
  106.             foreach (var item in numbers)
  107.             {
  108.                 if (item > number)
  109.                 {
  110.                     conditionList.Add(item);
  111.                 }
  112.             }
  113.         }
  114.         if (condition == ">=")
  115.         {
  116.             foreach (var item in numbers)
  117.             {
  118.                 if (item >= number)
  119.                 {
  120.                     conditionList.Add(item);
  121.                 }
  122.             }
  123.         }
  124.         if (condition == "<=")
  125.         {
  126.             foreach (var item in numbers)
  127.             {
  128.                 if (item <= number)
  129.                 {
  130.                     conditionList.Add(item);
  131.                 }
  132.             }
  133.         }
  134.         Console.WriteLine(String.Join(" ", conditionList));
  135.     }
  136.     command = Console.ReadLine();
  137. }
  138. if (hasChanges)
  139. {
  140.     Console.WriteLine(string.Join(" ", numbers));
  141. }
Add Comment
Please, Sign In to add comment