master > master: code - next greater element messages leicht verbessert

This commit is contained in:
RLogik 2021-11-07 08:50:12 +01:00
parent 8d2de4fa2b
commit cccc6a14ba
3 changed files with 32 additions and 25 deletions

View File

@ -27,32 +27,32 @@ Outputs: Liste von Paaren von Elementen und ihrem nächsten größeren Element
*/
func NextGreaterElement(L []int) [][2]int {
clearOutput()
if len(L) == 0 {
return output()
}
S := stacks.CREATE()
S.INIT()
for i := 0; i < len(L); i++ {
logging.Debug("Stack S | %v", S)
logging.Debug("Nächstes List-Element L[%v] = %v betrachten", i, L[i])
nextElement := L[i]
logging.Debug("Alle top Elemente vom Stack, die < nextElement sind, mit L[i] paaren")
// Führe aus, bis top Element >= nextElement oder Stack leer ist.
// Führe aus, bis top Element >= nextElement oder Stack leer ist.
logging.Debug("Stack S | %v", S)
for !S.EMPTY() { // ACHTUNG: dies ist eine while-Schleife in golang
for !S.EMPTY() { // ACHTUNG: schreibe 'while' im Pseudocode, denn dies ist eine while-Schleife in golang
element := S.TOP()
S.POP()
metrics.AddMovesCost()
logging.Debug("Stack S | %v; (popped) Top-Element = %v", S, element)
// falls element < next Element, zum Output hinzufügen
if element < nextElement {
logging.Debug("Stack S | %v; top Element > nextElement; ==> pop und Paar zum Output hinzufügen", S)
// falls top Element < next Element, zum Output hinzufügen und vom Stack
addToOutput(element, nextElement)
// sonst Element auf Stack zurücklegen und Schleife abbrechen.
S.POP()
metrics.AddMovesCost()
logging.Debug("Stack S | %v", S)
} else if element > nextElement {
// falls top Element > next Element, auf Stack lassen und Schleife abbrechen.
break
} else {
logging.Debug("Stack-Element >= nextElement ==> zurücklegen")
S.PUSH(element)
// falls top Element == next Element, vom Stack entfernen und Schleife abbrechen.
S.POP()
metrics.AddMovesCost()
break
}
@ -60,19 +60,21 @@ func NextGreaterElement(L []int) [][2]int {
logging.Debug("L[%v] auf Stack legen", i)
S.PUSH(nextElement)
logging.Debug("Stack S | %v", S)
metrics.AddMovesCost()
}
logging.Debug("Stack S | %v", S)
// was übrig bleibt hat kein größeres Element
logging.Debug("Alles übrige auf Stack hat kein nächstes größeres Element")
for !S.EMPTY() { // ACHTUNG: dies ist eine while-Schleife in golang
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()
S.POP()
metrics.AddMovesCost()
addToOutput(element, -1)
}
logging.Debug("Stack S | %v", S)
return output()

View File

@ -34,7 +34,7 @@ func postChecks(L []int, pairs [][2]int, _ ...interface{}) error {
* ---------------------------------------------------------------- */
func FancyNextGreaterElement(input_L []int) ([][2]int, error) {
var name = "Binärsuchalgorithmus"
var name = "Next Greater Element"
var inputs = map[string]interface{}{
"L": input_L,
}

View File

@ -46,6 +46,7 @@ def NextGreaterElement(L: List[int]) -> List[Tuple[int,int]]:
S.INIT();
for i in range(len(L)):
logDebug('Stack S | {S}'.format(S=S));
logDebug('Nächstes List-Element L[{i}] = {el} betrachten'.format(i=i, el=L[i]));
nextElement = L[i];
@ -54,25 +55,29 @@ def NextGreaterElement(L: List[int]) -> List[Tuple[int,int]]:
logDebug('Stack S | {S}'.format(S=S));
while not S.EMPTY():
element = S.TOP();
S.POP();
AddMovesCost();
logDebug('Stack S | {S}; (popped) Top-Element = {el}'.format(S=S, el=element))
# falls element < next Element, zum Output hinzufügen
if element < nextElement:
logDebug('Stack S | {S}; top Element > nextElement; ==> pop und Paar zum Output hinzufügen'.format(S=S))
addToOutput(element, nextElement);
# sonst Element auf Stack zurücklegen und Schleife abbrechen.
else:
logDebug('Stack element >= nextElement ==> zurücklegen')
S.PUSH(element);
S.POP();
AddMovesCost();
break;
logDebug('Stack S | {S}'.format(S=S));
# falls top Element > next Element, auf Stack lassen und Schleife abbrechen.
elif element > nextElement:
break
# falls top Element == next Element, vom Stack entfernen und Schleife abbrechen.
else:
S.POP()
AddMovesCost()
break
logDebug('Stack S | {S}'.format(S=S));
logDebug('L[{i}] auf Stack legen'.format(i=i));
S.PUSH(nextElement);
logDebug('Stack S | {S}'.format(S=S));
AddMovesCost();
logDebug('Stack S | {S}'.format(S=S));
# was übrig bleibt hat kein größeres Element
logDebug('Alles übrige auf Stack hat kein nächstes größeres Element')
while not S.EMPTY():