Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _04_ListOperations
- {
- class Program
- {
- public static void Main()
- {
- List<int> numbers = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToList();
- while (true)
- {
- List<string> command = Console.ReadLine()
- .ToLower()
- .Split()
- .ToList();
- string operation = command[0];
- if (operation == "end")
- {
- break;
- }
- else if (operation == "add")
- {
- int currentNumber = int.Parse(command[1]);
- numbers.Add(currentNumber);
- }
- else if (operation == "insert")
- {
- int position = int.Parse(command[2]);
- if (position > numbers.Count - 1)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- int numbToInsert = int.Parse(command[1]);
- numbers.Insert(position, numbToInsert);
- }
- }
- else if (operation == "remove")
- {
- int indexToRemove = int.Parse(command[1]);
- if (indexToRemove > numbers.Count - 1)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- numbers.RemoveAt(indexToRemove);
- }
- }
- else if (operation == "shift")
- {
- if (command[1] == "right")
- {
- int count = int.Parse(command[2]);
- int last = numbers.Count - 1;
- for (int i = 0; i < count; i++)
- {
- int moveToFront = numbers[last];
- numbers.RemoveAt(last);
- numbers.Insert(0, moveToFront);
- }
- }
- else if (command[1] == "left")
- {
- int count = int.Parse(command[2]);
- int last = numbers.Count - 1;
- for (int i = 0; i < count; i++)
- {
- int moveToEnd = numbers[0];
- numbers.RemoveAt(0);
- numbers.Insert(last, moveToEnd);
- }
- }
- }
- }
- Console.WriteLine(string.Join(" ", numbers));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement