Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace P04ListOperations
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
- while (true)
- {
- string command = Console.ReadLine();
- string[] commandLine = command.Split();
- if (command == "End")
- {
- break;
- }
- Commands(commandLine, numbers);
- }
- Console.WriteLine(String.Join(" ", numbers));
- }
- private static void Commands(string[] commandLine, List<int> numbers)
- {
- if (commandLine[0]== ("Add"))
- {
- numbers.Add(int.Parse(commandLine[1]));
- }
- else if (commandLine[0]== ("Insert"))
- {
- int numberToInsert = int.Parse(commandLine[1]);
- int index = int.Parse(commandLine[2]);
- if (index >= 0 && index < numbers.Count)
- {
- numbers.Insert(index, numberToInsert);
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- }
- else if (commandLine[0]==("Remove"))
- {
- int index = int.Parse(commandLine[1]);
- if (index >= 0 && index < numbers.Count)
- {
- numbers.RemoveAt(index);
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- }
- else if (commandLine[0]==("Shift"))
- {
- string direction = commandLine[1];
- int count = int.Parse(commandLine[2]);
- Shift(numbers, direction, count);
- }
- }
- private static void Shift( List<int> numbers, string direction, int count)
- {
- if (direction == "left")
- {
- for (int i = 0; i < count; i++)
- {
- int tempNumber = numbers[0];
- numbers.Add(tempNumber);
- numbers.RemoveAt(0);
- }
- }
- else if (direction=="right")
- {
- for (int i = 0; i < count; i++)
- {
- int tempNumber = numbers[numbers.Count - 1];
- numbers.Insert(0, tempNumber);
- numbers.RemoveAt(numbers.Count - 1);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement