Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _6._List_Manipulation_Basics
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> num = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse).ToList();
- int counter = 0;
- while (true)
- {
- string word = Console.ReadLine();
- if (word == "end")
- {
- break;
- }
- string[] command = word.Split();
- if (command[0] == "Add")
- {
- int toAdd = int.Parse(command[1]);
- num.Add(toAdd);
- counter += 1;
- }
- else if (command[0] == "Remove")
- {
- int toRemove = int.Parse(command[1]);
- num.Remove(toRemove);
- counter += 1;
- }
- else if (command[0] == "RemoveAt")
- {
- int removeAt = int.Parse(command[1]);
- num.RemoveAt(removeAt);
- counter += 1;
- }
- else if (command[0] == "Insert")
- {
- int insertNumber = int.Parse(command[1]);
- int indexLoction = int.Parse(command[2]);
- num.Insert(indexLoction, insertNumber);
- counter += 1;
- }
- if (command[0] == "Contains")
- {
- int contain = int.Parse(command[1]);
- bool isContain = false;
- foreach (var digit in num)
- {
- if (digit == contain)
- {
- Console.WriteLine("Yes");
- isContain = true;
- break;
- }
- }
- if (isContain == false)
- {
- Console.WriteLine("No such number");
- }
- }
- else if (command[0] == "PrintEven")
- {
- List<int> newNum = new List<int>();
- foreach (var digit in num)
- {
- if (digit % 2 == 0)
- {
- newNum.Add(digit);
- }
- }
- Console.WriteLine(string.Join(" ", newNum));
- }
- else if (command[0] == "PrintOdd")
- {
- List<int> newNum = new List<int>();
- foreach (var digit in num)
- {
- if (digit % 2 != 0)
- {
- newNum.Add(digit);
- }
- }
- Console.WriteLine(string.Join(" ", newNum));
- }
- else if (command[0] == "GetSum")
- {
- int sum = 0;
- foreach (var digit in num)
- {
- sum += digit;
- }
- Console.WriteLine(sum);
- }
- else if (command[0] == "Filter")
- {
- List<int> newNum = new List<int>();
- if (command[1] == ">")
- {
- int index = int.Parse(command[2]);
- foreach (var digit in num)
- {
- if (digit > index)
- {
- newNum.Add(digit);
- }
- }
- }
- else if (command[1] == ">=")
- {
- int index = int.Parse(command[2]);
- foreach (var digit in num)
- {
- if (digit >= index)
- {
- newNum.Add(digit);
- }
- }
- }
- else if (command[1] == "<")
- {
- int index = int.Parse(command[2]);
- foreach (var digit in num)
- {
- if (digit < index)
- {
- newNum.Add(digit);
- }
- }
- }
- Console.WriteLine(string.Join(" ", newNum));
- }
- }
- if (counter > 0)
- {
- Console.WriteLine(string.Join(" ", num));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement