Advertisement
sivancheva

Arrays

Mar 31st, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Arrays
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  12.  
  13. string[] command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  14.  
  15. while (command[0] != "END")
  16. {
  17. if (command[0] == "Reverse") // тук махнах проверката за count==1, защото и да има един символ не е проблем, ще даде просто същият резултат
  18. {
  19. input.Reverse();
  20.  
  21. }
  22. else if (command[0] == "Distinct") // тук също махнах допулниятелният лист, не ти трябва. Просто си махаш повтарящите се елементи от настоящия.
  23. {
  24.  
  25. input = input.Distinct().ToList();
  26.  
  27.  
  28. }
  29. else if (command[0] == "Replace")
  30. {
  31. int index = int.Parse(command[1]);
  32. if (index >= 0 && index <= input.Count - 1) //тук определям кога индекса е валиден и вместо да insert-вам и remove-вам директно на верния индекс присвоявам зададената дума.
  33. {
  34. input[index] = command[2];
  35. }
  36. else
  37. {
  38. Console.WriteLine("Invalid input!");
  39. }
  40. }
  41. else // ако ти подадат команда различна от Reverse, Distinct i Replace трябва да кажеш, че е невалидна
  42. {
  43. Console.WriteLine("Invalid input!");
  44. }
  45.  
  46. command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  47.  
  48. }
  49.  
  50. Console.WriteLine(string.Join(", ", input));
  51.  
  52. }
  53.  
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement