Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clones
  5. {
  6.     public class CloneVersionSystem : ICloneVersionSystem
  7.     {
  8.         private List<Clone> clones;
  9.         public string Execute(string query)
  10.         {
  11.             var allCommands = query.Split();
  12.             var commandName = allCommands[0];
  13.             var cloneId = int.Parse(allCommands[1]);
  14.             var programId = allCommands.Length > 2 ? allCommands[2] : "";
  15.             if (clones.Count < cloneId) return null;
  16.             return RunCommand(commandName, clones[cloneId - 1], programId);
  17.         }
  18.  
  19.         public CloneVersionSystem()
  20.         {
  21.             clones = new List<Clone>{new Clone()};
  22.         }
  23.  
  24.         private string RunCommand(string commandName, Clone clone, string programId)
  25.         {
  26.             string result = null;
  27.             switch (commandName)
  28.             {
  29.                 case "learn":
  30.                     LearnClone(clone, programId);
  31.                     break;
  32.                 case "rollback":
  33.                     RollbackClone(clone);
  34.                     break;
  35.                 case "relearn":
  36.                     RelearnClone(clone);
  37.                     break;
  38.                 case "clone":
  39.                     ToCloneClone(clone);
  40.                     break;
  41.                 case "check":
  42.                     result = CheckClone(clone);
  43.                     break;
  44.             }
  45.             return result;
  46.         }
  47.  
  48.         private void LearnClone(Clone clone, string programId)
  49.         {
  50.             clone.HistoryStack.Clear();
  51.             clone.ProgramsStack.Push(int.Parse(programId));
  52.         }
  53.  
  54.         private void RollbackClone(Clone clone)
  55.         {
  56.             clone.HistoryStack.Push(clone.ProgramsStack.Pop());
  57.         }
  58.  
  59.         private void RelearnClone(Clone clone)
  60.         {
  61.             clone.ProgramsStack.Push(clone.HistoryStack.Pop());
  62.         }
  63.  
  64.         private string CheckClone(Clone clone)
  65.         {
  66.             if (clone.ProgramsStack.IsEmpty()) return "basic";
  67.             return clone.ProgramsStack.Peek()+"";
  68.         }
  69.  
  70.         private void ToCloneClone(Clone clone)
  71.         {
  72.             var newClone = clone.CloneThisClone();
  73.             clones.Add(newClone);
  74.         }
  75.     }
  76.  
  77.     public class Clone
  78.     {
  79.         public Stack ProgramsStack;
  80.         public Stack HistoryStack;
  81.        
  82.         public Clone()
  83.         {
  84.             ProgramsStack = new Stack();
  85.             HistoryStack = new Stack();
  86.         }
  87.        
  88.         public Clone CloneThisClone()
  89.         {
  90.             var other = new Clone
  91.             {
  92.                 ProgramsStack = new Stack(ProgramsStack),
  93.                 HistoryStack = new Stack(HistoryStack)
  94.             };
  95.             return other;
  96.         }
  97.        
  98.         public class Stack
  99.         {
  100.             private StackItem last;
  101.            
  102.             public Stack()
  103.             {
  104.             }
  105.  
  106.             public Stack(Stack stack)
  107.             {
  108.                 last = stack.last;
  109.             }
  110.  
  111.             public void Push(int value)
  112.             {
  113.                 last = new StackItem(value, last);
  114.             }
  115.        using System;
  116. using System.Collections.Generic;
  117.  
  118. namespace Clones
  119. {
  120.     public class CloneVersionSystem : ICloneVersionSystem
  121.     {
  122.         private List<Clone> clones;
  123.         public string Execute(string query)
  124.         {
  125.             var allCommands = query.Split();
  126.             var commandName = allCommands[0];
  127.             var cloneId = int.Parse(allCommands[1]);
  128.             var programId = allCommands.Length > 2 ? allCommands[2] : "";
  129.             if (clones.Count < cloneId) return null;
  130.             return RunCommand(commandName, clones[cloneId - 1], programId);
  131.         }
  132.  
  133.         public CloneVersionSystem()
  134.         {
  135.             clones = new List<Clone>{new Clone()};
  136.         }
  137.  
  138.         private string RunCommand(string commandName, Clone clone, string programId)
  139.         {
  140.             string result = null;
  141.             switch (commandName)
  142.             {
  143.                 case "learn":
  144.                     LearnClone(clone, programId);
  145.                     break;
  146.                 case "rollback":
  147.                     RollbackClone(clone);
  148.                     break;
  149.                 case "relearn":
  150.                     RelearnClone(clone);
  151.                     break;
  152.                 case "clone":
  153.                     ToCloneClone(clone);
  154.                     break;
  155.                 case "check":
  156.                     result = CheckClone(clone);
  157.                     break;
  158.             }
  159.             return result;
  160.         }
  161.  
  162.         private void LearnClone(Clone clone, string programId)
  163.         {
  164.             clone.HistoryStack.Clear();
  165.             clone.ProgramsStack.Push(int.Parse(programId));
  166.         }
  167.  
  168.         private void RollbackClone(Clone clone)
  169.         {
  170.             clone.HistoryStack.Push(clone.ProgramsStack.Pop());
  171.         }
  172.  
  173.         private void RelearnClone(Clone clone)
  174.         {
  175.             clone.ProgramsStack.Push(clone.HistoryStack.Pop());
  176.         }
  177.  
  178.         private string CheckClone(Clone clone)
  179.         {
  180.             if (clone.ProgramsStack.IsEmpty()) return "basic";
  181.             return clone.ProgramsStack.Peek()+"";
  182.         }
  183.  
  184.         private void ToCloneClone(Clone clone)
  185.         {
  186.             var newClone = clone.CloneThisClone();
  187.             clones.Add(newClone);
  188.         }
  189.     }
  190.  
  191.     public class Clone
  192.     {
  193.         public Stack ProgramsStack;
  194.         public Stack HistoryStack;
  195.        
  196.         public Clone()
  197.         {
  198.             ProgramsStack = new Stack();
  199.             HistoryStack = new Stack();
  200.         }
  201.        
  202.         public Clone CloneThisClone()
  203.         {
  204.             var other = new Clone
  205.             {
  206.                 ProgramsStack = new Stack(ProgramsStack),
  207.                 HistoryStack = new Stack(HistoryStack)
  208.             };
  209.             return other;
  210.         }
  211.        
  212.         public class Stack
  213.         {
  214.             private StackItem last;
  215.            
  216.             public Stack()
  217.             {
  218.             }
  219.  
  220.             public Stack(Stack stack)
  221.             {
  222.                 last = stack.last;
  223.             }
  224.  
  225.             public void Push(int value)
  226.             {
  227.                 last = new StackItem(value, last);
  228.             }
  229.        
  230.             public int Pop()
  231.             {
  232.                 var value = last.Value;
  233.                 last = last.Previous;
  234.                 return value;
  235.             }
  236.        
  237.             public void Clear()
  238.             {
  239.                 last = null;
  240.             }
  241.        
  242.             public int Peek()
  243.             {
  244.                 return last.Value;
  245.             }
  246.        
  247.             public bool IsEmpty()
  248.             {
  249.                 return last == null;
  250.             }
  251.         }
  252.  
  253.         public class StackItem
  254.         {
  255.             public readonly int Value;
  256.             public readonly StackItem Previous;
  257.  
  258.             public StackItem(int value, StackItem previous)
  259.             {
  260.                 Value = value;
  261.                 Previous = previous;
  262.             }
  263.         }
  264.     }
  265. }
  266.             public int Pop()
  267.             {
  268.                 var value = last.Value;
  269.                 last = last.Previous;
  270.                 return value;
  271.             }
  272.        
  273.             public void Clear()
  274.             {
  275.                 last = null;
  276.             }
  277.        
  278.             public int Peek()
  279.             {
  280.                 return last.Value;
  281.             }
  282.        
  283.             public bool IsEmpty()
  284.             {
  285.                 return last == null;
  286.             }
  287.         }
  288.  
  289.         public class StackItem
  290.         {
  291.             public readonly int Value;
  292.             public readonly StackItem Previous;
  293.  
  294.             public StackItem(int value, StackItem previous)
  295.             {
  296.                 Value = value;
  297.                 Previous = previous;
  298.             }
  299.         }
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement