Advertisement
Guest User

ChangeableList

a guest
Feb 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ChangeableList
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> numbers = ReadInput();
  14.  
  15.             string command = ProcessCommands(numbers);
  16.  
  17.             PrintResultByCommand(numbers, command);
  18.         }
  19.  
  20.         private static List<int> ReadInput()
  21.         {
  22.             return Console.ReadLine()
  23.                 .Split(' ')
  24.                 .Where(x => x != "")
  25.                 .Select(int.Parse)
  26.                 .ToList();
  27.         }
  28.  
  29.         private static string ProcessCommands(List<int> numbers)
  30.         {
  31.             string command = Console.ReadLine();
  32.  
  33.             while (command != "Odd" && command != "Even")
  34.             {
  35.                 //Delete 5
  36.                 //Insert 10 1
  37.                 string[] commandArgs = command.Split(' ').ToArray();
  38.  
  39.                 if (commandArgs[0] == "Insert")
  40.                 {
  41.                     int insertNumber = int.Parse(commandArgs[1]);
  42.                     int insertPossition = int.Parse(commandArgs[2]);
  43.  
  44.                     numbers.Insert(insertPossition, insertNumber);
  45.  
  46.                 }
  47.                 else if (commandArgs[0] == "Delete")
  48.                 {
  49.                     int deleteValue = int.Parse(commandArgs[1]);
  50.  
  51.                     //Removes first match element
  52.                     // numbers.Remove(deleteValue);
  53.  
  54.                     numbers.RemoveAll(x => x == deleteValue);
  55.                 }
  56.  
  57.                 command = Console.ReadLine();
  58.             }
  59.  
  60.             return command;
  61.         }
  62.  
  63.         private static void PrintResultByCommand(List<int> numbers, string command)
  64.         {
  65.             if (command == "Odd")
  66.             {
  67.                 numbers.RemoveAll(x => x % 2 == 0);
  68.             }
  69.             else
  70.             {
  71.                 numbers.RemoveAll(x => x % 2 != 0);
  72.             }
  73.  
  74.             Console.WriteLine(string.Join(" ", numbers));
  75.         }      
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement