06. List Manipulation Basics
Понеже не открих такава тема във форума, освен на Java, пускам и за C#. Когато тествам с примера по-долу и стигам до командата "Insert 8 3" програмата се чупи-"Index out of range exception". Тествала съм по отделно всяка една команда и работят коректно. Моля за помощ, защото сама не откривам какво да коригирам в кода си, а Judge ми дава Runtime Error на Test#5 и Test #4.
Предварително благодаря!
60/100 https://judge.softuni.bg/Contests/Practice/Index/1210#5
Условие:
Write a program that reads a list of integers. Then until you receive "end", you will receive different commands:
Add {number}: add a number to the end of the list.
Remove {number}: remove a number from the list.
RemoveAt {index}: remove a number at a given index.
Insert {number} {index}: insert a number at a given index.
Note: All the indices will be valid!
When you receive the "end" command, print the final state of the list (separated by spaces).
Input
4 19 2 53 6 43
Add 3
Remove 2
RemoveAt 1
Insert 8 3
end
Output
4 53 6 8 43 3
using System;
using System.Linq;
using System.Collections.Generic;
namespace List
{
    class MainClass
    {
        public static void Main()
        {
            List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
            string command = Console.ReadLine();
            while (command != "end")
            {
              string[] commandArray = command.Split();
                if (commandArray[0] == "Add")
                {
                    int numberToAdd = int.Parse(commandArray[1]);
                    numbers.Add(numberToAdd);  
                }
                else if (commandArray[0] == "Remove")
                {
                    int removeNumber = int.Parse(commandArray[1]);
                    numbers.Remove(removeNumber);
                }
                else if (commandArray[0] == "RemoveAt")
                {
                    int removeAtIndex = int.Parse(commandArray[1]);
                    numbers.RemoveAt(removeAtIndex);
                }
                 else if (commandArray[0] == "Insert")
                {
                    int insertIndex = int.Parse(commandArray[1]);
                    int insertNumber = int.Parse(commandArray[2]);
                    numbers.Insert(insertIndex, insertNumber); 
                }
                command = Console.ReadLine();
            }
            Console.WriteLine(string.Join(" ",numbers));
        }
      }
    }