Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ListManipulationBasics
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<double> numbers = Console.ReadLine().Split().Select(double.Parse).ToList();
- bool isEnd = false;
- bool changeMade = false;
- while (!isEnd)
- {
- string input = Console.ReadLine();
- if (input == "end")
- {
- if (changeMade == true)
- {
- Console.WriteLine(string.Join(' ', numbers));
- }
- isEnd = true;
- continue;
- }
- List<string> command = input.Split().ToList();
- if (command[0] == "Add")
- {
- changeMade = true;
- Add(numbers, command);
- }
- else if (command[0] == "Remove")
- {
- changeMade = true;
- Remove(numbers, command);
- }
- else if (command[0] == "RemoveAt")
- {
- changeMade = true;
- RemoveAt(numbers, command);
- }
- else if (command[0] == "Insert")
- {
- changeMade = true;
- Insert(numbers, command);
- }
- else if (command[0] == "Contains")
- {
- bool result = Contains(numbers, command);
- if (result == true)
- {
- Console.WriteLine("Yes");
- }
- else
- {
- Console.WriteLine("No such number");
- }
- }
- else if (command[0] == "PrintEven")
- {
- List<double> result = PrintEven(numbers, command);
- Console.WriteLine(string.Join(' ', result));
- }
- else if (command[0] == "PrintOdd")
- {
- List<double> result = PrintOdd(numbers, command);
- Console.WriteLine(string.Join(' ', result));
- }
- else if (command[0] == "GetSum")
- {
- Console.WriteLine(GetSum(numbers, command));
- }
- else if (command[0] == "Filter")
- {
- List<double> result = Filter(numbers, command);
- Console.WriteLine(string.Join(' ', result));
- }
- }
- }
- static void Add(List<double> numbers, List<string> command)
- {
- double numberToBeAdded = double.Parse(command[1]);
- numbers.Add(numberToBeAdded);
- }
- static void Remove(List<double> numbers, List<string> command)
- {
- double numberToBeRemoved = double.Parse(command[1]);
- numbers.Remove(numberToBeRemoved);
- }
- static void RemoveAt(List<double> numbers, List<string> command)
- {
- int index = int.Parse(command[1]);
- numbers.RemoveAt(index);
- }
- static void Insert(List<double> numbers, List<string> command)
- {
- double numberToBeInserted = double.Parse(command[1]);
- int index = int.Parse(command[2]);
- numbers.Insert(index, numberToBeInserted);
- }
- static bool Contains(List<double> numbers, List<string> command)
- {
- double number = double.Parse(command[1]);
- bool contains = false;
- if (numbers.Contains(number))
- {
- contains = true;
- }
- return contains;
- }
- static List<double> PrintEven(List<double> numbers, List<string> command)
- {
- List<double> result = new List<double>();
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] % 2 == 0)
- {
- result.Add(numbers[i]);
- }
- }
- return result;
- }
- static List<double> PrintOdd(List<double> numbers, List<string> command)
- {
- List<double> result = new List<double>();
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] % 2 == 1)
- {
- result.Add(numbers[i]);
- }
- }
- return result;
- }
- static double GetSum(List<double> numbers, List<string> command)
- {
- double sum = 0;
- for (int i = 0; i < numbers.Count; i++)
- {
- sum += numbers[i];
- }
- return sum;
- }
- static List<double> Filter(List<double> numbers, List<string> command)
- {
- List<double> result = new List<double>();
- string condition = command[1];
- double number = double.Parse(command[2]);
- if (condition == "<")
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] < number)
- {
- result.Add(numbers[i]);
- }
- }
- }
- else if (condition == ">")
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] > number)
- {
- result.Add(numbers[i]);
- }
- }
- }
- else if (condition == ">=")
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] >= number)
- {
- result.Add(numbers[i]);
- }
- }
- }
- else if (condition == "<=")
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- if (numbers[i] <= number)
- {
- result.Add(numbers[i]);
- }
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment