Advertisement
LachezarNinkov

07. List Manipulation Advanced

Mar 16th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ListManipulationAdvanced
  6. {
  7.     class ListManipulationAdvanced
  8.     {
  9.         static void Main()
  10.         {
  11.             List<int> nums = Console.ReadLine()
  12.                 .Split()
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.             string command = Console.ReadLine();
  16.             bool isChange = false;
  17.             while (command != "end")
  18.             {
  19.                 string[] tokens = command.Split();
  20.                 if (tokens[0] == "Add")
  21.                 {
  22.                     int numberToAdd = int.Parse(tokens[1]);
  23.                     nums.Add(numberToAdd);
  24.                 }
  25.                 if (tokens[0] == "Remove")
  26.                 {
  27.                     int numberToRemove = int.Parse(tokens[1]);
  28.                     nums.Remove(numberToRemove);
  29.                 }
  30.                 if (tokens[0] == "RemoveAt")
  31.                 {
  32.                     int numberToRemoveAtIndex = int.Parse(tokens[1]);
  33.                     nums.RemoveAt(numberToRemoveAtIndex);
  34.                 }
  35.                 if (tokens[0] == "Insert")
  36.                 {
  37.                     int numberToAddInIndex = int.Parse(tokens[1]);
  38.                     int indexToAdd = int.Parse(tokens[2]);
  39.                     nums.Insert(indexToAdd, numberToAddInIndex);
  40.                 }
  41.                 if (tokens[0] == "Contains")
  42.                 {
  43.  
  44.                     int numberToCheck = int.Parse(tokens[1]);
  45.                     bool isContains = nums.Contains(numberToCheck);
  46.                     if (isContains)
  47.                     {
  48.                         Console.WriteLine("Yes");
  49.                     }
  50.                     else
  51.                     {
  52.                         Console.WriteLine("No such number");
  53.                     }
  54.                 }
  55.                 if (tokens[0] == "PrintEven")
  56.                 {
  57.                     PrintEvenNums(nums);
  58.                     Console.WriteLine();
  59.                 }
  60.                 if (tokens[0] == "PrintOdd")
  61.                 {
  62.                     PrintOddNums(nums);
  63.                     Console.WriteLine();
  64.                 }
  65.                 if (tokens[0] == "GetSum")
  66.                 {
  67.                     Console.WriteLine(nums.Sum());
  68.  
  69.                 }
  70.                 if (tokens[0] == "Filter")
  71.                 {
  72.                     int index = int.Parse(tokens[2]);
  73.                     if (tokens[1] == ">=")
  74.                     {
  75.                         Console.WriteLine(string.Join(" ", nums.Where(x => x >= index)));
  76.                     }
  77.                 }
  78.                 if (tokens[0] == "Filter")
  79.                 {
  80.                     int index = int.Parse(tokens[2]);
  81.                     if (tokens[1] == "<=")
  82.                     {
  83.                         Console.WriteLine(string.Join(" ", nums.Where(x => x <= index)));
  84.                     }
  85.                 }
  86.                 if (tokens[0] == "Filter")
  87.                 {
  88.                     int index = int.Parse(tokens[2]);
  89.                     if (tokens[1] == "<")
  90.                     {
  91.                         Console.WriteLine(string.Join(" ", nums.Where(x => x < index)));
  92.                     }
  93.                 }
  94.                 if (tokens[0] == "Filter")
  95.                 {
  96.                     int index = int.Parse(tokens[2]);
  97.                     if (tokens[1] == ">")
  98.                     {
  99.                         Console.WriteLine(string.Join(" ", nums.Where(x => x > index)));
  100.                     }
  101.                 }
  102.                 if (tokens[0] == "Add" || tokens[0] == "Remove" || tokens[0] == "RemoveAt" || tokens[0] == "Insert" || tokens[0] == "Contains")
  103.                 {
  104.                     isChange = true;
  105.                     Console.WriteLine(string.Join(" ", nums).TrimEnd());
  106.                 }
  107.                 command = Console.ReadLine();
  108.             }
  109.         }
  110.         private static void PrintEvenNums(List<int> numbers)
  111.         {
  112.             for (int i = 0; i < numbers.Count; i++)
  113.             {
  114.                 int currentNum = numbers[i];
  115.                 if (currentNum % 2 == 0)
  116.                 {
  117.                     Console.Write(currentNum + " ");
  118.                 }
  119.             }
  120.         }
  121.         private static void PrintOddNums(List<int> numbers)
  122.         {
  123.             for (int i = 0; i < numbers.Count; i++)
  124.             {
  125.                 int currentNum = numbers[i];
  126.                 if (currentNum % 2 != 0)
  127.                 {
  128.                     Console.Write(currentNum + " ");
  129.                 }
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement