diff --git a/code/golang/pkg/algorithms/stacks/next_greater_element/next_greater_element.go b/code/golang/pkg/algorithms/stacks/next_greater_element/next_greater_element.go index 1ec5fe0..b07ef4d 100644 --- a/code/golang/pkg/algorithms/stacks/next_greater_element/next_greater_element.go +++ b/code/golang/pkg/algorithms/stacks/next_greater_element/next_greater_element.go @@ -14,7 +14,7 @@ import ( * GLOBAL VARIABLES/CONSTANTS * ---------------------------------------------------------------- */ -var _output_list [][2]int +// /* ---------------------------------------------------------------- * * ALGORITHM next greater element @@ -26,7 +26,7 @@ Inputs: L = Liste von Zahlen, x = Zahl. Outputs: Liste von Paaren von Elementen und ihrem nächsten größeren Element */ func NextGreaterElement(L []int) [][2]int { - clearOutput() + output := [][2]int{} S := stacks.CREATE() S.INIT() @@ -43,7 +43,7 @@ func NextGreaterElement(L []int) [][2]int { if element < nextElement { // falls top Element < next Element, zum Output hinzufügen und vom Stack entfernen logging.Debug("Stack S | %v; top Element > nextElement; ==> pop und Paar zum Output hinzufügen", S) - addToOutput(element, nextElement) + output = append(output, [2]int{element, nextElement}) S.POP() metrics.AddMovesCost() logging.Debug("Stack S | %v", S) @@ -70,31 +70,12 @@ func NextGreaterElement(L []int) [][2]int { for !S.EMPTY() { // ACHTUNG: schreibe 'while' im Pseudocode, denn dies ist eine while-Schleife in golang logging.Debug("Stack S | %v", S) element := S.TOP() + output = append(output, [2]int{element, -1}) S.POP() metrics.AddMovesCost() - addToOutput(element, -1) } logging.Debug("Stack S | %v", S) - return output() -} - -/* ---------------------------------------------------------------- * - * AUXILIARY METHODS - * ---------------------------------------------------------------- */ - -func clearOutput() { - _output_list = [][2]int{} -} - -func addToOutput(m int, n int) { - _output_list = append(_output_list, [2]int{m, n}) -} - -func output() [][2]int { - var output = make([][2]int, len(_output_list)) - copy(output, _output_list) - clearOutput() return output } diff --git a/code/python/src/algorithms/stacks/next_greater_element.py b/code/python/src/algorithms/stacks/next_greater_element.py index 49bb23d..d164b05 100644 --- a/code/python/src/algorithms/stacks/next_greater_element.py +++ b/code/python/src/algorithms/stacks/next_greater_element.py @@ -16,7 +16,7 @@ from src.algorithms.methods import *; # GLOBAL VARIABLES/CONSTANTS # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -_output_list: List[Tuple[int,int]] +# # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # CHECKS @@ -40,7 +40,7 @@ def NextGreaterElement(L: List[int]) -> List[Tuple[int,int]]: Inputs: L = Liste von Zahlen, x = Zahl. Outputs: Liste von Paaren von Elementen und ihrem nächsten größeren Element ''' - clearOutput(); + output = []; S = Stack(); S.INIT(); @@ -58,7 +58,7 @@ def NextGreaterElement(L: List[int]) -> List[Tuple[int,int]]: # falls element < next Element, zum Output hinzufügen und vom Stack entfernen if element < nextElement: logDebug('Stack S | {S}; top Element > nextElement; ==> pop und Paar zum Output hinzufügen'.format(S=S)) - addToOutput(element, nextElement); + output.append((element, nextElement)); S.POP(); AddMovesCost(); logDebug('Stack S | {S}'.format(S=S)); @@ -85,27 +85,7 @@ def NextGreaterElement(L: List[int]) -> List[Tuple[int,int]]: element = S.TOP(); S.POP(); AddMovesCost(); - addToOutput(element, -1); + output.append((element, -1)); logDebug('Stack S | {S}'.format(S=S)) - return output(); - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# AUXILIARY METHODS -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -def clearOutput(): - global _output_list; - _output_list = []; - return; - -def addToOutput(m: int, n: int): - global _output_list; - _output_list.append((m, n)) - return; - -def output() -> List[Tuple[int, int]]: - global _output_list; - output = _output_list[:] # erstelle Kopie - clearOutput() - return output + return output;