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 ListManipulationAdvanced
- {
- static void Main()
- {
- List<int> nums = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToList();
- string command = Console.ReadLine();
- bool isChange = false;
- while (command != "end")
- {
- string[] tokens = command.Split();
- if (tokens[0] == "Add")
- {
- int numberToAdd = int.Parse(tokens[1]);
- nums.Add(numberToAdd);
- }
- if (tokens[0] == "Remove")
- {
- int numberToRemove = int.Parse(tokens[1]);
- nums.Remove(numberToRemove);
- }
- if (tokens[0] == "RemoveAt")
- {
- int numberToRemoveAtIndex = int.Parse(tokens[1]);
- nums.RemoveAt(numberToRemoveAtIndex);
- }
- if (tokens[0] == "Insert")
- {
- int numberToAddInIndex = int.Parse(tokens[1]);
- int indexToAdd = int.Parse(tokens[2]);
- nums.Insert(indexToAdd, numberToAddInIndex);
- }
- if (tokens[0] == "Contains")
- {
- int numberToCheck = int.Parse(tokens[1]);
- bool isContains = nums.Contains(numberToCheck);
- if (isContains)
- {
- Console.WriteLine("Yes");
- }
- else
- {
- Console.WriteLine("No such number");
- }
- }
- if (tokens[0] == "PrintEven")
- {
- PrintEvenNums(nums);
- Console.WriteLine();
- }
- if (tokens[0] == "PrintOdd")
- {
- PrintOddNums(nums);
- Console.WriteLine();
- }
- if (tokens[0] == "GetSum")
- {
- Console.WriteLine(nums.Sum());
- }
- if (tokens[0] == "Filter")
- {
- int index = int.Parse(tokens[2]);
- if (tokens[1] == ">=")
- {
- Console.WriteLine(string.Join(" ", nums.Where(x => x >= index)));
- }
- }
- if (tokens[0] == "Filter")
- {
- int index = int.Parse(tokens[2]);
- if (tokens[1] == "<=")
- {
- Console.WriteLine(string.Join(" ", nums.Where(x => x <= index)));
- }
- }
- if (tokens[0] == "Filter")
- {
- int index = int.Parse(tokens[2]);
- if (tokens[1] == "<")
- {
- Console.WriteLine(string.Join(" ", nums.Where(x => x < index)));
- }
- }
- if (tokens[0] == "Filter")
- {
- int index = int.Parse(tokens[2]);
- if (tokens[1] == ">")
- {
- Console.WriteLine(string.Join(" ", nums.Where(x => x > index)));
- }
- }
- if (tokens[0] == "Add" || tokens[0] == "Remove" || tokens[0] == "RemoveAt" || tokens[0] == "Insert" || tokens[0] == "Contains")
- {
- isChange = true;
- Console.WriteLine(string.Join(" ", nums).TrimEnd());
- }
- command = Console.ReadLine();
- }
- }
- private static void PrintEvenNums(List<int> numbers)
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- int currentNum = numbers[i];
- if (currentNum % 2 == 0)
- {
- Console.Write(currentNum + " ");
- }
- }
- }
- private static void PrintOddNums(List<int> numbers)
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- int currentNum = numbers[i];
- if (currentNum % 2 != 0)
- {
- Console.Write(currentNum + " ");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement