Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _4._ListOperations
- {
- class Program
- {
- static void Main()
- {
- List<int> numbers = Console.ReadLine()
- .Split(" ")
- .Select(int.Parse)
- .ToList();
- string input;
- while ((input = Console.ReadLine()) != "End")
- {
- string[] splitedInput = input.Split(" ");
- string command = splitedInput[0];
- switch (command)
- {
- case "Add":
- int number = int.Parse(splitedInput[1]);
- numbers.Add(number);
- break;
- case "Insert":
- number = int.Parse(splitedInput[1]);
- int index = int.Parse(splitedInput[2]);
- bool isIndexValid = IsIndexValid(index, 0, numbers.Count);
- if (isIndexValid)
- {
- numbers.Insert(index, number);
- }
- break;
- case "Remove":
- index = int.Parse(splitedInput[1]);
- isIndexValid = IsIndexValid(index, 0, numbers.Count - 1);
- if (isIndexValid)
- {
- numbers.RemoveAt(index);
- }
- break;
- case "Shift":
- string direction = splitedInput[1];
- int count = int.Parse(splitedInput[2]);
- if (direction == "left")
- {
- for (int i = 0; i < count; i++)
- {
- numbers.Add(numbers[0]);
- numbers.RemoveAt(0);
- }
- //for (int i = 0; i < count; i++)
- //{
- // int temp = numbers[0];
- // for (int j = 0; j < numbers.Count - 1; j++)
- // {
- // numbers[j] = numbers[j + 1];
- // }
- // numbers[numbers.Count - 1] = temp;
- //}
- }
- else if (direction == "right")
- {
- for (int i = 0; i < count; i++)
- {
- numbers.Insert(0, numbers[numbers.Count - 1]);
- numbers.RemoveAt(numbers.Count - 1);
- }
- //for (int i = 0; i < count; i++)
- //{
- // int temp = numbers[numbers.Count - 1];
- // for (int j = numbers.Count - 2; j >= 0; j--)
- // {
- // numbers[j + 1] = numbers[j];
- // }
- // numbers[0] = temp;
- //}
- }
- break;
- default:
- break;
- }
- }
- Console.WriteLine(string.Join(" ", numbers));
- }
- static bool IsIndexValid(int index, int minRange, int maxRange)
- {
- if (index < minRange || index > maxRange)
- {
- Console.WriteLine("Invalid index");
- return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement