master > master: code go - fügte algorithmus hinzu

This commit is contained in:
RD
2021-11-05 15:43:53 +01:00
parent 1fd932c0ef
commit 38025247c7
5 changed files with 242 additions and 1 deletions

View File

@@ -0,0 +1,107 @@
package next_greater_element
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
"ads/internal/data_structures/stacks"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
var _output_list [][2]int
/* ---------------------------------------------------------------- *
* ALGORITHM next greater element
* ---------------------------------------------------------------- */
/*
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()
if len(L) == 0 {
return output()
}
S := stacks.CREATE()
for i := 0; i < len(L); i++ {
logging.Debug("Lies Element L[%v] ein", i)
nextElement := L[i]
logging.Debug("Stack S | %v", S)
if !S.EMPTY() {
logging.Debug("Entferne alle top Elemente vom Stack bis >= nextElement")
element := S.TOP()
S.POP()
metrics.AddTimeCost()
/*
Entferne kleinere Elemente vom Stack
Aktuelles Element ist jeweils größerer rechter Partner
*/
for element < nextElement {
logging.Debug("Stack S | %v", S)
addToOutput(element, nextElement)
if S.EMPTY() {
break
}
element = S.TOP()
S.POP()
metrics.AddTimeCost()
}
// lege letztes Element zurück
if element > nextElement {
logging.Debug("Element >= nextElement zurücklegen")
S.PUSH(element)
metrics.AddTimeCost()
}
logging.Debug("Stack S | %v", S)
}
S.PUSH(nextElement)
metrics.AddTimeCost()
logging.Debug("L[%v] auf Stack legen", i)
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() {
metrics.AddTimeCost()
logging.Debug("Stack S | %v", S)
element := S.TOP()
S.POP()
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

@@ -0,0 +1,89 @@
package next_greater_element
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/metrics"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, _ ...interface{}) error {
// TODO
return nil
}
func postChecks(L []int, pairs [][2]int, _ ...interface{}) error {
// TODO
return nil
}
/* ---------------------------------------------------------------- *
* ALGORITHM binary search + Display
* ---------------------------------------------------------------- */
func FancyNextGreaterElement(input_L []int) ([][2]int, error) {
var name = "Binärsuchalgorithmus"
var inputs = map[string]interface{}{
"L": input_L,
}
var outputs map[string]interface{}
var (
pairs [][2]int
)
var err error
do_once := true
for do_once {
do_once = false
// Start Message
setup.DisplayStartOfAlgorithm(name, inputs)
// Prechecks:
if setup.AppConfigPerformChecks() {
err = preChecks(input_L)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
pairs = NextGreaterElement(input_L)
metrics.StopMetrics()
outputs = map[string]interface{}{
"pairs": pairs,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, pairs)
if err != nil {
break
}
}
}
return pairs, err
}