Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace listManipulationAdvanced
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
- string command = Console.ReadLine();
- bool hasChanges = false;
- while (command != "end")
- {
- string[] commandArray = command.Split();
- if (commandArray[0] == "Add")
- {
- int number =int.Parse( commandArray[1]);
- numbers.Add(number);
- hasChanges = true;
- }
- else if (commandArray[0] == "Remove")
- {
- int number = int.Parse(commandArray[1]);
- numbers.Remove(number);
- hasChanges = true;
- }
- else if (commandArray[0] == "RemoveAt")
- {
- int index = int.Parse(commandArray[1]);
- numbers.RemoveAt(index);
- hasChanges = true;
- }
- else if (commandArray[0] == "Insert")
- {
- int number = int.Parse(commandArray[1]);
- int index = int.Parse(commandArray[2]);
- numbers.Insert(index, number);
- hasChanges = true;
- }
- else if (commandArray[0] == "Contains")
- {
- int number = int.Parse(commandArray[1]);
- if (numbers.Contains(number))
- {
- Console.WriteLine("Yes");
- }
- else
- {
- Console.WriteLine("No such number");
- }
- }
- else if (commandArray[0] == "PrintEven")
- {
- var filteredList = numbers.Where(x => x % 2 == 0);
- Console.WriteLine(string.Join(" ",filteredList));
- }
- else if (commandArray[0] == "PrintOdd")
- {
- var filteredList = numbers.Where(x => x % 2 != 0);
- Console.WriteLine(string.Join(" ",filteredList));
- }
- else if (commandArray[0] == "GetSum")
- {
- int sum = numbers.Sum();
- Console.WriteLine(sum);
- }
- else if (commandArray[0] == "Filter")
- {
- if (commandArray[1] == ">")
- {
- int number = int.Parse(commandArray[2]);
- var newNumbersWithCondition = numbers.Where(x => x>number);
- Console.WriteLine(string.Join(" ", newNumbersWithCondition));
- }
- else if(commandArray[1]== ">=")
- {
- int number = int.Parse(commandArray[2]);
- var newNumbersWithCondition = numbers.Where(x => x >= number);
- Console.WriteLine(string.Join(" ", newNumbersWithCondition));
- }
- else if (commandArray[1] == "<")
- {
- int number = int.Parse(commandArray[2]);
- var newNumbersWithCondition = numbers.Where(x => x < number);
- Console.WriteLine(string.Join(" ", newNumbersWithCondition));
- }
- else if (commandArray[1] == "<=")
- {
- int number = int.Parse(commandArray[2]);
- var newNumbersWithCondition = numbers.Where(x => x <= number);
- Console.WriteLine(string.Join(" ", newNumbersWithCondition));
- }
- }
- command = Console.ReadLine();
- }
- if(hasChanges)
- Console.WriteLine(string.Join(" ",numbers));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement