public static void Demo() { //var stack = new Stack<string>(); var methodLogList = new List <MethodLog>(); //Console.Write("\nMethod Log: methodName, (start or end), timestamp. 0 to stop.\n"); var logs = new string[] { //"Main,start,0", //"Print,start,10", //"Print,end,19", //"Malloc,start,23", //"Print,start,25", //"Print,end,27", //"Malloc,end,30", //"Main,end,32" "0:start: 0", "1:start:2", "1:end:5", "0:end:6" }; var lastTimestamp = int.MinValue; var result = new Dictionary <string, int>(); var stack = new Stack <string>(); foreach (var item in logs) { var CurrentLogLine = item ?? "0"; if (CurrentLogLine.Trim() == "0" || CurrentLogLine.Trim() == "") { break; } var log = new MethodLog(CurrentLogLine); if (log.Timestamp < lastTimestamp) { throw new Exception("Timestamp must be in ascending order"); } if (log.Mode == "start") { if (stack.Count() > 0) //need to add time to the parent caller { if (result.ContainsKey(stack.Peek())) { result[stack.Peek()] += log.Timestamp - lastTimestamp; } } stack.Push(log.MethodName); //create a new entry for the started method if (!result.ContainsKey(log.MethodName)) { result.Add(log.MethodName, 0); } } else //end { var s = stack.Pop(); if (s != log.MethodName) //there should be a start for every end { throw new Exception("Incorrect start end sequence"); } result[s] += log.Timestamp - lastTimestamp; } methodLogList.Add(log); lastTimestamp = log.Timestamp; } Console.WriteLine("\ninput:"); methodLogList.ForEach(t => Console.WriteLine($"{t.MethodName},{t.Mode},{t.Timestamp.ToString()}")); Console.WriteLine("\noutput:"); result.Select(t => t).ToList().ForEach(t => Console.WriteLine($"{t.Key}:{t.Value.ToString()}")); Console.ReadLine(); }
public int solution(string S) { int minInteger = 0; int maxInteger = (1 << 20) - 1; Stack <int> stack = new Stack <int>(); foreach (string op in S.Split(' ')) { if (op == "DUP") { if (!stack.Any()) { return(-1); } stack.Push(stack.Peek()); } else if (op == "POP") { if (!stack.Any()) { return(-1); } stack.Pop(); } else if (op == "+") { if (stack.Count() < 2) { return(-1); } var top = stack.Pop(); var nextTop = stack.Pop(); var sum = top + nextTop; if (sum > maxInteger) { // Overflow return(-1); } stack.Push(sum); } else if (op == "-") { if (stack.Count() < 2) { return(-1); } var top = stack.Pop(); var nextTop = stack.Pop(); var diff = top - nextTop; if (diff < minInteger) { // Overflow return(-1); } stack.Push(diff); } else { int number; if (!int.TryParse(op, out number)) { return(-1); } if (number < minInteger || number > maxInteger) { return(-1); } stack.Push(number); } } if (!stack.Any()) { return(-1); } return(stack.Pop()); }
public int solution(string S) { int minInteger = 0; int maxInteger = (1 << 20) - 1; Stack<int> stack = new Stack<int>(); foreach (string op in S.Split(' ')) { if (op == "DUP") { if (!stack.Any()) { return -1; } stack.Push(stack.Peek()); } else if (op == "POP") { if (!stack.Any()) { return -1; } stack.Pop(); } else if (op == "+") { if (stack.Count() < 2) { return -1; } var top = stack.Pop(); var nextTop = stack.Pop(); var sum = top + nextTop; if (sum > maxInteger) { // Overflow return -1; } stack.Push(sum); } else if (op == "-") { if (stack.Count() < 2) { return -1; } var top = stack.Pop(); var nextTop = stack.Pop(); var diff = top - nextTop; if (diff < minInteger) { // Overflow return -1; } stack.Push(diff); } else { int number; if (!int.TryParse(op, out number)) { return -1; } if (number < minInteger || number > maxInteger) { return -1; } stack.Push(number); } } if (!stack.Any()) { return -1; } return stack.Pop(); }
void DeleteNode() { DiskBNode <T> Parent = new DiskBNode <T>(delegate_toT, ValueLength, Degree); DiskBNode <T> Brother = new DiskBNode <T>(delegate_toT, ValueLength, Degree); string Line = FindNode(ParentID); Parent.ToTObj(Line); Stack <int> temp = new Stack <int>(); Stack <int> temp2 = new Stack <int>(); int count; T ParentValue; int ParentValueIndex; //volver a llenar BNodeSons do { temp.Push(Parent.BNodeSons.Pop()); } while (temp.Peek() != ActualID); ParentValueIndex = temp.Count - 1; if (Parent.BNodeSons.Count != 0 && Parent.BNodeSons.Peek() != 0) { Line = FindNode(Parent.BNodeSons.Peek()); Brother.ToTObj(Line); count = GreatestValues.Count(); ParentValue = Parent.BNodeValues.GetByIndex(ParentValueIndex); Brother.Insert(ParentValue); Parent.Insert(ParentValue); for (int i = 0; i < count; i++) { Brother.Insert(GreatestValues.Pop()); } temp.Pop(); count = temp.Count; for (int i = 0; i < count; i++) { Parent.BNodeSons.Push(temp.Pop()); } count = GreatestSons.Count; for (int i = 0; i < count; i++) { Brother.BNodeSons.Push(GreatestSons.Pop()); } count = temp2.Count; for (int i = 0; i < count; i++) { Brother.BNodeSons.Push(temp2.Pop()); } } else { Parent.BNodeSons.Push(temp.Pop()); Line = FindNode(temp.Peek()); Brother.ToTObj(Line); count = GreatestValues.Count(); ParentValue = Parent.BNodeValues.GetByIndex(ParentValueIndex); Brother.Insert(ParentValue); Parent.Insert(ParentValue); for (int i = 0; i < count; i++) { Brother.Insert(GreatestValues.Pop()); } temp.Pop(); count = temp.Count; for (int i = 0; i < count; i++) { Parent.BNodeSons.Push(temp.Pop()); } count = Brother.BNodeSons.Count; for (int i = 0; i < count; i++) { temp2.Push(Brother.BNodeSons.Pop()); } count = GreatestSons.Count; for (int i = 0; i < count; i++) { Brother.BNodeSons.Push(GreatestSons.Pop()); } } RewriteNode(Parent.ID, Parent.ToFixedLengthText()); bool newRoot = !RecursiveDelete(ParentValue.Key, Parent.ID, true); if (newRoot) { RootID = Brother.ID; Brother.Parent = 0; } count = Brother.BNodeSons.Count; for (int i = 0; i < count; i++) { SonsUpdate(Brother.BNodeSons.Peek(), Brother.ID); temp2.Push(Brother.BNodeSons.Pop()); } count = temp2.Count; for (int i = 0; i < count; i++) { Brother.BNodeSons.Push(temp2.Pop()); } RewriteNode(Brother.ID, Brother.ToFixedLengthText()); }