master > master: code - nextGreaterEl alg für python, vereinfachte Algorithmus für py + go
This commit is contained in:
@@ -6,4 +6,5 @@
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.algorithms.search.exports import *;
|
||||
from src.algorithms.stacks.exports import *;
|
||||
from src.algorithms.sum.exports import *;
|
||||
|
||||
0
code/python/src/algorithms/stacks/__init__.py
Normal file
0
code/python/src/algorithms/stacks/__init__.py
Normal file
8
code/python/src/algorithms/stacks/exports.py
Normal file
8
code/python/src/algorithms/stacks/exports.py
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.algorithms.stacks.next_greater_element import NextGreaterElement;
|
||||
107
code/python/src/algorithms/stacks/next_greater_element.py
Normal file
107
code/python/src/algorithms/stacks/next_greater_element.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user