Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class ArrayManipulator
- {
- static void Main(string[] args)
- {
- List<int> numbers= Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- string command = "";
- while (command != "print")
- {
- string[] input = Console.ReadLine().Split(' ').ToArray();
- command = input[0];
- int index = 0;
- int element = 0;
- switch (command)
- {
- case "add":
- index = Convert.ToInt32(input[1]);
- element = Convert.ToInt32(input[2]);
- Add(numbers, index, element);
- break;
- case "addMany":
- index = Convert.ToInt32(input[1]);
- int[] elements = new int[input.Length-2];
- for (int i = 2; i < input.Length; i++)
- {
- elements[i-2]= Convert.ToInt32(input[i]);
- }
- AddManyElements(numbers, index, elements);
- break;
- case "contains":
- element = Convert.ToInt32(input[1]);
- Console.WriteLine(Contains(numbers, element));
- break;
- case "remove":
- index = Convert.ToInt32(input[1]);
- RemovesElement(numbers, index);
- break;
- case "shift":
- int positions = Convert.ToInt32(input[1]);
- numbers = ShiftElementsWithNPositions(numbers, positions);
- break;
- case "sumPairs":
- SumPairs(numbers);
- break;
- case "print":
- PrintArray(numbers);
- break;
- default:
- break;
- }
- }
- }
- static List<int> SumPairs(List<int> numbers)
- {
- if(numbers.Count % 2 == 0)
- {
- for (int i = 0; i < numbers.Count-1; i++)
- {
- int sum = numbers[i] + numbers[i + 1];
- numbers[i] = sum;
- numbers.RemoveAt(i + 1);
- }
- }
- else
- {
- for (int i = 0; i < numbers.Count-2; i++)
- {
- int sum = numbers[i] + numbers[i + 1];
- numbers[i] = sum;
- numbers.RemoveAt(i + 1);
- }
- }
- return numbers;
- }
- static List<int> ShiftElementsWithNPositions(List<int> nums, int positions)
- {
- int[] numsArr = new int[nums.Count];
- for (int currentPosition = 0; currentPosition < positions; currentPosition++)
- {
- int firstElement = nums[0];
- for (int i = 0; i < nums.Count-1; i++)
- {
- numsArr[i] = nums[i+1];
- }
- numsArr[numsArr.Length-1]=firstElement;
- nums = new List<int>(numsArr);
- }
- return nums;
- }
- static List<int> RemovesElement(List<int> numbers, int index)
- {
- numbers.RemoveAt(index);
- return numbers;
- }
- static int Contains(List<int> numbers, int element)
- {
- int result = -1;
- if (numbers.Contains(element))
- result = numbers.IndexOf(element);
- return result;
- }
- static List<int> AddManyElements(List<int> numbers, int index, int[] elements)
- {
- for (int i = elements.Length-1; i >= 0; i--)
- {
- numbers.Insert(index, elements[i]);
- }
- return numbers;
- }
- static List<int> Add(List<int> numbers, int index, int element)
- {
- numbers.Insert(index, element);
- return numbers;
- }
- static void PrintArray(List<int> numbers)
- {
- Console.WriteLine("[" + string.Join(", ",numbers) + "]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement