master > master: code go - ordner von internal -> pkg umgezogen

This commit is contained in:
RD
2021-11-05 15:48:06 +01:00
parent dbdb6a8480
commit 9237dbfad0
20 changed files with 20 additions and 20 deletions

View File

@@ -1,54 +0,0 @@
package binary
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM binary search
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl.
Annahme: L sei aufsteigend sortiert.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func BinarySearch(L []int, x int) int {
if len(L) == 0 {
logging.Debug("x nicht in L.")
return -1
}
metrics.AddTimeCost()
m := int(len(L) / 2)
if L[m] == x {
logging.Debug("Suche in %v .. %v; m = %v; L[m] = x ==> Element gefunden.", 0, len(L)-1, m)
return m
} else if len(L) == 1 {
logging.Debug("L enthält 1 Element; L[m] =/= x; ==> x nicht in L.")
return -1
} else if x < L[m] {
logging.Debug("Suche in %v .. %v; m = %v; L[m] = %v > x ==> Suche in linker Hälfte fortsetzen.", 0, len(L)-1, m, L[m])
index := BinarySearch(L[:m], x)
return index
} else { // } else if x > L[m] {
logging.Debug("Suche in %v .. %v; m = %v; L[m] = %v < x ==> Suche in rechter Hälfte fortsetzen.", 0, len(L)-1, m, L[m])
index := BinarySearch(L[m+1:], x)
if index == -1 {
return -1 // wenn nicht gefunden
}
return index + (m + 1) // NOTE: muss Indexwert kompensieren
}
}

View File

@@ -1,106 +0,0 @@
package binary
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"ads/internal/core/metrics"
"ads/internal/core/utils"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, _ ...interface{}) error {
if !utils.IsSortedListInt(L) {
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
}
return nil
}
func postChecks(L []int, x int, index int, _ ...interface{}) error {
if utils.ArrayContains(L, x) {
if !(index >= 0) {
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
}
if L[index] != x {
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
}
} else {
if index != -1 {
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
}
}
return nil
}
/* ---------------------------------------------------------------- *
* ALGORITHM binary search + Display
* ---------------------------------------------------------------- */
func FancyBinarySearch(input_L []int, input_x int) (int, error) {
var name = "Binärsuchalgorithmus"
var inputs = map[string]interface{}{
"L": input_L,
"x": input_x,
}
var outputs map[string]interface{}
var (
output_index 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, input_x)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = BinarySearch(input_L, input_x)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_x, output_index)
if err != nil {
break
}
}
}
return output_index, err
}

View File

@@ -1,67 +0,0 @@
package interpol
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"math"
"ads/internal/core/logging"
"ads/internal/core/metrics"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM interpolation
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl.
Annahme: L sei aufsteigend sortiert.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func InterpolationSearch(L []int, x int, u int, v int) int {
if len(L) == 0 {
logging.Debug("Liste L leer, also x nicht in L")
return -1
} else if !(L[u] <= x && x <= L[v]) {
logging.Debug("x liegt außerhalb der Grenzen von L")
return -1
}
metrics.AddTimeCost()
p := getSuchposition(L, x, u, v)
if L[p] == x {
logging.Debug("Interpolante in (%[1]v, %[2]v) ist p = %[3]v; L[p] == x; ===> Element gefunden", u, v, p)
return p
} else if x < L[p] {
logging.Debug("Interpolante in (%[1]v, %[2]v) ist p = %[3]v; L[p] > x; ===> suche in linker Hälfte", u, v, p)
return InterpolationSearch(L, x, u, p-1)
} else { // } else if x > L[p] {
logging.Debug("Interpolante in (%[1]v, %[2]v) ist p = %[3]v; L[p] < x; ===> suche in rechter Hälfte", u, v, p)
return InterpolationSearch(L, x, p+1, v)
}
}
/* ---------------------------------------------------------------- *
* ALGORITHM interpolation
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
Outputs: Interpolierte Position, um Suchinterval ausgeglichen aufzuteilen.
*/
func getSuchposition(L []int, x int, u int, v int) int {
metrics.AddTimeCost()
r := float64(x-L[u]) / float64(L[v]-L[u])
p := int(math.Floor(float64(u) + r*float64(v-u)))
return p
}

View File

@@ -1,108 +0,0 @@
package interpol
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"ads/internal/core/metrics"
"ads/internal/core/utils"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, _ ...interface{}) error {
if !utils.IsSortedListInt(L) {
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
}
return nil
}
func postChecks(L []int, x int, index int, _ ...interface{}) error {
if utils.ArrayContains(L, x) {
if !(index >= 0) {
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
}
if L[index] != x {
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
}
} else {
if index != -1 {
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
}
}
return nil
}
/* ---------------------------------------------------------------- *
* ALGORITHM interpolation + Display
* ---------------------------------------------------------------- */
func FancyInterpolationSearch(input_L []int, input_x int, input_u int, input_v int) (int, error) {
var name = "Interpolationssuchalgorithmus"
var inputs = map[string]interface{}{
"L": input_L,
"x": input_x,
"u": input_u,
"v": input_v,
}
var outputs map[string]interface{}
var (
output_index 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, input_x, input_u, input_v)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = InterpolationSearch(input_L, input_x, input_u, input_v)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_x, output_index)
if err != nil {
break
}
}
}
return output_index, err
}

View File

@@ -1,94 +0,0 @@
package ith_element
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
"ads/internal/core/utils"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM find ith smallest
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, i = Ordinalzahl
Annahmen:
- L enthält keine Duplikate.
- L enthält mindestens i Elemente.
Outputs: Wert des i. kleinste Element in L.
Beachte 1.kleinstes <==> Minimum.
*/
func FindIthSmallest(L []int, i int) int {
n := len(L)
// extrahiere Wert + Index des Minimums - bedarf n Schritte
metrics.AddTimeCost(n)
index := utils.ArgMinInt(L)
minValue := L[index]
// Falls i = 1, dann wird das Minimum gesucht, sonst Minimum entfernen und nach i-1. Element suchen
if i == 1 {
logging.Debug("Das i. kleinste Element wurde gefunden.")
return minValue
} else {
logging.Debug("Entferne Minimum: %[1]v.", minValue)
i = i - 1
L_ := utils.PopIndexListInt(L, index) // entferne Element mit Index = index
return FindIthSmallest(L_, i)
}
}
/* ---------------------------------------------------------------- *
* ALGORITHM find ith smallest (D & C)
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, i = Ordinalzahl
Annahmen:
- L enthält keine Duplikate.
- L enthält mindestens i Elemente.
Outputs: Wert des i. kleinste Element in L.
Beachte 1.kleinstes <==> Minimum.
*/
func FindIthSmallestDC(L []int, i int) int {
metrics.AddTimeCost()
p := L[len(L)-1] // NOTE: Pivotelement kann beliebig gewählt werden
// Werte in L in linke und rechte Teillisten um das Pivotelement aufteilen:
Ll := []int{} // wird alle Elemente in L < p enthalten
Lr := []int{} // wird alle Elemente in L > p enthalten
for i := 0; i < len(L); i++ {
x := L[i]
if x < p {
Ll = append(Ll, x)
} else if x > p {
Lr = append(Lr, x)
}
}
// Fallunterscheidung:
if len(Ll) == i-1 {
logging.Debug("Es gibt i-1 Elemente vor p=%[1]v. ==> i. kleinste Element = p", p)
return p
} else if len(Ll) >= i {
logging.Debug("Es gibt >= i Elemente vor p=%[1]v. ==> Suche in linker Hälfte!", p)
return FindIthSmallestDC(Ll, i)
} else {
logging.Debug("Es gibt < i-1 Elemente vor p=%[1]v. ==> Suche in rechter Hälfte!", p)
i = i - (len(Ll) + 1)
return FindIthSmallestDC(Lr, i)
}
}

View File

@@ -1,163 +0,0 @@
package ith_element
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"sort"
"ads/internal/core/metrics"
"ads/internal/core/utils"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, i int, _ ...interface{}) error {
if !(1 <= i && i <= len(L)) {
return fmt.Errorf("Der Wert von i muss zw. %[1]v und %[2]v liegen.", 1, len(L))
} else if !utils.ContainsNoDuplicatesListInt(L) {
return fmt.Errorf("Ungültiger Input: L darf keine Duplikate enthalten!")
}
return nil
}
func postChecks(L []int, i int, value int, _ ...interface{}) error {
L_ := make([]int, len(L))
copy(L_, L)
sort.Ints(L_)
if !(L_[i-1] == value) {
return fmt.Errorf("Das i=%[1]v. kleinste Element ist %[2]v und nicht %[3]v.", i, L_[i-1], value)
}
return nil
}
/* ---------------------------------------------------------------- *
* ALGORITHM find ith smallest + Display
* ---------------------------------------------------------------- */
func FancyFindIthSmallest(input_L []int, input_i int) (int, error) {
var name = "Auswahlproblem (i. kleinstes Element)"
var inputs = map[string]interface{}{
"L": input_L,
"i": input_i,
}
var outputs map[string]interface{}
var (
output_value 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, input_i)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_value = FindIthSmallest(input_L, input_i)
metrics.StopMetrics()
outputs = map[string]interface{}{
"value": output_value,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_i, output_value)
if err != nil {
break
}
}
}
return output_value, err
}
/* ---------------------------------------------------------------- *
* ALGORITHM find ith smallest (D & C) + Display
* ---------------------------------------------------------------- */
func FancyFindIthSmallestDC(input_L []int, input_i int) (int, error) {
var name = "Auswahlproblem (i. kleinstes Element, D & C)"
var inputs = map[string]interface{}{
"L": input_L,
"i": input_i,
}
var outputs map[string]interface{}
var (
output_value 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, input_i)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_value = FindIthSmallestDC(input_L, input_i)
metrics.StopMetrics()
outputs = map[string]interface{}{
"value": output_value,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_i, output_value)
if err != nil {
break
}
}
}
return output_value, err
}

View File

@@ -1,96 +0,0 @@
package jump
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/algorithms/search/sequential"
"ads/internal/core/logging"
"ads/internal/core/metrics"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM jump search
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
Annahmen:
- L sei aufsteigend sortiert.
- Idealerweise: L enthält keine Duplikate.
- Idealerweise: Abstände zw. Elementen nicht uniform.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func JumpSearchLinear(L []int, x int, m int) int {
i0 := 0
i1 := m
// ACHTUNG: dies ist eine while-Schleife ist golang:
for i0 < len(L) {
metrics.AddTimeCost()
if i1 > len(L) {
i1 = len(L)
}
block := L[i0:i1]
elementAfterBlock := block[len(block)-1] + 1
if x < elementAfterBlock {
logging.Debug("Element muss sich im Block [%[1]v, %[2]v) befinden.", i0, i1)
index := sequential.SequentialSearch(block, x)
if index == -1 {
return -1 // wenn nicht gefunden
}
return index + i0 // NOTE: muss wegen Offset kompensieren
}
logging.Debug("Element befindet sich nicht im Block [%[1]v, %[2]v).", i0, i1)
i0 = i1
i1 += m
}
return -1
}
/* ---------------------------------------------------------------- *
* ALGORITHM jump search - exponentiell
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl.
Annahmen:
- L sei aufsteigend sortiert.
- L enthält keine Duplikate.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func JumpSearchExponentiell(L []int, x int) int {
i0 := 0
i1 := 1
// ACHTUNG: dies ist eine while-Schleife ist golang:
for i0 < len(L) {
metrics.AddTimeCost()
if i1 > len(L) {
i1 = len(L)
}
block := L[i0:i1]
elementAfterBlock := block[len(block)-1] + 1
if x < elementAfterBlock {
logging.Debug("Element muss sich im Block [%[1]v, %[2]v) befinden.", i0, i1)
index := sequential.SequentialSearch(block, x)
if index == -1 {
return -1 // wenn nicht gefunden
}
return index + i0 // NOTE: muss wegen Offset kompensieren
}
logging.Debug("Element befindet sich nicht im Block [%[1]v, %[2]v).", i0, i1)
i0 = i1
i1 *= 2
}
return -1
}

View File

@@ -1,168 +0,0 @@
package jump
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"ads/internal/core/metrics"
"ads/internal/core/utils"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, _ ...interface{}) error {
if !utils.IsSortedListInt(L) {
return fmt.Errorf("Ungültiger Input: L muss aufsteigend sortiert sein!")
}
// NOTE: nicht prüfen, ob Duplikate existieren. Das ist nur eine erwünschte aber keine notwendige Annahme.
return nil
}
func postChecks(L []int, x int, index int, _ ...interface{}) error {
if utils.ArrayContains(L, x) {
if !(index >= 0) {
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
}
if L[index] != x {
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
}
} else {
if index != -1 {
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
}
}
return nil
}
/* ---------------------------------------------------------------- *
* ALGORITHM jump search + Display
* ---------------------------------------------------------------- */
func FancyJumpSearchLinear(input_L []int, input_x int, input_m int) (int, error) {
var name = "Sprungsuche"
var inputs = map[string]interface{}{
"L": input_L,
"x": input_x,
"m": input_m,
}
var outputs map[string]interface{}
var (
output_index 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, input_x, input_m)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = JumpSearchLinear(input_L, input_x, input_m)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_x, output_index)
if err != nil {
break
}
}
}
return output_index, err
}
/* ---------------------------------------------------------------- *
* ALGORITHM jump search - exponentiell + Display
* ---------------------------------------------------------------- */
func FancyJumpSearchExponentiell(input_L []int, input_x int) (int, error) {
var name = "Sprungsuche (exponentiell)"
var inputs = map[string]interface{}{
"L": input_L,
"x": input_x,
}
var outputs map[string]interface{}
var (
output_index 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, input_x)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = JumpSearchExponentiell(input_L, input_x)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_x, output_index)
if err != nil {
break
}
}
}
return output_index, err
}

View File

@@ -1,171 +0,0 @@
package poison
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
"ads/internal/core/utils"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//
/* ---------------------------------------------------------------- *
* ALGORITHM find poison
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/*
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
Annahme: Genau ein Getränk sei vergiftet.
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
NOTE: Zeitkosten hier messen nur die Anzahl der Vorkoster.
*/
func FindPoison(L []int) int {
logging.Debug("Bereite Vorkoster vor")
n := len(L)
testers := [][]int{}
for i := 0; i < n; i++ {
metrics.AddSpaceCost()
logging.Debug("Füge Vorkoster hinzu, der nur Getränk %[1]v testet.", i)
testers = append(testers, []int{i})
}
logging.Debug("Warte auf Effekte")
effects := waitForEffects(L, testers)
logging.Debug("Effekte auswerten, um vergiftete Getränke zu lokalisieren.")
poisened := evaluateEffects(testers, effects)
if len(poisened) > 0 {
return poisened[0]
}
return -1
}
/* ---------------------------------------------------------------- *
* ALGORITHM find poison fast
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/*
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
Annahme: Genau ein Getränk sei vergiftet.
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
NOTE: Zeitkosten hier messen nur die Anzahl der Vorkoster.
*/
func FindPoisonFast(L []int) int {
logging.Debug("Bereite Vorkoster vor")
n := len(L)
p := utils.LengthOfBinary(n)
testers := [][]int{}
// Für jedes Bit i=0 bis p-1 ...
for i := 0; i < p; i++ {
tester0 := []int{}
tester1 := []int{}
for k := 0; k < n; k++ {
if utils.NthBit(k, i) == 0 {
tester0 = append(tester0, k)
} else {
tester1 = append(tester1, k)
}
}
/*
* NOTE: tester1 ist virtuell: aus den Effekten auf tester0 und den Annahmen
* lassen sich die Effekte auf tester0 erschließen.
* Darum zählen wir nicht 2 sondern 1 Vorkoster.
*/
metrics.AddSpaceCost(1)
logging.Debug("Füge Vorkoster hinzu, der alle Getränke k testet mit %[1]v. Bit von k = 0.", i)
testers = append(testers, tester0)
testers = append(testers, tester1)
}
logging.Debug("Warte auf Effekte")
effects := waitForEffects(L, testers)
logging.Debug("Effekte auswerten, um vergiftete Getränke zu lokalisieren.")
poisened := evaluateEffects(testers, effects)
if len(poisened) > 0 {
return poisened[0]
}
return -1
}
/* ---------------------------------------------------------------- *
* AUXILIARY METHOD wait for effects, evaluate effects
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/*
Inputs:
- L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
- testers = Liste von Vorkostern. Jeder Vorkoster kostet eine 'Teilliste' der Getränke.
Outputs: effects = eine Liste, die jedem Vorkoster zuordnet, wie viele vergiftete Getränke er konsumiert hat.
*/
func waitForEffects(L []int, testers [][]int) []int {
m := len(testers)
effects := make([]int, m)
for i, tester := range testers {
effect := 0
for _, k := range tester {
effect += L[k]
}
effects[i] = effect
}
return effects
}
/*
Inputs:
- testers = Liste von Vorkostern. Jeder Vorkoster kostet eine 'Teilliste' der Getränke.
- effects = eine Liste, die jedem Vorkoster zuordnet, wie viele vergiftete Getränke er konsumiert hat.
Annahmen: Vorkoster wurden so angewiesen, dass es garantiert ist, vergiftete Getränke zu finden, wenn es die gibt.
Outputs: Liste der Indexes aller vergifteten Getränke.
*/
func evaluateEffects(testers [][]int, effects []int) []int {
var states = map[int]bool{}
var poisened = []int{}
// Werte Effekte aus, um Gift zu lokalisieren:
// Zuerst die Indexes der Getränke bei allen vergifteten Tester zusammenführen:
for i, tester := range testers {
// wenn Tester positiv testet, dann ist eines der von ihm probierten Getränks vergiftet
if effects[i] > 0 {
// markiere alle vom Tester probierten Getränke
for _, k := range tester {
states[k] = true
}
}
}
// jetzt eliminieren wir alle Getränke, die von nicht vergifteten Testern konsumiert wurden:
for i, tester := range testers {
// wenn Tester negativ testet, dann ist KEINES der von ihm probierten Getränks vergiftet
if effects[i] == 0 {
// schließe alle vom Tester probierten Getränke aus
for _, k := range tester {
states[k] = false
}
}
}
// was übrig bleibt sind die vergifteten Getränke, vorausgesetzt genug Vorkoster wurden ausgewählt
for k, state := range states {
if state {
poisened = append(poisened, k)
}
}
return poisened
}

View File

@@ -1,160 +0,0 @@
package poison
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"ads/internal/core/metrics"
"ads/internal/core/utils"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
func preChecks(L []int, _ ...interface{}) error {
s := utils.SumListInt(L)
if !(s > 0) {
return fmt.Errorf("Mindestens ein Getränk muss vergiftet sein!")
} else if !(s <= 1) {
return fmt.Errorf("Höchstens ein Getränk darf vergiftet sein!")
}
return nil
}
func postChecks(L []int, index int, _ ...interface{}) error {
if !(index >= 0) {
return fmt.Errorf("Der Algorithmus hat kein vergiftetes Getränk gefunden, obwohl per Annahme eines existiert.")
} else if !(L[index] > 0) {
return fmt.Errorf("Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.")
}
return nil
}
/* ---------------------------------------------------------------- *
* ALGORITHM find poison + Display
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
func FancyFindPoison(input_L []int) (int, error) {
var name = "Giftsuche (O(n) Vorkoster)"
var inputs = map[string]interface{}{
"L": input_L,
}
var outputs map[string]interface{}
var (
output_index 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()
output_index = FindPoison(input_L)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, output_index)
if err != nil {
break
}
}
}
return output_index, err
}
/* ---------------------------------------------------------------- *
* ALGORITHM find poison fast + Display
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
func FancyFindPoisonFast(input_L []int) (int, error) {
var name = "Giftsuche (O(log(n)) Vorkoster)"
var inputs = map[string]interface{}{
"L": input_L,
}
var outputs map[string]interface{}
var (
output_index 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()
output_index = FindPoisonFast(input_L)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, output_index)
if err != nil {
break
}
}
}
return output_index, err
}

View File

@@ -1,37 +0,0 @@
package sequential
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM sequential search
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen, x = Zahl.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
*/
func SequentialSearch(L []int, x int) int {
n := len(L)
for i := 0; i < n; i++ {
metrics.AddTimeCost()
if L[i] == x {
logging.Debug("Element in Position %[1]v gefunden.", i)
return i
}
logging.Debug("Element nicht in Position %[1]v.", i)
}
return -1
}

View File

@@ -1,104 +0,0 @@
package sequential
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"ads/internal/core/metrics"
"ads/internal/core/utils"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, _ ...interface{}) error {
// Keine Checks!
return nil
}
func postChecks(L []int, x int, index int, _ ...interface{}) error {
if utils.ArrayContains(L, x) {
if !(index >= 0) {
return fmt.Errorf("Der Algorithmus sollte nicht -1 zurückgeben.")
}
if L[index] != x {
return fmt.Errorf("Der Algorithmus hat den falschen Index bestimmt.")
}
} else {
if index != -1 {
return fmt.Errorf("Der Algorithmus sollte -1 zurückgeben.")
}
}
return nil
}
/* ---------------------------------------------------------------- *
* METHOD sequential search + Display
* ---------------------------------------------------------------- */
func FancySequentialSearch(input_L []int, input_x int) (int, error) {
var name = "Sequenziellsuchalgorithmus"
var inputs = map[string]interface{}{
"L": input_L,
"x": input_x,
}
var outputs map[string]interface{}
var (
output_index 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, input_x)
if err != nil {
break
}
}
// Ausführung des Algorithmus:
metrics.ResetMetrics()
metrics.StartMetrics()
output_index = SequentialSearch(input_L, input_x)
metrics.StopMetrics()
outputs = map[string]interface{}{
"index": output_index,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, input_x, output_index)
if err != nil {
break
}
}
}
return output_index, err
}

View File

@@ -1,107 +0,0 @@
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

@@ -1,89 +0,0 @@
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
}

View File

@@ -1,167 +0,0 @@
package maxsubsum
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"ads/internal/core/logging"
"ads/internal/core/metrics"
"ads/internal/core/utils"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* ALGORITHM max sub sum
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen
Outputs:
- maxSum = Wert der maximalen Summe einer Teilliste aufeinanderfolgender Elemente
- u, v = Indexes so dass die Teilliste [L[u], L[u+1], ..., L[v]] die maximale Summe aufweist
*/
func MaxSubSum(L []int) (int, int, int) {
maxSum := 0
u := 0
v := -1
for i := 0; i < len(L); i++ {
// Bestimme maximale Teilsumme der linken Rände der Liste ab Index i {
maxSum_, _, k := lRandSum(L[i:])
if maxSum_ > maxSum {
k += i // NOTE: muss wegen Offset kompensieren
maxSum, u, v = maxSum_, i, k
logging.Debug("max Teilsumme aktualisiert: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum)
}
}
return maxSum, u, v
}
/* ---------------------------------------------------------------- *
* ALGORITHM max sub sum (D & C)
* ---------------------------------------------------------------- */
/*
Inputs: L = Liste von Zahlen
Outputs:
- maxSum = Wert der maximalen Summe einer Teilliste aufeinanderfolgender Elemente
- u, v = Indexes so dass die Teilliste [L[u], L[u+1], ..., L[v]] die maximale Summe aufweist
*/
func MaxSubSumDC(L []int) (int, int, int) {
maxSum := 0
u := 0
v := -1
if len(L) == 1 {
// wenn Liste aus 1 Element besteht, nicht teilen:
if L[0] > maxSum {
v = 0
maxSum = L[0]
}
} else {
u = utils.Ceil(float64(len(L)) / 2)
Ll := L[:u]
Lr := L[u:]
// berechnet maximale Teilsumme der linken Hälfte:
maxSum1, u1, v1 := MaxSubSumDC(Ll)
// berechnet maximale Teilsumme der rechten Hälfte:
maxSum2, u2, v2 := MaxSubSumDC(Lr)
u2, v2 = u2+len(Ll), v2+len(Ll) // offsets kompensieren
// berechnet maximale Teilsumme mit Überschneidung zw. den Hälften:
maxSum3, u3, v3 := lrRandSum(Ll, Lr)
// bestimme Maximum der 3 Möglichkeiten:
maxSum = utils.MaxInt(maxSum1, maxSum2, maxSum3)
if maxSum == maxSum1 {
maxSum, u, v = maxSum1, u1, v1
logging.Debug("max Teilsumme kommt in linker Partition vor: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum)
} else if maxSum == maxSum3 {
maxSum, u, v = maxSum3, u3, v3
logging.Debug("max Teilsumme kommt in Überschneidung vor: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum)
} else { // } else if maxSum == maxSum2 {
maxSum, u, v = maxSum2, u2, v2
logging.Debug("max Teilsumme kommt in rechter Partition vor: Summe L[i] von i=%[1]v .. %[2]v = %[3]v", u, v, maxSum)
}
}
return maxSum, u, v
}
/* ---------------------------------------------------------------- *
* AUXILIARY METHODS
* ---------------------------------------------------------------- */
/*
Bestimmt maximale Teilsumme einer Teiliste einer Liste,
wobei die Liste in zwei Intervalle partitioniert ist
und die Teilliste beide überschneidet.
Inputs: Ll, Lr = eine Partition einer Liste von Zahlen in zwei Intervalle
Outputs: maxSum, u=0, v
*/
func lrRandSum(Ll []int, Lr []int) (int, int, int) {
maxSumL, u, _ := rRandSum(Ll)
maxSumR, _, v := lRandSum(Lr)
maxSum := maxSumL + maxSumR
v += len(Ll) // offsets kompensieren
return maxSum, u, v
}
/*
Bestimmt maximale Teilsumme aller nicht leeren linken Segmente einer Liste.
Inputs: L = Liste von Zahlen
Outputs: maxSum, u(=0), v
*/
func lRandSum(L []int) (int, int, int) {
n := len(L)
// berechne kumulative Summen (vorwärts)
metrics.AddTimeCost(n)
total := L[0]
maxSum := total
u := 0
v := 0
for i := 0; i < n; i++ {
total += L[i]
if total > maxSum {
v = i
maxSum = total
}
}
return maxSum, u, v
}
/*
Bestimmt maximale Teilsumme aller nicht leeren rechten Segmente einer Liste.
Inputs: L = Liste von Zahlen
Outputs: maxSum, u, v(=len(L)-1)
*/
func rRandSum(L []int) (int, int, int) {
n := len(L)
// berechne kumulative Summen (rückwärts)
metrics.AddTimeCost(n)
total := L[n-1]
maxSum := total
u := n - 1
v := n - 1
for i := n - 2; i >= 0; i-- {
total += L[i]
if total > maxSum {
u = i
maxSum = total
}
}
return maxSum, u, v
}

View File

@@ -1,160 +0,0 @@
package maxsubsum
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"ads/internal/core/metrics"
"ads/internal/setup"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* CHECKS
* ---------------------------------------------------------------- */
func preChecks(L []int, _ ...interface{}) error {
if !(len(L) > 0) {
return fmt.Errorf("Liste darf nicht leer sein.")
}
return nil
}
func postChecks(L []int, _ ...interface{}) error {
// TODO
return nil
}
/* ---------------------------------------------------------------- *
* METHOD Algorithm + Display
* ---------------------------------------------------------------- */
func FancyMaxSubSum(input_L []int) (int, int, int, error) {
var name = "MaxSubSum (Maximale Teilsumme)"
var inputs = map[string]interface{}{
"L": input_L,
}
var outputs map[string]interface{}
var (
output_maxSum int
output_indexFrom int
output_indexTo 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()
output_maxSum, output_indexFrom, output_indexTo = MaxSubSum(input_L)
metrics.StopMetrics()
outputs = map[string]interface{}{
"maxSum": output_maxSum,
"index_from": output_indexFrom,
"index_to": output_indexTo,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, output_maxSum, output_indexFrom, output_indexTo)
if err != nil {
break
}
}
}
return output_maxSum, output_indexFrom, output_indexTo, err
}
/* ---------------------------------------------------------------- *
* ALGORITHM max sub sum (D & C)
* ---------------------------------------------------------------- */
func FancyMaxSubSumDC(input_L []int) (int, int, int, error) {
var name = "MaxSubSum (Maximale Teilsumme mit D & C)"
var inputs = map[string]interface{}{
"L": input_L,
}
var outputs map[string]interface{}
var (
output_maxSum int
output_indexFrom int
output_indexTo 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()
output_maxSum, output_indexFrom, output_indexTo = MaxSubSumDC(input_L)
metrics.StopMetrics()
outputs = map[string]interface{}{
"maxSum": output_maxSum,
"index_from": output_indexFrom,
"index_to": output_indexTo,
}
// Metriken anzeigen
if setup.AppConfigShowMetrics() {
setup.DisplayMetrics()
}
// End Message
setup.DisplayEndOfAlgorithm(outputs)
// Postchecks:
if setup.AppConfigPerformChecks() {
err = postChecks(input_L, output_maxSum, output_indexFrom, output_indexTo)
if err != nil {
break
}
}
}
return output_maxSum, output_indexFrom, output_indexTo, err
}

View File

@@ -1,72 +0,0 @@
package stacks
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"fmt"
"strings"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
//
/* ---------------------------------------------------------------- *
* TYPE
* ---------------------------------------------------------------- */
type StackInt struct {
values []int
}
/* ---------------------------------------------------------------- *
* METHODS stacks
* ---------------------------------------------------------------- */
func CREATE() StackInt {
return StackInt{
values: []int{},
}
}
func (S *StackInt) INIT() StackInt {
return StackInt{
values: []int{},
}
}
func (S StackInt) EMPTY() bool {
return len(S.values) == 0
}
func (S *StackInt) PUSH(x int) {
S.values = append(S.values, x)
}
func (S *StackInt) TOP() int {
if S.EMPTY() {
panic("Cannot pop from empty stack!")
}
return S.values[len(S.values)-1]
}
func (S *StackInt) POP() int {
x := S.TOP()
S.values = S.values[:(len(S.values) - 1)]
return x
}
func (S StackInt) String() string {
if len(S.values) == 0 {
return "-"
}
values := []string{}
for _, value := range S.values {
values = append(values, fmt.Sprintf("%v", value))
}
return fmt.Sprintf(strings.Join(values, ", "))
}

View File

@@ -11,14 +11,14 @@ import (
"ads/internal/setup"
"ads/internal/types"
algorithm_search_binary "ads/internal/algorithms/search/binary"
algorithm_search_interpol "ads/internal/algorithms/search/interpol"
algorithm_search_ith_element "ads/internal/algorithms/search/ith_element"
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"
algorithm_search_binary "ads/pkg/algorithms/search/binary"
algorithm_search_interpol "ads/pkg/algorithms/search/interpol"
algorithm_search_ith_element "ads/pkg/algorithms/search/ith_element"
algorithm_search_jump "ads/pkg/algorithms/search/jump"
algorithm_search_poison "ads/pkg/algorithms/search/poison"
algorithm_search_sequential "ads/pkg/algorithms/search/sequential"
algorithm_stacks_next_greater_element "ads/pkg/algorithms/stacks/next_greater_element"
algorithm_sum_maxsubsum "ads/pkg/algorithms/sum/maxsubsum"
)
/* ---------------------------------------------------------------- *

View File

@@ -8,14 +8,14 @@ import (
"ads/internal/core/logging"
"ads/internal/setup"
algorithm_search_binary "ads/internal/algorithms/search/binary"
algorithm_search_interpol "ads/internal/algorithms/search/interpol"
algorithm_search_ith_element "ads/internal/algorithms/search/ith_element"
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"
algorithm_search_binary "ads/pkg/algorithms/search/binary"
algorithm_search_interpol "ads/pkg/algorithms/search/interpol"
algorithm_search_ith_element "ads/pkg/algorithms/search/ith_element"
algorithm_search_jump "ads/pkg/algorithms/search/jump"
algorithm_search_poison "ads/pkg/algorithms/search/poison"
algorithm_search_sequential "ads/pkg/algorithms/search/sequential"
algorithm_stacks_next_greater_element "ads/pkg/algorithms/stacks/next_greater_element"
algorithm_sum_maxsubsum "ads/pkg/algorithms/sum/maxsubsum"
)
/* ---------------------------------------------------------------- *