Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _10.ArrayManipulator
- {
- class ArrayManipulator
- {
- static void Main()
- {
- List<int> numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- string[] commands = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- List<int> comand = new List<int>();
- int startPossition = 0;
- while (commands[0] != "print")
- {
- if (commands[0] == "addMany")
- {
- foreach (var nums in commands)
- {
- if (nums == "addMany")
- {
- continue;
- }
- comand.Add(int.Parse(nums));
- startPossition = comand[0];
- }
- }
- switch (commands[0])
- {
- case "add":
- numbers = AddElement(int.Parse(commands[1]), int.Parse(commands[2]), numbers);
- break;
- case "contains":
- Contains(int.Parse(commands[1]), numbers);
- break;
- case "remove":
- numbers = Remove(int.Parse(commands[1]), numbers);
- break;
- case "addMany":
- comand.RemoveAt(0);
- numbers = AddMany(startPossition, comand, numbers);
- comand = new List<int>();
- break;
- case "shift":
- numbers = Shift(int.Parse(commands[1]), numbers);
- break;
- default:
- numbers = SumPairs(numbers);
- break;
- }
- commands = Console.ReadLine().Split(' ').ToArray();
- }
- Console.WriteLine("[{0}]", string.Join(", ", numbers));
- }
- private static List<int> Shift(int shiftCounts, List<int> numbers)
- {
- for (int i = 0; i < shiftCounts; i++)
- {
- int startPossition = numbers[0];
- numbers.RemoveAt(0);
- numbers.Add(startPossition);
- }
- return numbers;
- }
- private static List<int> AddMany(int possition, List<int> nums, List<int> numbers)
- {
- numbers.InsertRange(possition, nums);
- return numbers;
- }
- private static List<int> SumPairs(List<int> numbers)
- {
- List<int> sumNumbers = new List<int>();
- for (int i = 0; i < numbers.Count; i++)
- {
- if (i == numbers.Count - 1)
- {
- break;
- }
- var sum = numbers[i] + numbers[i + 1];
- sumNumbers.Add(sum);
- i++;
- }
- if (numbers.Count % 2 == 0)
- {
- return sumNumbers;
- }
- sumNumbers.Add(numbers[numbers.Count - 1]);
- return sumNumbers;
- }
- private static List<int> Remove(int index, List<int> numbers)
- {
- numbers.RemoveAt(index);
- return numbers;
- }
- private static void Contains(int element, List<int> numbers)
- {
- Console.WriteLine(numbers.IndexOf(element));
- }
- private static List<int> AddElement(int index, int element, List<int> numbers)
- {
- numbers.Insert(index, element);
- return numbers;
- }
- }
- }
Add Comment
Please, Sign In to add comment