Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace _05.Array_Manipulator
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> inputList = Console.ReadLine()
- .Split(' ').Select(int.Parse).ToList();
- string command;
- while ((command = Console.ReadLine()) != "print")
- {
- string[] commands = command.Split();
- if (commands[0] == "add")
- {
- int index = int.Parse(commands[1]);
- int elementValue = int.Parse(commands[2]);
- inputList.Insert(index, elementValue);
- }
- else if (commands[0] == "addMany")
- {
- int index = int.Parse(commands[1]);
- List<int> elementsToAdd = commands
- .Skip(2).Select(int.Parse).ToList();
- inputList.InsertRange(index, elementsToAdd);
- }
- else if (commands[0] == "contains")
- {
- int element = int.Parse(commands[1]); //added
- if (inputList.Contains(element)) //changed
- {
- Console.WriteLine(inputList.IndexOf(element)); //changed
- }
- else
- {
- Console.WriteLine(-1);
- }
- }
- else if (commands[0] == "remove")
- {
- inputList.RemoveAt(int.Parse(commands[1]));
- }
- else if (commands[0] == "shift")
- {
- int index = int.Parse(commands[1]);
- //for (int j = 0; j < inputList.Count; j++)
- //{
- List<int> firstPartOfList = new List<int> { };
- for (int i = 0; i < index; i++)
- {
- firstPartOfList.Add(inputList[i]);
- }
- int j = 0;
- for (int i = index; i < inputList.Count; i++)
- {
- inputList[j] = inputList[i];
- j++;
- }
- for (int i = 0; i < index; i++)
- {
- inputList[j] = firstPartOfList[i];
- j++;
- }
- //}
- }
- else if (commands[0] == "sumPairs")
- {
- if (inputList.Count % 2 == 1) inputList.Add(0); //added
- int iterations = inputList.Count / 2; //added
- for (int i = 0; i < iterations; i++) //changed
- {
- inputList[i] = inputList[i] + inputList[i + 1];
- inputList.RemoveAt(i + 1);
- }
- }
- }
- Console.Write("[");
- for (int i = 0; i < inputList.Count; i++)
- {
- if (i == inputList.Count - 1)
- Console.Write(inputList[i]);
- else
- {
- Console.Write(inputList[i] + ", ");
- }
- }
- Console.WriteLine("]");
- }
- }
- }
Add Comment
Please, Sign In to add comment