Advertisement
dimipan80

Array Manipulator

Mar 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. /*
  2. Write a program that reads an array of integers from the console and set of commands and executes them over the array.
  3. The commands are as follows:
  4. • add <index> <element> – adds element at the specified index (elements right from this position inclusively are shifted to the right).
  5. • addMany <index> <element 1> <element 2> … <element n> – adds a set of elements at the specified index.
  6. • contains <element> – prints the index of the first occurrence of the specified element (if exists) in the array
  7.     or -1 if the element is not found.
  8. • remove <index> – removes the element at the specified index.
  9. • shift <positions> – shifts every element of the array the number of positions to the left (with rotation).
  10.     o   For example, [1, 2, 3, 4, 5] -> shift 2 -> [3, 4, 5, 1, 2]
  11. • sumPairs – sums the elements in the array by pairs (first + second, third + fourth, …).
  12.     o   For example, [1, 2, 4, 5, 6, 7, 8] -> [3, 9, 13, 8].
  13. • print – stop receiving more commands and print the last state of the array.
  14.  
  15.     input             output                       input                         output
  16.   1 2 4 5 6 7      0                            1 2 3 4 5                  -1
  17.   add 1 8          -1                           addMany 5 9 8 7 6 5        [2, 3, 5, 9, 8, 7, 6, 5, 1]
  18.   contains 1       [1, 8, 2, 4, 5, 6, 7]        contains 15
  19.   contains -3                                   remove 3
  20.   print                                         shift 1
  21.                                                 print
  22. */
  23.  
  24. namespace Array_Manipulator
  25. {
  26.     using System;
  27.     using System.Collections.Generic;
  28.     using System.Linq;
  29.  
  30.     internal static class Program
  31.     {
  32.         private static void Main()
  33.         {
  34.             var numbers = Console.ReadLine()
  35.                 .Split()
  36.                 .Select(n => int.Parse(n))
  37.                 .ToList();
  38.  
  39.             var nextLine = Console.ReadLine();
  40.             while (nextLine != "print")
  41.             {
  42.                 if (nextLine == "sumPairs")
  43.                 {
  44.                     SumElementsInListByPairs(numbers);
  45.                 }
  46.                 else
  47.                 {
  48.                     var arguments = nextLine.Split();
  49.                     switch (arguments[0])
  50.                     {
  51.                         case "add":
  52.                             numbers.Insert(
  53.                                 int.Parse(arguments[1]), int.Parse(arguments[2]));
  54.                             break;
  55.  
  56.                         case "addMany":
  57.                             AddsSetOfElementsInListAtSpecificIndex(arguments, numbers);
  58.                             break;
  59.  
  60.                         case "contains":
  61.                             Console.WriteLine(numbers.IndexOf(int.Parse(arguments[1])));
  62.                             break;
  63.  
  64.                         case "remove":
  65.                             numbers.RemoveAt(int.Parse(arguments[1]));
  66.                             break;
  67.  
  68.                         case "shift":
  69.                             ShiftsEveryElementOfTheListWithNumberPositionsToTheLeft(
  70.                                 int.Parse(arguments[1]), numbers);
  71.                             break;
  72.  
  73.                         default:
  74.                             break;
  75.                     }
  76.                 }
  77.  
  78.                 nextLine = Console.ReadLine();
  79.             }
  80.  
  81.             Console.WriteLine("[{0}]", string.Join(", ", numbers));
  82.         }
  83.  
  84.         private static void AddsSetOfElementsInListAtSpecificIndex(
  85.             string[] args, List<int> numbers)
  86.         {
  87.             var numbersToAdd = args.Skip(1).Select(n => int.Parse(n)).ToArray();
  88.             for (int i = numbersToAdd.Length - 1; i > 0; i--)
  89.             {
  90.                 numbers.Insert(numbersToAdd[0], numbersToAdd[i]);
  91.             }
  92.         }
  93.  
  94.         private static void ShiftsEveryElementOfTheListWithNumberPositionsToTheLeft(
  95.             int positions, List<int> numbers)
  96.         {
  97.             for (int i = 0; i < positions; i++)
  98.             {
  99.                 var movingNumber = numbers[0];
  100.                 numbers.RemoveAt(0);
  101.                 numbers.Add(movingNumber);
  102.             }
  103.         }
  104.  
  105.         private static void SumElementsInListByPairs(List<int> numbers)
  106.         {
  107.             var numbersArr = numbers.ToArray();
  108.             numbers.Clear();
  109.             for (int i = 0; i < numbersArr.Length; i += 2)
  110.             {
  111.                 if (i + 1 >= numbersArr.Length)
  112.                 {
  113.                     numbers.Add(numbersArr[i]);
  114.                     break;
  115.                 }
  116.  
  117.                 numbers.Add(numbersArr[i] + numbersArr[i + 1]);
  118.             }
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement