Prohause

CommandInterpreter

Jul 17th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5.  
  6. namespace CommandInterpreter
  7.  
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<string> seq = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList();
  14. while (true)
  15. {
  16. var command = Console.ReadLine();
  17. if (command == "end")
  18. {
  19. break;
  20. }
  21.  
  22. string[] commands = command.Split();
  23. string instruction = commands[0];
  24. long start = 0;
  25. long count = 0;
  26. if (instruction == "reverse" || instruction == "sort")
  27. {
  28. start = long.Parse(commands[2]);
  29. count = long.Parse(commands[4]);
  30. if (start < 0 || start + count > seq.Count || count < 0 || start > seq.Count - 1)
  31. {
  32. Console.WriteLine("Invalid input parameters.");
  33. }
  34. else
  35. {
  36. seq = Reverse(seq, start, count, instruction);
  37. }
  38. }
  39. }
  40. Console.WriteLine($"[{String.Join(", ", seq)}]");
  41. }
  42.  
  43. private static List<string> Reverse(List<string> seq, long start, long count, string instruction)
  44. {
  45. List<string> leftPart = seq.Take((int)start).ToList();
  46. List<string> rightPart = seq.Skip((int)(start + count)).ToList();
  47. List<string> toTreat = seq.Skip((int)start).Take((int)count).ToList();
  48.  
  49. if (instruction == "reverse")
  50. {
  51. toTreat.Reverse();
  52.  
  53. }
  54. else if (instruction == "sort")
  55. {
  56. toTreat.Sort();
  57. }
  58. List<string> treated = new List<string>();
  59. treated.AddRange(leftPart);
  60. treated.AddRange(toTreat);
  61. treated.AddRange(rightPart);
  62.  
  63. return treated;
  64.  
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment