Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write a program that reads an array of integers from the console and set of commands and executes them over the array.
- The commands are as follows:
- • add <index> <element> – adds element at the specified index (elements right from this position inclusively are shifted to the right).
- • addMany <index> <element 1> <element 2> … <element n> – adds a set of elements at the specified index.
- • contains <element> – prints the index of the first occurrence of the specified element (if exists) in the array
- or -1 if the element is not found.
- • remove <index> – removes the element at the specified index.
- • shift <positions> – shifts every element of the array the number of positions to the left (with rotation).
- o For example, [1, 2, 3, 4, 5] -> shift 2 -> [3, 4, 5, 1, 2]
- • sumPairs – sums the elements in the array by pairs (first + second, third + fourth, …).
- o For example, [1, 2, 4, 5, 6, 7, 8] -> [3, 9, 13, 8].
- • print – stop receiving more commands and print the last state of the array.
- input output input output
- 1 2 4 5 6 7 0 1 2 3 4 5 -1
- add 1 8 -1 addMany 5 9 8 7 6 5 [2, 3, 5, 9, 8, 7, 6, 5, 1]
- contains 1 [1, 8, 2, 4, 5, 6, 7] contains 15
- contains -3 remove 3
- print shift 1
- print
- */
- namespace Array_Manipulator
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- internal static class Program
- {
- private static void Main()
- {
- var numbers = Console.ReadLine()
- .Split()
- .Select(n => int.Parse(n))
- .ToList();
- var nextLine = Console.ReadLine();
- while (nextLine != "print")
- {
- if (nextLine == "sumPairs")
- {
- SumElementsInListByPairs(numbers);
- }
- else
- {
- var arguments = nextLine.Split();
- switch (arguments[0])
- {
- case "add":
- numbers.Insert(
- int.Parse(arguments[1]), int.Parse(arguments[2]));
- break;
- case "addMany":
- AddsSetOfElementsInListAtSpecificIndex(arguments, numbers);
- break;
- case "contains":
- Console.WriteLine(numbers.IndexOf(int.Parse(arguments[1])));
- break;
- case "remove":
- numbers.RemoveAt(int.Parse(arguments[1]));
- break;
- case "shift":
- ShiftsEveryElementOfTheListWithNumberPositionsToTheLeft(
- int.Parse(arguments[1]), numbers);
- break;
- default:
- break;
- }
- }
- nextLine = Console.ReadLine();
- }
- Console.WriteLine("[{0}]", string.Join(", ", numbers));
- }
- private static void AddsSetOfElementsInListAtSpecificIndex(
- string[] args, List<int> numbers)
- {
- var numbersToAdd = args.Skip(1).Select(n => int.Parse(n)).ToArray();
- for (int i = numbersToAdd.Length - 1; i > 0; i--)
- {
- numbers.Insert(numbersToAdd[0], numbersToAdd[i]);
- }
- }
- private static void ShiftsEveryElementOfTheListWithNumberPositionsToTheLeft(
- int positions, List<int> numbers)
- {
- for (int i = 0; i < positions; i++)
- {
- var movingNumber = numbers[0];
- numbers.RemoveAt(0);
- numbers.Add(movingNumber);
- }
- }
- private static void SumElementsInListByPairs(List<int> numbers)
- {
- var numbersArr = numbers.ToArray();
- numbers.Clear();
- for (int i = 0; i < numbersArr.Length; i += 2)
- {
- if (i + 1 >= numbersArr.Length)
- {
- numbers.Add(numbersArr[i]);
- break;
- }
- numbers.Add(numbersArr[i] + numbersArr[i + 1]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement