fk4m1913

Untitled

Jun 19th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ListManipulationBasics
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<double> numbers = Console.ReadLine().Split().Select(double.Parse).ToList();
  12.             bool isEnd = false;
  13.             bool changeMade = false;
  14.  
  15.             while (!isEnd)
  16.             {
  17.                 string input = Console.ReadLine();
  18.  
  19.                 if (input == "end")
  20.                 {
  21.                     if (changeMade == true)
  22.                     {
  23.                         Console.WriteLine(string.Join(' ', numbers));
  24.                     }
  25.                     isEnd = true;
  26.                     continue;
  27.                 }
  28.  
  29.                 List<string> command = input.Split().ToList();
  30.  
  31.                 if (command[0] == "Add")
  32.                 {
  33.                     changeMade = true;
  34.                     Add(numbers, command);
  35.                 }
  36.  
  37.                 else if (command[0] == "Remove")
  38.                 {
  39.                     changeMade = true;
  40.                     Remove(numbers, command);
  41.                 }
  42.  
  43.                 else if (command[0] == "RemoveAt")
  44.                 {
  45.                     changeMade = true;
  46.                     RemoveAt(numbers, command);
  47.                 }
  48.  
  49.                 else if (command[0] == "Insert")
  50.                 {
  51.                     changeMade = true;
  52.                     Insert(numbers, command);
  53.                 }
  54.  
  55.                 else if (command[0] == "Contains")
  56.                 {
  57.                     bool result = Contains(numbers, command);
  58.  
  59.                     if (result == true)
  60.                     {
  61.                         Console.WriteLine("Yes");
  62.                     }
  63.  
  64.                     else
  65.                     {
  66.                         Console.WriteLine("No such number");
  67.                     }
  68.                 }
  69.  
  70.                 else if (command[0] == "PrintEven")
  71.                 {
  72.                     List<double> result = PrintEven(numbers, command);
  73.                     Console.WriteLine(string.Join(' ', result));
  74.                 }
  75.  
  76.                 else if (command[0] == "PrintOdd")
  77.                 {
  78.                     List<double> result = PrintOdd(numbers, command);
  79.                     Console.WriteLine(string.Join(' ', result));
  80.                 }
  81.  
  82.                 else if (command[0] == "GetSum")
  83.                 {
  84.                     Console.WriteLine(GetSum(numbers, command));
  85.                 }
  86.  
  87.                 else if (command[0] == "Filter")
  88.                 {
  89.                     List<double> result = Filter(numbers, command);
  90.                     Console.WriteLine(string.Join(' ', result));
  91.                 }
  92.             }
  93.         }
  94.  
  95.         static void Add(List<double> numbers, List<string> command)
  96.         {
  97.             double numberToBeAdded = double.Parse(command[1]);
  98.             numbers.Add(numberToBeAdded);
  99.         }
  100.  
  101.         static void Remove(List<double> numbers, List<string> command)
  102.         {
  103.             double numberToBeRemoved = double.Parse(command[1]);
  104.             numbers.Remove(numberToBeRemoved);
  105.         }
  106.  
  107.         static void RemoveAt(List<double> numbers, List<string> command)
  108.         {
  109.             int index = int.Parse(command[1]);
  110.             numbers.RemoveAt(index);
  111.         }
  112.  
  113.         static void Insert(List<double> numbers, List<string> command)
  114.         {
  115.             double numberToBeInserted = double.Parse(command[1]);
  116.             int index = int.Parse(command[2]);
  117.             numbers.Insert(index, numberToBeInserted);
  118.         }
  119.  
  120.         static bool Contains(List<double> numbers, List<string> command)
  121.         {
  122.             double number = double.Parse(command[1]);
  123.             bool contains = false;
  124.  
  125.             if (numbers.Contains(number))
  126.             {
  127.                 contains = true;
  128.             }
  129.  
  130.             return contains;
  131.         }
  132.  
  133.         static List<double> PrintEven(List<double> numbers, List<string> command)
  134.         {
  135.             List<double> result = new List<double>();
  136.  
  137.             for (int i = 0; i < numbers.Count; i++)
  138.             {
  139.                 if (numbers[i] % 2 == 0)
  140.                 {
  141.                     result.Add(numbers[i]);
  142.                 }
  143.             }
  144.  
  145.             return result;
  146.         }
  147.  
  148.         static List<double> PrintOdd(List<double> numbers, List<string> command)
  149.         {
  150.             List<double> result = new List<double>();
  151.  
  152.             for (int i = 0; i < numbers.Count; i++)
  153.             {
  154.                 if (numbers[i] % 2 == 1)
  155.                 {
  156.                     result.Add(numbers[i]);
  157.                 }
  158.             }
  159.  
  160.             return result;
  161.         }
  162.  
  163.         static double GetSum(List<double> numbers, List<string> command)
  164.         {
  165.             double sum = 0;
  166.  
  167.             for (int i = 0; i < numbers.Count; i++)
  168.             {
  169.                 sum += numbers[i];
  170.             }
  171.  
  172.             return sum;
  173.         }
  174.  
  175.         static List<double> Filter(List<double> numbers, List<string> command)
  176.         {
  177.             List<double> result = new List<double>();
  178.             string condition = command[1];
  179.             double number = double.Parse(command[2]);
  180.  
  181.             if (condition == "<")
  182.             {
  183.                 for (int i = 0; i < numbers.Count; i++)
  184.                 {
  185.                     if (numbers[i] < number)
  186.                     {
  187.                         result.Add(numbers[i]);
  188.                     }
  189.                 }
  190.             }
  191.  
  192.             else if (condition == ">")
  193.             {
  194.                 for (int i = 0; i < numbers.Count; i++)
  195.                 {
  196.                     if (numbers[i] > number)
  197.                     {
  198.                         result.Add(numbers[i]);
  199.                     }
  200.                 }
  201.             }
  202.  
  203.             else if (condition == ">=")
  204.             {
  205.                 for (int i = 0; i < numbers.Count; i++)
  206.                 {
  207.                     if (numbers[i] >= number)
  208.                     {
  209.                         result.Add(numbers[i]);
  210.                     }
  211.                 }
  212.             }
  213.  
  214.             else if (condition == "<=")
  215.             {
  216.                 for (int i = 0; i < numbers.Count; i++)
  217.                 {
  218.                     if (numbers[i] <= number)
  219.                     {
  220.                         result.Add(numbers[i]);
  221.                     }
  222.                 }
  223.             }
  224.  
  225.             return result;
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment