Advertisement
gospod1978

List\List Manipulation Advanced

Oct 18th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace fundamental14
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main()
  10.         {
  11.             string input = string.Empty;
  12.             List<int> number = Console.ReadLine().Split().Select(int.Parse).ToList();
  13.             //int count = 0;
  14.             List<int>dif = new List<int>(number);
  15.            
  16.             while((input = Console.ReadLine()) != "end")
  17.             {
  18.                 List<string> command = input.Split().ToList();
  19.                
  20.                 if (command[0] == "Contains")
  21.                 {
  22.                     int amount = int.Parse(command[1]);
  23.                
  24.                     if (number.Contains(amount))
  25.                     {
  26.                         Console.WriteLine("Yes");
  27.                     }
  28.                     else
  29.                     {
  30.                         Console.WriteLine("No such number");
  31.                     }
  32.                 }
  33.                 else if (command[0] == "PrintEven")
  34.                 {
  35.                     for (int i = 0; i < number.Count; i++)
  36.                     {
  37.                         if (number[i] % 2 == 0)
  38.                         {
  39.                             Console.Write($"{number[i]} ");
  40.                         }
  41.                     }
  42.                     Console.WriteLine();
  43.                 }
  44.                 else if (command[0] == "PrintOdd")
  45.                 {
  46.                     for (int i = 0; i < number.Count; i++)
  47.                     {
  48.                         if (number[i] % 2 != 0)
  49.                         {
  50.                             Console.Write($"{number[i]} ");
  51.                         }
  52.                     }
  53.                     Console.WriteLine();
  54.                 }
  55.                 else if (command[0] == "GetSum")
  56.                 {
  57.                     Console.WriteLine(number.Sum());
  58.                 }
  59.                 else if(command[0] == "Filter")
  60.                 {
  61.                     int filter = int.Parse(command[2]);
  62.                
  63.                     if (command[1] == ">=")
  64.                     {
  65.                         for (int i = 0; i < number.Count; i++)
  66.                         {
  67.                             if (number[i] >= filter)
  68.                             {
  69.                                 Console.Write($"{number[i]} ");
  70.                             }
  71.                         }
  72.                         Console.WriteLine();
  73.                     }
  74.                     else if (command[1] == "<")
  75.                     {
  76.                         for (int i = 0; i < number.Count; i++)
  77.                         {
  78.                             if (number[i] < filter)
  79.                             {
  80.                                 Console.Write($"{number[i]} ");
  81.                             }
  82.                         }
  83.                         Console.WriteLine();
  84.                     }
  85.                     else if (command[1] == "<=")
  86.                     {
  87.                         for (int i = 0; i < number.Count; i++)
  88.                         {
  89.                             if (number[i] <= filter)
  90.                             {
  91.                                 Console.Write($"{number[i]} ");
  92.                             }
  93.                         }
  94.                         Console.WriteLine();
  95.                     }
  96.                     else if (command[1] == ">")
  97.                     {
  98.                         for (int i = 0; i < number.Count; i++)
  99.                         {
  100.                             if (number[i] > filter)
  101.                             {
  102.                                 Console.Write($"{number[i]} ");
  103.                             }
  104.                         }
  105.                         Console.WriteLine();
  106.                     }
  107.                 }
  108.                 else if (command[0] == "Add")
  109.                 {
  110.                     int amount = int.Parse(command[1]);
  111.                
  112.                     number.Add(amount);
  113.                     //count++;
  114.                 }
  115.                 else if (command[0] == "Remove")
  116.                 {
  117.                     int amount = int.Parse(command[1]);
  118.                
  119.                     number.Remove(amount);
  120.                     //count++;
  121.                 }
  122.                 else if (command[0] == "RemoveAt")
  123.                 {
  124.                     int amount = int.Parse(command[1]);
  125.                
  126.                     number.RemoveAt(amount);
  127.                     //count++;
  128.                 }
  129.                 else if (command[0] == "Insert")
  130.                 {
  131.                     int amount = int.Parse(command[1]);
  132.                
  133.                     int index = int.Parse(command[2]);
  134.                     number.Insert(index, amount);
  135.                     //count++;
  136.                 }
  137.             }
  138.         var compare = (number.All(dif.Contains) && number.Count == dif.Count);
  139.        
  140.         if (!compare)
  141.         {
  142.         Console.WriteLine(string.Join(" ", number));
  143.         }
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement