master > mater: code - nextGreaterElement, auxiliary methods entfernt

This commit is contained in:
RLogik 2021-11-07 18:56:49 +01:00
parent 3505401c7f
commit 6541a5246d
2 changed files with 9 additions and 48 deletions

View File

@ -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
}

View File

@ -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;