Advertisement
ambiorixdr

Untitled

Feb 1st, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 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 _03Lab.Array_Manipulator
  8. {
  9.     class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             List<int> numbers = Console.ReadLine()
  14.                                 .Split(' ')
  15.                                 .Select(int.Parse)
  16.                                 .ToList();
  17.             List<string> commandLine = Console.ReadLine()
  18.                                    .Split(' ')
  19.                                    .ToList();
  20.             string command = commandLine[0];
  21.             while (command != "print")
  22.             {
  23.                 List<int> commandNumbers = commandLine
  24.                                            .Skip(1)
  25.                                            .Select(int.Parse)
  26.                                            .ToList();
  27.  
  28.  
  29.                 switch (command)
  30.                 {
  31.                     case "add":
  32.                         numbers = AddToNumbers(commandNumbers, numbers);
  33.                         break;
  34.                     case "addMany":
  35.                         numbers = AddManyToNumbers(commandNumbers, numbers);
  36.                         break;
  37.                     case "contains":
  38.                         ContainsIndexNumbers(commandNumbers[0], numbers);
  39.                         break;
  40.                     case "remove":
  41.                         numbers = RemoveFromNumbers(commandNumbers[0], numbers);
  42.                         break;
  43.                     case "shift":
  44.                         ShiftFromNumbers(commandNumbers[0], numbers);
  45.                         break;
  46.                     case "sumPairs":
  47.                         numbers = SumPairsInNumbers(numbers);
  48.                         break;
  49.                 }
  50.                 commandLine = Console.ReadLine()
  51.                                .Split(' ')
  52.                                .ToList();
  53.                 command = commandLine[0];
  54.  
  55.             }
  56.             Console.WriteLine($"[{string.Join(", ", numbers).TrimEnd()}]");
  57.         }
  58.  
  59.  
  60.         public static List<int> SumPairsInNumbers(List<int> numbers)
  61.         {
  62.             //•   sumPairs – sums the elements in the array by pairs (first + second, third + fourth, …).
  63.             //o For example, [1, 2, 4, 5, 6, 7, 8] -> [3, 9, 13, 8].
  64.             int len = numbers.Count;
  65.             List<int> result = new List<int>();
  66.  
  67.             for (int i = 1; i <= len; i += 2)
  68.             {
  69.                 int pairSum = 0;
  70.                 if (i != len)
  71.                 {
  72.                     pairSum = numbers[i] + numbers[i - 1];
  73.                 }
  74.                 else
  75.                 {
  76.                     pairSum = numbers[len - 1];
  77.                 }
  78.                 result.Add(pairSum);
  79.             }
  80.  
  81.             return result;
  82.         }
  83.  
  84.         public static void ShiftFromNumbers(int v, List<int> numbers)
  85.         {
  86.             //•   shift < positions > – shifts every element of the array the number of positions to the left(with rotation).
  87.             int shiftPositions = v;
  88.  
  89.             for (int i = 0; i < v; i++)
  90.             {
  91.                 // Storing the first digit of the array:
  92.                 int firstDigit = numbers[0];
  93.  
  94.                 for (int j = 0; j < numbers.Count - 1; j++)
  95.                 {
  96.                     numbers[j] = numbers[j + 1];
  97.                 }
  98.  
  99.                 // After the repositioning of the digits for the current rotation we replace the last digit in the shifted array
  100.                 // with the already stored one that was first digit:
  101.                 numbers[numbers.Count - 1] = firstDigit;
  102.             }
  103.         }
  104.  
  105.         public static List<int> RemoveFromNumbers(int v, List<int> numbers)
  106.         {
  107.             //•   remove < index > – removes the element at the specified index.
  108.             numbers.RemoveAt(v);
  109.             return numbers;
  110.         }
  111.  
  112.         public static void ContainsIndexNumbers(int v, List<int> numbers)
  113.         {
  114.             //contains < element > – prints the index of the first occurrence of the specified element(if exists) in the array or - 1 if the element is not found.
  115.             Console.WriteLine(numbers.FindIndex(num => num == v));
  116.         }
  117.  
  118.         public static List<int> AddManyToNumbers(List<int> commandNumbers, List<int> numbers)
  119.         {
  120.             //addMany < index > < element 1 > < element 2 > … < element n > – adds a set of elements at the specified index.
  121.  
  122.             int index = commandNumbers[0];
  123.             int[] toAdd = commandNumbers.Skip(1).ToArray();
  124.             numbers.InsertRange(index, toAdd);
  125.             return numbers;
  126.         }
  127.  
  128.         public static List<int> AddToNumbers(List<int> commandNumbers, List<int> numbers)
  129.         {
  130.             //add < index > < element > – adds element at the specified index (elements right from this position inclusively are shifted to the right).
  131.             int index = commandNumbers[0];
  132.             int element = commandNumbers[1];
  133.             numbers.Insert(index, element);
  134.             return numbers;
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement