Advertisement
Kristina_Ivanova

Untitled

Mar 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Lists10_Grains_of_sand
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             List<int> sands = Console.ReadLine()
  12.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.  
  16.             string input = Console.ReadLine();
  17.             while (input != "Mort")
  18.             {
  19.                 string[] tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  20.                     .ToArray();
  21.                 string command = tokens[0];
  22.                 int value = int.Parse(tokens[1]);
  23.                 switch (command)
  24.                 {
  25.                     case "Add":
  26.                         sands.Add(value);
  27.                         break;
  28.                     case "Remove":
  29.                         if (sands.Contains(value))
  30.                         {
  31.                             sands.Remove(value);
  32.                         }
  33.                         else if (value >= 0 && value < sands.Count)
  34.                         {
  35.                             sands.RemoveAt(value);
  36.                         }
  37.                         break;
  38.                     case "Replace":
  39.                         int replacement = int.Parse(tokens[2]);
  40.  
  41.                         if (sands.Contains(value))
  42.                         {
  43.                             int indexToreplace = sands.IndexOf(value);
  44.                             sands[indexToreplace] = replacement;
  45.                         }
  46.  
  47.                         break;
  48.                     case "Increase":
  49.                        
  50.                         if (sands.Exists(x => x >= value))
  51.                         {
  52.                             sands = sands.Select(x => x + value).ToList();
  53.                         }
  54.                         else
  55.                         {
  56.                             sands = sands.Select(x => x + sands[sands.Count - 1]).ToList();
  57.                         }
  58.                         break;
  59.                     case "Collapse":
  60.                         sands.RemoveAll(x => x < value);
  61.                         break;
  62.                 }
  63.                 input = Console.ReadLine();
  64.             }
  65.             Console.WriteLine(string.Join(" ", sands));
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement