Advertisement
dragonbs

SoftUniCoursePlaning

Feb 17th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static List<string> Add(List<string> list, string title)
  9.     {
  10.         list.Add(title);
  11.         return list;
  12.     }
  13.  
  14.     static List<string> Insert(List<string> list, string title, int index)
  15.     {
  16.         if (list.Contains(title)) return list;
  17.  
  18.         list.Insert(index, title);
  19.         return list;
  20.     }
  21.  
  22.     static List<string> Remove(List<string> list, string title)
  23.     {
  24.         if (!list.Contains(title)) return list;
  25.  
  26.         list.Remove(title);
  27.         return list;
  28.  
  29.     }
  30.  
  31.     static List<string> Swap(List<string> list, string firstTitle, string secondtitle)
  32.     {
  33.         if (list.Contains(firstTitle) && list.Contains(secondtitle))
  34.         {
  35.             int firstIndex = -1;
  36.             int secondIndex = -1;
  37.             for (int i = 0; i < list.Count; i++)
  38.             {
  39.                 if (list[i] == firstTitle) firstIndex = i;
  40.                 else if (list[i] == secondtitle) secondIndex = i;
  41.             }
  42.             string temp;
  43.             temp = list[firstIndex];
  44.             list[firstIndex] = list[secondIndex];
  45.             list[secondIndex] = temp;
  46.         }
  47.         return list;
  48.     }
  49.  
  50.     static List<string> Exercise(List<string> list, string title)
  51.     {
  52.         if (!list.Contains(title)) list.Add(title);
  53.         if (list.Contains(title + "-Exercise")) return list;
  54.         int titleIndex = -1;
  55.         for (int i = 0; i < list.Count; i++)
  56.             if (list[i] == title) titleIndex = i;
  57.  
  58.         list.Insert(titleIndex + 1, title + "-Exercise");
  59.         return list;
  60.     }
  61.  
  62.     static List<string> RearrangeExercises(List<string> list)
  63.     {
  64.         for (int i = 0; i < list.Count - 1; i++)
  65.             if (list[i + 1].Contains("-Exercise") && list[i + 1] != list[i] + "-Exercise")
  66.                 for (int j = 0; j < list.Count; j++)
  67.                     if (list[i + 1] == list[j] + "-Exercise")
  68.                     {
  69.                         string temp = list[i + 1];
  70.                         list.RemoveAt(i + 1);
  71.                         list.Insert(j + 1, temp);
  72.                     }
  73.         return list;
  74.     }
  75.  
  76.     static void Main()
  77.     {
  78.         List<string> list = Console.ReadLine().Split(',').ToList();
  79.  
  80.         // Clearing spaces in the strings
  81.         for (int i = 1; i < list.Count; i++)
  82.         {
  83.             StringBuilder sb = new StringBuilder();
  84.             sb.Append(list[i]);
  85.             sb.Remove(0, 1);
  86.             list[i] = sb.ToString();
  87.         }
  88.         string input;
  89.  
  90.         while ((input = Console.ReadLine()) != "course start")
  91.         {
  92.             string[] command = input.Split(':').ToArray();
  93.             if (command[0] == "Add") list = Add(list, command[1]);
  94.             else if (command[0] == "Insert") list = Insert(list, command[1], int.Parse(command[2]));
  95.             else if (command[0] == "Remove") list = Remove(list, command[1]);
  96.             else if (command[0] == "Swap") list = Swap(list, command[1], command[2]);
  97.             else if (command[0] == "Exercise") list = Exercise(list, command[1]);
  98.             list = RearrangeExercises(list);
  99.         }
  100.  
  101.         for (int i = 1; i <= list.Count; i++)
  102.             Console.WriteLine($"{i}.{list[i - 1]}");
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement