Advertisement
knoteva

02. Number Array

Aug 4th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Problem
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.             var list = Console.ReadLine()
  13.                 .Split(' ')
  14.                 .Select(int.Parse)
  15.                 .ToList();
  16.  
  17.             string input;
  18.  
  19.             while ((input = Console.ReadLine()) != "End")
  20.             {
  21.                 string command = input.Split(" ")[0];
  22.  
  23.                 if (command == "Switch")
  24.                 {
  25.                     int index = int.Parse(input.Split(" ")[1]);
  26.  
  27.                     if (index >= 0 && index < list.Count)
  28.                     {
  29.                         int number = list[index];
  30.                         list[index] = number - (number * 2);
  31.                     }                  
  32.                 }
  33.                 else if (command == "Change")
  34.                 {
  35.                     int index = int.Parse(input.Split(" ")[1]);
  36.                     int value = int.Parse(input.Split(" ")[2]);
  37.  
  38.                     if (index >= 0 && index < list.Count)
  39.                     {
  40.                         list[index] = value;
  41.                     }
  42.                 }
  43.                 else if (command == "Sum")
  44.                 {
  45.                     string commandType = input.Split(" ")[1];
  46.  
  47.                     if (commandType == "Negative")
  48.                     {                
  49.                         Console.WriteLine(list.Where(x => x < 0).Sum());
  50.                     }
  51.                     else if (commandType == "Positive")
  52.                     {
  53.                         Console.WriteLine(list.Where(x => x >= 0).Sum());
  54.                     }
  55.                     else if (commandType == "All")
  56.                     {
  57.                         Console.WriteLine(list.Sum());
  58.                     }
  59.                 }
  60.             }
  61.             Console.WriteLine(string.Join(" ", list.Where(x => x >= 0)));
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement