Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- namespace ListOperations
- {
- class ListOperations
- {
- static void Main()
- {
- List<int> numbers = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToList();
- string command = string.Empty;
- while ((command = Console.ReadLine()) != "End")
- {
- string[] splitedCommand = command.Split(" ");
- string currentCommand = splitedCommand[0];
- bool isContained = true;
- if (currentCommand == "Add")
- {
- int digitToAdd = int.Parse(splitedCommand[1]);
- numbers.Add(digitToAdd);
- }
- else if (currentCommand == "Insert")
- {
- int numberToInsert = int.Parse(splitedCommand[1]);
- int index = int.Parse(splitedCommand[2]);
- isContained = CheckIndexContain(numbers, isContained, numberToInsert, index);
- }
- else if (currentCommand == "Remove")
- {
- int indexToRem = int.Parse(splitedCommand[1]);
- if (indexToRem > numbers.Count)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- numbers.RemoveAt(indexToRem);
- }
- }
- else if (currentCommand == "Shift")
- {
- int countOfShift = int.Parse(splitedCommand[2]);
- if (splitedCommand[1] == "left")
- {
- for (int i = 0; i < countOfShift; i++)
- {
- numbers.Add(numbers[0]);
- numbers.RemoveAt(0);
- }
- }
- else if (splitedCommand[1] == "right")
- {
- for (int i = 0; i < countOfShift; i++)
- {
- numbers.Insert(0, numbers[numbers.Count - 1]);
- numbers.RemoveAt(numbers.Count - 1);
- }
- }
- }
- }
- Console.WriteLine(string.Join(" ", numbers));
- }
- private static bool CheckIndexContain(List<int> numbers, bool isContained, int numberToInsert, int index)
- {
- if (index >= numbers.Count || index < 0)
- {
- isContained = false;
- }
- if (isContained)
- {
- numbers.Insert(index, numberToInsert);
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- return isContained;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment