From 38025247c7c050617b265627346206dceb26ea08 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Fri, 5 Nov 2021 15:43:53 +0100 Subject: [PATCH] =?UTF-8?q?master=20>=20master:=20code=20go=20-=20f=C3=BCg?= =?UTF-8?q?te=20algorithmus=20hinzu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next_greater_element.go | 107 ++++++++++++++++++ .../next_greater_element_fancy.go | 89 +++++++++++++++ code/golang/internal/endpoints/run/run.go | 8 ++ .../endpoints/run/run_actions_algorithms.go | 25 +++- .../internal/endpoints/run/run_menus.go | 14 +++ 5 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element.go create mode 100644 code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element_fancy.go diff --git a/code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element.go b/code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element.go new file mode 100644 index 0000000..d7e82a0 --- /dev/null +++ b/code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element.go @@ -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 +} diff --git a/code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element_fancy.go b/code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element_fancy.go new file mode 100644 index 0000000..6531fe7 --- /dev/null +++ b/code/golang/internal/algorithms/stacks/next_greater_element/next_greater_element_fancy.go @@ -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 +} diff --git a/code/golang/internal/endpoints/run/run.go b/code/golang/internal/endpoints/run/run.go index 463de90..f62c5e0 100644 --- a/code/golang/internal/endpoints/run/run.go +++ b/code/golang/internal/endpoints/run/run.go @@ -17,6 +17,7 @@ import ( algorithm_search_jump "ads/internal/algorithms/search/jump" algorithm_search_poison "ads/internal/algorithms/search/poison" algorithm_search_sequential "ads/internal/algorithms/search/sequential" + algorithm_stacks_next_greater_element "ads/internal/algorithms/stacks/next_greater_element" algorithm_sum_maxsubsum "ads/internal/algorithms/sum/maxsubsum" ) @@ -138,6 +139,13 @@ func RunNonInteractive(path string) error { } else { err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command) } + case "algorithm-stacks-next-greater-element": + L := inputs.List + if L != nil { + _, err_case = algorithm_stacks_next_greater_element.FancyNextGreaterElement(*L) + } else { + err_case = fmt.Errorf("Fehlende Inputs für Befehl '%[1]s'.", *problem.Command) + } case "algorithm-sum-maxsub": L := inputs.List if L != nil { diff --git a/code/golang/internal/endpoints/run/run_actions_algorithms.go b/code/golang/internal/endpoints/run/run_actions_algorithms.go index b01f1ec..5e6aaf3 100644 --- a/code/golang/internal/endpoints/run/run_actions_algorithms.go +++ b/code/golang/internal/endpoints/run/run_actions_algorithms.go @@ -14,11 +14,12 @@ import ( algorithm_search_jump "ads/internal/algorithms/search/jump" algorithm_search_poison "ads/internal/algorithms/search/poison" algorithm_search_sequential "ads/internal/algorithms/search/sequential" + algorithm_stacks_next_greater_element "ads/internal/algorithms/stacks/next_greater_element" algorithm_sum_maxsubsum "ads/internal/algorithms/sum/maxsubsum" ) /* ---------------------------------------------------------------- * - * ACTIONS - Algorithmen + * ACTIONS algorithmen - search * ---------------------------------------------------------------- */ func actionAlgorithmSearchBinary() (bool, error) { @@ -202,6 +203,28 @@ func actionAlgorithmSearchSequential() (bool, error) { return cancel, nil } +/* ---------------------------------------------------------------- * + * ACTIONS algorithmen - stacks + * ---------------------------------------------------------------- */ + +func actionAlgorithmStacksNextGreaterElement() (bool, error) { + input_L, cancel, err := promptInputListOfInt("L", "Liste von Werten", []string{}) + if cancel || err != nil { + return cancel, err + } + setup.DisplayStartOfCaseBlank() + _, err = algorithm_stacks_next_greater_element.FancyNextGreaterElement(input_L) + if err != nil { + logging.Error(err) + } + setup.DisplayEndOfCase() + return cancel, nil +} + +/* ---------------------------------------------------------------- * + * ACTIONS algorithmen - sum + * ---------------------------------------------------------------- */ + func actionAlgorithmSumMaxsub() (bool, error) { input_L, cancel, err := promptInputListOfInt("L", "Liste von Werten", []string{}) if cancel || err != nil { diff --git a/code/golang/internal/endpoints/run/run_menus.go b/code/golang/internal/endpoints/run/run_menus.go index a2c4688..3060662 100644 --- a/code/golang/internal/endpoints/run/run_menus.go +++ b/code/golang/internal/endpoints/run/run_menus.go @@ -27,6 +27,7 @@ var menuMain = menus.Menu{ }, { {Label: "Suchalgorithmen", Submenu: &menuSearchAlgorithms}, {Label: "Summenalgorithmen", Submenu: &menuSumAlgorithms}, + {Label: "Algorithmen mit Stacks und Queues", Submenu: &menuStacksQueuesAlgorithms}, }, }, Default: -1, @@ -117,6 +118,19 @@ var menuSearchAlgorithms = menus.Menu{ Default: -1, } +var menuStacksQueuesAlgorithms = menus.Menu{ + Path: []string{"Hauptmenü", "Algorithmen mit Stacks und Queues"}, + PromptMessages: []string{ + "Algorithmus wählen", + }, + Options: [][]menus.MenuOption{ + { + {Label: "'Next Greater Element' mit Stacks", Action: actionAlgorithmStacksNextGreaterElement}, + }, + }, + Default: -1, +} + var menuSumAlgorithms = menus.Menu{ Path: []string{"Hauptmenü", "Summenalgorithmen"}, PromptMessages: []string{