Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ArrayManipulators_2._06._2016
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<long> inputNumbers = Console.ReadLine().Split(' ').Select(long.Parse).ToList();
- List<string> commands = new List<string>();
- int indexCommand = 0;
- int cntContains = 0; // times command "contains" was entered
- List<int> containsIndex = new List<int>();
- do
- {
- commands.Add(Console.ReadLine());
- indexCommand++;
- } while (commands[indexCommand - 1] != "print");
- for (int i = 0; i < commands.Count; i++)
- {
- string[] currentCommand = commands[i].Split(' ');
- switch (currentCommand[0].ToLower())
- {
- case "add":
- inputNumbers.Insert(int.Parse(currentCommand[1]), long.Parse(currentCommand[2]));
- break;
- case "addmany":
- AddMany(inputNumbers, currentCommand);
- break;
- case "contains":
- cntContains++;
- FindingContainIndex(inputNumbers, currentCommand, containsIndex);
- break;
- case "remove":
- inputNumbers.RemoveAt(int.Parse(currentCommand[1]));
- break;
- case "shift":
- Shift(inputNumbers, currentCommand);
- break;
- case "sumpairs":
- SumPairs(inputNumbers);
- break;
- case "print":
- if (containsIndex.Count > 0)
- {
- for (int cntCont = 0; cntCont < cntContains; cntCont++)
- {
- Console.WriteLine(containsIndex[cntCont]);
- }
- }
- Console.Write('[');
- Console.Write(string.Join(", ", inputNumbers));
- Console.WriteLine(']');
- break;
- }
- }
- }
- public static void SumPairs(List<long> inputNumbers)
- {
- List<long> pairsSum = new List<long>();
- for (int iPairs = 0; iPairs < inputNumbers.Count; iPairs++)
- {
- pairsSum.Add(inputNumbers[iPairs] + inputNumbers[iPairs + 1]);
- iPairs++;
- }
- while (inputNumbers.Count > 0)
- {
- inputNumbers.RemoveAt(0);
- }
- for (int addPairs = 0; addPairs < pairsSum.Count; addPairs++)
- {
- inputNumbers.Add(pairsSum[addPairs]);
- }
- }
- public static void Shift(List<long> inputNumbers, string[] currentCommand)
- {
- // saving shifted elements
- int numberOfElements = int.Parse(currentCommand[1]); // number of elements for shifiting: 1, 2, 3, etc...
- long[] savedElements = new long[numberOfElements];
- for (int iSave = 0; iSave < numberOfElements; iSave++)
- {
- savedElements[iSave] = inputNumbers[iSave];
- }
- // removing first left elements
- int initialCountInpNumbers = inputNumbers.Count;
- while (inputNumbers.Count > initialCountInpNumbers - numberOfElements)
- {
- inputNumbers.RemoveAt(0);
- }
- //adding already saved elements
- for (int iAdd = 0; iAdd < numberOfElements; iAdd++)
- {
- inputNumbers.Add(savedElements[iAdd]);
- }
- }
- public static void FindingContainIndex(List<long> inputNumbers, string[] currentCommand, List<int> containsIndex)
- {
- long containNumber = long.Parse(currentCommand[1]);
- for (int iContain = 0; iContain < inputNumbers.Count; iContain++) // searching within the input for a particular number
- {
- if (inputNumbers[iContain] == containNumber)
- {
- containsIndex.Add(iContain);
- break;
- }
- else
- {
- containsIndex.Add(-1);
- }
- }
- }
- public static void AddMany(List<long> inputNumbers, string[] command)
- {
- long[] addElements = new long[command.Length - 2];
- // saving add elements
- for (int iSave = 0; iSave < command.Length - 2; iSave++)
- {
- addElements[iSave] = long.Parse(command[iSave + 2]);
- }
- // inserting saved elements at particular index
- for (int iInsert = addElements.Length - 1; iInsert >= 0; iInsert--)
- {
- int index = int.Parse(command[1]);
- inputNumbers.Insert(int.Parse(command[1]), addElements[iInsert]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement