Compare commits
5 Commits
9a56c63018
...
580cc97387
Author | SHA1 | Date |
---|---|---|
RD | 580cc97387 | |
RD | ef833533f6 | |
RD | ab38c181c9 | |
RD | 972027ce41 | |
RD | 2d91e98904 |
|
@ -18,9 +18,10 @@
|
|||
!/assets/LOGO
|
||||
!/assets/config.yml
|
||||
|
||||
!/internal
|
||||
!/pkg
|
||||
!/**/*.go
|
||||
!/internal
|
||||
!/*/**/
|
||||
!/*/**/*.go
|
||||
!/go.mod
|
||||
!/go.sum
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
X.Y.Z
|
||||
0.0.0
|
||||
|
|
|
@ -5,6 +5,7 @@ package logging
|
|||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
@ -24,7 +25,7 @@ func Debug(line interface{}, args ...interface{}) {
|
|||
if !debugmode {
|
||||
return
|
||||
}
|
||||
logGeneric(os.Stdout, "[\033[96;1mDEBUG\033[0m]", line, args...)
|
||||
logGeneric(os.Stdout, "\033[2m[\033[96;1mDEBUG\033[0m\033[2m]\033[0m", fmt.Sprintf("\033[2m%v\033[0m", line), args...)
|
||||
}
|
||||
|
||||
func Warn(line interface{}, args ...interface{}) {
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
* ---------------------------------------------------------------- */
|
||||
|
||||
var _ctr_time = types.NewCounter()
|
||||
var _ctr_moves = types.NewCounter()
|
||||
var _ctr_space = types.NewCounter()
|
||||
var _tmr = types.NewTimer()
|
||||
|
||||
|
@ -26,6 +27,7 @@ var _tmr = types.NewTimer()
|
|||
|
||||
func ResetMetrics() {
|
||||
_ctr_time.Reset()
|
||||
_ctr_moves.Reset()
|
||||
_ctr_space.Reset()
|
||||
_tmr.Reset()
|
||||
}
|
||||
|
@ -42,14 +44,26 @@ func AddTimeCost(options ...int) {
|
|||
_ctr_time.Add(options...)
|
||||
}
|
||||
|
||||
func AddMovesCost(options ...int) {
|
||||
_ctr_moves.Add(options...)
|
||||
}
|
||||
|
||||
func AddSpaceCost(options ...int) {
|
||||
_ctr_space.Add(options...)
|
||||
}
|
||||
|
||||
func SetSpaceCost(n int) {
|
||||
_ctr_space.Set(n)
|
||||
}
|
||||
|
||||
func GetTimeCost() int {
|
||||
return _ctr_time.Size()
|
||||
}
|
||||
|
||||
func GetMovesCost() int {
|
||||
return _ctr_moves.Size()
|
||||
}
|
||||
|
||||
func GetSpaceCost() int {
|
||||
return _ctr_space.Size()
|
||||
}
|
||||
|
|
|
@ -104,6 +104,7 @@ func DisplayMetrics() {
|
|||
// logging.Plain("Dauer der Ausführung: t = \033[1m%[1]v\033[0m", metrics.GetTimeElapsed())
|
||||
logging.Plain("Dauer der Ausführung: t = \033[1m%[1]v\033[0m", metrics.GetTimeElapsedLongFormat())
|
||||
logging.Plain("Kosten (Zeit): T(n) = \033[1m%[1]v\033[0m", displayCost(metrics.GetTimeCost()))
|
||||
logging.Plain("Kosten (#Züge): M(n) = \033[1m%[1]v\033[0m", displayCost(metrics.GetMovesCost()))
|
||||
logging.Plain("Kosten (Platz): S(n) = \033[1m%[1]v\033[0m", displayCost(metrics.GetSpaceCost()))
|
||||
return
|
||||
}
|
||||
|
|
|
@ -39,6 +39,14 @@ func (self Counter) Size() int {
|
|||
return self.nr
|
||||
}
|
||||
|
||||
func (self *Counter) Reset() {
|
||||
self.nr = 0
|
||||
}
|
||||
|
||||
func (self *Counter) Set(n int) {
|
||||
self.nr = n
|
||||
}
|
||||
|
||||
func (self *Counter) Add(options ...int) {
|
||||
n := 1
|
||||
if len(options) > 0 {
|
||||
|
@ -46,7 +54,3 @@ func (self *Counter) Add(options ...int) {
|
|||
}
|
||||
self.nr += n
|
||||
}
|
||||
|
||||
func (self *Counter) Reset() {
|
||||
self.nr = 0
|
||||
}
|
||||
|
|
|
@ -34,52 +34,45 @@ func NextGreaterElement(L []int) [][2]int {
|
|||
S := stacks.CREATE()
|
||||
|
||||
for i := 0; i < len(L); i++ {
|
||||
logging.Debug("Lies Element L[%v] ein", i)
|
||||
logging.Debug("Nächstes List-Element L[%v] = %v betrachten", i, L[i])
|
||||
nextElement := L[i]
|
||||
|
||||
logging.Debug("Entferne alle top Elemente vom Stack bis >= nextElement")
|
||||
/*
|
||||
Entferne alle top Elemente vom Stack < nextElement
|
||||
bis oben ein Elment >= nextElement ist, oder Stack leer ist.
|
||||
*/
|
||||
logging.Debug("Stack S | %v", S)
|
||||
if !S.EMPTY() {
|
||||
logging.Debug("Entferne alle top Elemente vom Stack bis >= nextElement")
|
||||
for !S.EMPTY() {
|
||||
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)
|
||||
metrics.AddMovesCost()
|
||||
logging.Debug("Stack S | %v; (popped) Top-Element = %v", S, element)
|
||||
// falls element < next Element, zum Output hinzufügen
|
||||
if element < nextElement {
|
||||
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")
|
||||
// sonst Element auf Stack zurücklegen und Schleife abbrechen.
|
||||
} else {
|
||||
logging.Debug("Stack-Element >= nextElement ==> zurücklegen")
|
||||
S.PUSH(element)
|
||||
metrics.AddTimeCost()
|
||||
metrics.AddMovesCost()
|
||||
break
|
||||
}
|
||||
logging.Debug("Stack S | %v", S)
|
||||
}
|
||||
|
||||
S.PUSH(nextElement)
|
||||
metrics.AddTimeCost()
|
||||
logging.Debug("L[%v] auf Stack legen", i)
|
||||
S.PUSH(nextElement)
|
||||
logging.Debug("Stack S | %v", S)
|
||||
metrics.AddMovesCost()
|
||||
}
|
||||
|
||||
// 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()
|
||||
metrics.AddMovesCost()
|
||||
addToOutput(element, -1)
|
||||
}
|
||||
logging.Debug("Stack S | %v", S)
|
||||
|
|
|
@ -1 +1 @@
|
|||
X.Y.Z
|
||||
0.0.0
|
||||
|
|
|
@ -6,4 +6,5 @@
|
|||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.algorithms.search.exports import *;
|
||||
from src.algorithms.stacks.exports import *;
|
||||
from src.algorithms.sum.exports import *;
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.algorithms.stacks.next_greater_element import NextGreaterElement;
|
|
@ -0,0 +1,107 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.local.typing import *;
|
||||
|
||||
from src.core.log import *;
|
||||
from src.core.metrics import *;
|
||||
from src.data_structures.stacks import Stack;
|
||||
from src.algorithms.methods import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
_output_list: List[Tuple[int,int]]
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CHECKS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def preChecks(L: List[int], **_):
|
||||
# TODO
|
||||
return;
|
||||
|
||||
def postChecks(L: List[int], **_):
|
||||
# TODO
|
||||
return;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# ALGORITHM next greater element
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@algorithmInfos(name='NextGreaterElement (with stacks)', outputnames=['pairs'], preChecks=preChecks, postChecks=postChecks)
|
||||
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();
|
||||
|
||||
S = Stack();
|
||||
S.INIT();
|
||||
|
||||
for i in range(len(L)):
|
||||
logDebug('Nächstes List-Element L[{i}] = {el} betrachten'.format(i=i, el=L[i]));
|
||||
nextElement = L[i];
|
||||
|
||||
logDebug('Entferne alle top Elemente vom Stack bis >= nextElement');
|
||||
# Entferne alle top Elemente vom Stack < nextElement
|
||||
# bis oben ein Elment >= nextElement ist, oder Stack leer ist.
|
||||
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:
|
||||
addToOutput(element, nextElement);
|
||||
# sonst Element auf Stack zurücklegen und Schleife abbrechen.
|
||||
else:
|
||||
logDebug('Stack element >= nextElement ==> zurücklegen')
|
||||
S.PUSH(element);
|
||||
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();
|
||||
|
||||
# was übrig bleibt hat kein größeres Element
|
||||
logDebug('Alles übrige auf Stack hat kein nächstes größeres Element')
|
||||
while not S.EMPTY():
|
||||
logDebug('Stack S | {S}'.format(S=S));
|
||||
element = S.TOP();
|
||||
S.POP();
|
||||
AddMovesCost();
|
||||
addToOutput(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
|
|
@ -22,24 +22,20 @@ _tmr = None;
|
|||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Counter(object):
|
||||
_nr_steps: int;
|
||||
value: int;
|
||||
|
||||
def __init__(self):
|
||||
self.reset();
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self._nr_steps);
|
||||
|
||||
@property
|
||||
def numberOfStep(self) -> int:
|
||||
return self._nr_steps;
|
||||
return str(self.value);
|
||||
|
||||
def add(self, n: int = 1):
|
||||
self._nr_steps += n;
|
||||
self.value += n;
|
||||
return self;
|
||||
|
||||
def reset(self):
|
||||
self._nr_steps = 0;
|
||||
self.value = 0;
|
||||
return self;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -86,6 +82,7 @@ class Timer(object):
|
|||
|
||||
## Initialisierung:
|
||||
_ctr_time = Counter();
|
||||
_ctr_moves = Counter();
|
||||
_ctr_space = Counter();
|
||||
_tmr = Timer();
|
||||
|
||||
|
@ -95,10 +92,12 @@ _tmr = Timer();
|
|||
|
||||
def ResetMetrics():
|
||||
global _ctr_time;
|
||||
global _ctr_moves;
|
||||
global _ctr_space;
|
||||
global _tmr;
|
||||
|
||||
_ctr_time.reset();
|
||||
_ctr_moves.reset();
|
||||
_ctr_space.reset();
|
||||
_tmr.reset();
|
||||
return;
|
||||
|
@ -116,16 +115,29 @@ def AddTimeCost(n: int = 1):
|
|||
_ctr_time.add(n);
|
||||
return;
|
||||
|
||||
def AddMovesCost(n: int = 1):
|
||||
global _ctr_moves;
|
||||
_ctr_moves.add(n);
|
||||
return;
|
||||
|
||||
def AddSpaceCost(n: int = 1):
|
||||
global _ctr_space;
|
||||
_ctr_space.add(n);
|
||||
return;
|
||||
|
||||
def SetSpaceCost(n: int):
|
||||
global _ctr_space;
|
||||
_ctr_space.value = n;
|
||||
return;
|
||||
|
||||
def GetTimeCost() -> int:
|
||||
return _ctr_time.numberOfStep;
|
||||
return _ctr_time.value;
|
||||
|
||||
def GetMovesCost() -> int:
|
||||
return _ctr_moves.value;
|
||||
|
||||
def GetSpaceCost() -> int:
|
||||
return _ctr_space.numberOfStep;
|
||||
return _ctr_space.value;
|
||||
|
||||
def GetTimeElapsed() -> timedelta:
|
||||
global _tmr;
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from local.typing import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CLASS Stack
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Stack(object):
|
||||
values: List[int];
|
||||
|
||||
def INIT(self):
|
||||
self.values = [];
|
||||
|
||||
def EMPTY(self) -> bool:
|
||||
return len(self.values) == 0;
|
||||
|
||||
def PUSH(self, x: int):
|
||||
self.values.append(x);
|
||||
|
||||
def TOP(self) -> int:
|
||||
if self.EMPTY():
|
||||
raise Exception('Cannot pop from empty stack!')
|
||||
return self.values[-1];
|
||||
|
||||
def POP(self) -> int:
|
||||
x = self.TOP()
|
||||
self.values = self.values[:-1]
|
||||
return x
|
||||
|
||||
def __str__(self) -> str:
|
||||
if len(self.values) == 0:
|
||||
return '-';
|
||||
return ', '.join([str(x) for x in self.values]);
|
|
@ -39,11 +39,7 @@ def runNonInteractive(path: str):
|
|||
DisplayStartOfCase(caseindex, descr);
|
||||
|
||||
try:
|
||||
if command == 'algorithm-sum-maxsub':
|
||||
MaxSubSum(L=inputs['L']);
|
||||
elif command == 'algorithm-sum-maxsub-dc':
|
||||
MaxSubSumDC(L=inputs['L']);
|
||||
elif command == 'algorithm-search-sequential':
|
||||
if command == 'algorithm-search-sequential':
|
||||
SequentialSearch(L=inputs['L'], x=inputs['x']);
|
||||
elif command == 'algorithm-search-binary':
|
||||
BinarySearch(L=inputs['L'], x=inputs['x']);
|
||||
|
@ -61,6 +57,12 @@ def runNonInteractive(path: str):
|
|||
FindPoison(L=inputs['L']);
|
||||
elif command == 'algorithm-search-poison-fast':
|
||||
FindPoisonFast(L=inputs['L']);
|
||||
elif command == 'algorithm-stacks-next-greater-element':
|
||||
NextGreaterElement(L=inputs['L']);
|
||||
elif command == 'algorithm-sum-maxsub':
|
||||
MaxSubSum(L=inputs['L']);
|
||||
elif command == 'algorithm-sum-maxsub-dc':
|
||||
MaxSubSumDC(L=inputs['L']);
|
||||
else:
|
||||
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
|
||||
except Exception as e:
|
||||
|
|
|
@ -69,8 +69,9 @@ def DisplayEndOfAlgorithm(*_: Any, **outputs: Any):
|
|||
|
||||
def DisplayMetrics():
|
||||
logPlain('Dauer der Ausführung: t = \033[1m{}\033[0m'.format(GetTimeElapsed()));
|
||||
logPlain('Kosten (Zeit): T(n) = \033[1m{}\033[0m'.format(GetTimeCost()));
|
||||
logPlain('Kosten (Platz): S(n) = \033[1m{}\033[0m'.format(GetSpaceCost()));
|
||||
logPlain('Kosten (Zeit): T(n) = \033[1m{}\033[0m'.format(displayCost(GetTimeCost())));
|
||||
logPlain('Kosten (#Züge): M(n) = \033[1m{}\033[0m'.format(displayCost(GetMovesCost())));
|
||||
logPlain('Kosten (Platz): S(n) = \033[1m{}\033[0m'.format(displayCost(GetSpaceCost())));
|
||||
return;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -80,3 +81,6 @@ def DisplayMetrics():
|
|||
def DisplayBar(n: int = 80):
|
||||
logPlain('+{}+'.format('-'*n));
|
||||
return;
|
||||
|
||||
def displayCost(cost: int) -> str:
|
||||
return str(cost) if cost > 0 else '-';
|
||||
|
|
Loading…
Reference in New Issue