TonyTroev

DebugCommands

Feb 1st, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _18_DebuggingCommands
  8. {
  9.     class DebuggingCommands
  10.     {
  11.         private static int Size = int.Parse(Console.ReadLine());
  12.         static void Main()
  13.         {
  14.             List<long> array = Console.ReadLine()
  15.                 .Split(' ')
  16.                 .Select(long.Parse)
  17.                 .ToList();
  18.  
  19.             while (true)
  20.             {
  21.                 string[] command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  22.                 if (command[0] == "stop")
  23.                 {
  24.                     break;
  25.                 }
  26.  
  27.                 int[] arguments = new int[2];
  28.                 if (command[0] == "add" || command[0] == "subtract" || command[0] == "multiply")
  29.                 {
  30.                     arguments[0] = int.Parse(command[1]) - 1;
  31.                     arguments[1] = int.Parse(command[2]);
  32.                 }
  33.                 else
  34.                 {
  35.                     arguments[0] = 0;
  36.                     arguments[1] = 0;
  37.                 }
  38.                 array = PerformAction(array, command[0], arguments);
  39.  
  40.                 Console.WriteLine(string.Join(" ", array));
  41.             }
  42.         }
  43.  
  44.         static List<long> PerformAction(List<long> arr, string action, int[] arguments)
  45.         {
  46.             int position = arguments[0];
  47.             int value = arguments[1];
  48.  
  49.             switch (action)
  50.             {
  51.                 case "multiply":
  52.                     arr[position] *= value;
  53.                     break;
  54.                 case "add":
  55.                     arr[position] += value;
  56.                     break;
  57.                 case "subtract":
  58.                     arr[position] -= value;
  59.                     break;
  60.                 case "lshift":
  61.                     arr = ArrayShiftLeft(arr);
  62.                     break;
  63.                 case "rshift":
  64.                     arr = ArrayShiftRight(arr);
  65.                     break;
  66.                 default:
  67.                     return arr;
  68.             }
  69.  
  70.             return arr;
  71.         }
  72.  
  73.         private static List<long> ArrayShiftRight(List<long> array)
  74.         {
  75.             Queue<long> shift = new Queue<long>();
  76.             for (int i = Size - 1; i >= 0; i--)
  77.             {
  78.                 shift.Enqueue(array[i]);
  79.             }
  80.             shift.Enqueue(shift.Dequeue());
  81.             return new List<long>(shift.Reverse());
  82.         }
  83.  
  84.         private static List<long> ArrayShiftLeft(List<long> array)
  85.         {
  86.             Queue<long> shift = new Queue<long>();
  87.             for (int i = 0; i < Size; i++)
  88.             {
  89.                 shift.Enqueue(array[i]);
  90.             }
  91.             shift.Enqueue(shift.Dequeue());
  92.             return new List<long>(shift);
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment