Advertisement
Guest User

07. List manipulation advanced

a guest
Nov 2nd, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Xml.Schema;
  6.  
  7. namespace _6._List_Manipulation_Basics
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> number = Console.ReadLine()
  14.                  .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  15.                  .Select(int.Parse)
  16.                  .ToList();
  17.  
  18.             string[] command = Console.ReadLine()
  19.                                       .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  20.                                       .ToArray();
  21.  
  22.             List<int> copiedList = new List<int>(number);
  23.             bool isChanged = false;
  24.  
  25.             while (command[0].ToUpper() != "END")
  26.             {
  27.                 isChanged = false;
  28.                 switch (command[0].ToUpper())
  29.                 {
  30.                     case "ADD":
  31.                         copiedList.Add(int.Parse(command[1]));
  32.                         isChanged = true;
  33.                         break;
  34.                     case "REMOVE":
  35.                         copiedList.Remove(int.Parse(command[1]));
  36.                         isChanged = true;
  37.                         break;
  38.                     case "REMOVEAT":
  39.                         copiedList.RemoveAt(int.Parse(command[1]));
  40.                         isChanged = true;
  41.                         break;
  42.                     case "INSERT":
  43.                         copiedList.Insert(int.Parse(command[2]), int.Parse(command[1]));
  44.                         isChanged = true;
  45.                         break;
  46.                     case "CONTAINS":
  47.                         if (copiedList.Contains(int.Parse(command[1])))
  48.                         {
  49.                             Console.WriteLine("Yes");
  50.                         }
  51.                         else if (!copiedList.Contains(int.Parse(command[1])))
  52.                         {
  53.                             Console.WriteLine("No such number");
  54.                         }
  55.                         break;
  56.                     case "PRINTEVEN":
  57.                         for (int i = 0; i < copiedList.Count; i++)
  58.                         {
  59.                             int currentInteger = copiedList[i];
  60.                             if (currentInteger % 2 == 0)
  61.                             {
  62.                                 Console.Write(currentInteger + " ");
  63.                             }
  64.                         }
  65.                         Console.WriteLine();
  66.                         break;
  67.                     case "PRINTODD":
  68.                         for (int i = 0; i < copiedList.Count; i++)
  69.                         {
  70.                             int currentInteger = copiedList[i];
  71.                             if (currentInteger % 2 != 0)
  72.                             {
  73.                                 Console.Write(currentInteger + " ");
  74.                             }
  75.                         }
  76.                         Console.WriteLine();
  77.                         break;
  78.                     case "GETSUM":
  79.                         int sumAll = 0;
  80.                         for (int i = 0; i < copiedList.Count; i++)
  81.                         {
  82.                             int currentInteger = copiedList[i];
  83.                             sumAll += currentInteger;
  84.                         }
  85.                         Console.WriteLine(sumAll);
  86.                         break;
  87.                     case "FILTER":
  88.                         if (command[1] == "<")
  89.                         {
  90.                             List<int> results = new List<int>(copiedList.Where(n => n < int.Parse(command[2])));
  91.                             Console.WriteLine(string.Join(' ', results));
  92.                         }
  93.                         else if (command[1] == ">")
  94.                         {
  95.                             List<int> results = new List<int>(copiedList.Where(n => n > int.Parse(command[2])));
  96.                             Console.WriteLine(string.Join(' ', results));
  97.                         }
  98.                         else if (command[1] == ">=")
  99.                         {
  100.                             List<int> results = new List<int>(copiedList.Where(n => n >= int.Parse(command[2])));
  101.                             Console.WriteLine(string.Join(' ', results));
  102.                         }
  103.                         else if (command[1] == "<=")
  104.                         {
  105.                             List<int> results = new List<int>(copiedList.Where(n => n <= int.Parse(command[2])));
  106.                             Console.WriteLine(string.Join(' ', results));
  107.                         }
  108.                         break;
  109.                 }
  110.                 command = Console.ReadLine()
  111.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  112.                     .ToArray();
  113.             }
  114.             if (isChanged)
  115.             {
  116.                 Console.WriteLine(string.Join(' ',copiedList));
  117.             }
  118.  
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement