master > master: src py - minor änderungen
This commit is contained in:
parent
15c2c37213
commit
2c23f4c728
@ -8,6 +8,7 @@
|
|||||||
import functools;
|
import functools;
|
||||||
|
|
||||||
from src.core.log import *;
|
from src.core.log import *;
|
||||||
|
from src.core.metrics import *;
|
||||||
from src.setup.display import *;
|
from src.setup.display import *;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -39,12 +40,12 @@ def algorithmInfos(
|
|||||||
name: str,
|
name: str,
|
||||||
checks: bool = True,
|
checks: bool = True,
|
||||||
metrics: bool = None,
|
metrics: bool = None,
|
||||||
outputnames: Union[str, Tuple[str]] = 'result',
|
outputnames: List[str] = ['result'],
|
||||||
preChecks: Any = None,
|
preChecks: Any = None,
|
||||||
postChecks: Any = None
|
postChecks: Any = None
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Decorator für Algorthmen, der Folgendes macht:
|
Decorator für Algorithmen, der Folgendes macht:
|
||||||
|
|
||||||
- Zeigt vorm Start Console-Messages mit Namen des Algorithmus + Inputs.
|
- Zeigt vorm Start Console-Messages mit Namen des Algorithmus + Inputs.
|
||||||
- Zeit am Ende optional Metriken (bspw. Zeitkosten).
|
- Zeit am Ende optional Metriken (bspw. Zeitkosten).
|
||||||
@ -65,7 +66,7 @@ def algorithmInfos(
|
|||||||
if state:
|
if state:
|
||||||
# Initialisierung
|
# Initialisierung
|
||||||
DisplayStartOfAlgorithm(name, **inputs);
|
DisplayStartOfAlgorithm(name, **inputs);
|
||||||
RestartCounter();
|
RestartMetrics();
|
||||||
# Prechecks
|
# Prechecks
|
||||||
if checks and callable(preChecks):
|
if checks and callable(preChecks):
|
||||||
preChecks(**inputs);
|
preChecks(**inputs);
|
||||||
@ -78,8 +79,7 @@ def algorithmInfos(
|
|||||||
if state:
|
if state:
|
||||||
# benenne Outputs:
|
# benenne Outputs:
|
||||||
outputs_ = outputs if isinstance(outputs, tuple) else tuple([outputs]);
|
outputs_ = outputs if isinstance(outputs, tuple) else tuple([outputs]);
|
||||||
outputnames_ = outputnames if isinstance(outputnames, tuple) else tuple([outputnames]);
|
outputsNamed = { outputnames[k]: value for k, value in enumerate(outputs_) };
|
||||||
outputsNamed = { outputnames_[k]: value for k, value in enumerate(outputs_) };
|
|
||||||
# Letzte Messages
|
# Letzte Messages
|
||||||
if metrics:
|
if metrics:
|
||||||
DisplayMetrics();
|
DisplayMetrics();
|
||||||
|
@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
|||||||
# ALGORITHM binary search
|
# ALGORITHM binary search
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Binärsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Binärsuchalgorithmus', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def BinarySearch(L: List[int], x: int) -> int:
|
def BinarySearch(L: List[int], x: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||||
@ -49,7 +49,7 @@ def BinarySearch(L: List[int], x: int) -> int:
|
|||||||
if len(L) == 0:
|
if len(L) == 0:
|
||||||
logDebug('x nicht in L');
|
logDebug('x nicht in L');
|
||||||
return -1;
|
return -1;
|
||||||
AddToCounter();
|
AddTimeCost();
|
||||||
m = math.floor(len(L)/2);
|
m = math.floor(len(L)/2);
|
||||||
if L[m] == x:
|
if L[m] == x:
|
||||||
logDebug('x in Position m gefunden');
|
logDebug('x in Position m gefunden');
|
||||||
|
@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
|||||||
# ALGORITHM interpolation
|
# ALGORITHM interpolation
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Interpolationssuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Interpolationssuchalgorithmus', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def InterpolationSearch(L: List[int], x: int, u: int, v: int) -> int:
|
def InterpolationSearch(L: List[int], x: int, u: int, v: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||||||
@ -66,7 +66,7 @@ def getSuchposition(L: List[int], x: int, u: int, v: int) -> int:
|
|||||||
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||||||
Outputs: Interpolierte Position, um Suchinterval ausgeglichen aufzuteilen.
|
Outputs: Interpolierte Position, um Suchinterval ausgeglichen aufzuteilen.
|
||||||
'''
|
'''
|
||||||
AddToCounter();
|
AddTimeCost();
|
||||||
r = (x - L[u])/(L[v]-L[u]);
|
r = (x - L[u])/(L[v]-L[u]);
|
||||||
p = math.floor(u + r*(v-u))
|
p = math.floor(u + r*(v-u))
|
||||||
return p;
|
return p;
|
||||||
|
@ -33,10 +33,10 @@ def postChecks(L: List[int], i: int, value: int, **_):
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM jump search
|
# ALGORITHM find ith smallest element
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element)', outputnames=['value'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def FindIthSmallest(L: List[int], i: int) -> int:
|
def FindIthSmallest(L: List[int], i: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||||
@ -51,7 +51,7 @@ def FindIthSmallest(L: List[int], i: int) -> int:
|
|||||||
'''
|
'''
|
||||||
index = 0;
|
index = 0;
|
||||||
minValue = L[0];
|
minValue = L[0];
|
||||||
AddToCounter(len(L));
|
AddTimeCost(len(L));
|
||||||
for i_ in range(1, len(L)):
|
for i_ in range(1, len(L)):
|
||||||
if L[i_] < minValue:
|
if L[i_] < minValue:
|
||||||
index = i_;
|
index = i_;
|
||||||
@ -65,10 +65,10 @@ def FindIthSmallest(L: List[int], i: int) -> int:
|
|||||||
return FindIthSmallest(L=L[:index] + L[(index+1):], i=i);
|
return FindIthSmallest(L=L[:index] + L[(index+1):], i=i);
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM jump search (D & C)
|
# ALGORITHM find ith smallest element (D & C)
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element, D & C)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element, D & C)', outputnames=['value'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def FindIthSmallestDC(L: List[int], i: int) -> int:
|
def FindIthSmallestDC(L: List[int], i: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||||
@ -81,7 +81,7 @@ def FindIthSmallestDC(L: List[int], i: int) -> int:
|
|||||||
Outputs: Wert des i. kleinste Element in L.
|
Outputs: Wert des i. kleinste Element in L.
|
||||||
Beachte 1.kleinstes <==> Minimum.
|
Beachte 1.kleinstes <==> Minimum.
|
||||||
'''
|
'''
|
||||||
AddToCounter();
|
AddTimeCost();
|
||||||
p = L[len(L)-1]; # NOTE: Pivotelement kann beliebig gewählt werden
|
p = L[len(L)-1]; # NOTE: Pivotelement kann beliebig gewählt werden
|
||||||
Ll = [ x for x in L if x < p ];
|
Ll = [ x for x in L if x < p ];
|
||||||
Lr = [ x for x in L if x > p ];
|
Lr = [ x for x in L if x > p ];
|
||||||
|
@ -39,7 +39,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
|||||||
# ALGORITHM jump search
|
# ALGORITHM jump search
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Sprungsuche', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Sprungsuche', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
|
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
|
||||||
@ -52,7 +52,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
|||||||
'''
|
'''
|
||||||
i = 0;
|
i = 0;
|
||||||
while i*m < len(L):
|
while i*m < len(L):
|
||||||
AddToCounter();
|
AddTimeCost();
|
||||||
offset = i*m;
|
offset = i*m;
|
||||||
block = L[offset:][:m];
|
block = L[offset:][:m];
|
||||||
elementAfterBlock = block[-1] + 1;
|
elementAfterBlock = block[-1] + 1;
|
||||||
@ -70,7 +70,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
|||||||
# ALGORITHM jump search - exponentiell
|
# ALGORITHM jump search - exponentiell
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Sprungsuche (exponentiell)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Sprungsuche (exponentiell)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def JumpSearchExponentiell(L: List[int], x: int) -> int:
|
def JumpSearchExponentiell(L: List[int], x: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||||
@ -84,7 +84,7 @@ def JumpSearchExponentiell(L: List[int], x: int) -> int:
|
|||||||
i0 = 0;
|
i0 = 0;
|
||||||
i1 = 1;
|
i1 = 1;
|
||||||
while i0 < len(L):
|
while i0 < len(L):
|
||||||
AddToCounter();
|
AddTimeCost();
|
||||||
block = L[i0:i1];
|
block = L[i0:i1];
|
||||||
elementAfterBlock = block[-1] + 1;
|
elementAfterBlock = block[-1] + 1;
|
||||||
if x < elementAfterBlock:
|
if x < elementAfterBlock:
|
||||||
|
@ -21,23 +21,25 @@ from src.algorithms.methods import *;
|
|||||||
# CHECKS
|
# CHECKS
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def preChecks(L: List[bool], **_):
|
def preChecks(L: List[int], **_):
|
||||||
assert sum(L) > 0, 'Mindestens ein Getränk muss vergiftet sein!';
|
s = sum(L);
|
||||||
assert sum(L) == 1, 'Höchstens ein Getränk darf vergiftet sein!';
|
assert s > 0, 'Mindestens ein Getränk muss vergiftet sein!';
|
||||||
|
assert s <= 1, 'Höchstens ein Getränk darf vergiftet sein!';
|
||||||
return;
|
return;
|
||||||
|
|
||||||
def postChecks(L: List[bool], index: int, **_):
|
def postChecks(L: List[int], index: int, **_):
|
||||||
assert L[index] == True, 'Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.';
|
assert index >= 0, 'Der Algorithmus hat kein vergiftetes Getränk gefunden, obwohl per Annahme eines existiert.';
|
||||||
|
assert L[index] > 0, 'Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.';
|
||||||
return;
|
return;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM find poison
|
# ALGORITHM find poison
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Giftsuche (O(n) Vorkoster)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Giftsuche (O(n) Vorkoster)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def FindPoison(L: List[bool]) -> int:
|
def FindPoison(L: List[int]) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
Inputs: L = Liste von Getränken: durch 0-1 Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
||||||
Annahme: Genau ein Getränk sei vergiftet.
|
Annahme: Genau ein Getränk sei vergiftet.
|
||||||
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
|
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
|
||||||
|
|
||||||
@ -47,7 +49,7 @@ def FindPoison(L: List[bool]) -> int:
|
|||||||
n = len(L);
|
n = len(L);
|
||||||
testers = [];
|
testers = [];
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
AddToCounter();
|
AddSpaceCost();
|
||||||
logDebug('Füge Vorkoster hinzu, der nur Getränk {i} testet.'.format(i=i))
|
logDebug('Füge Vorkoster hinzu, der nur Getränk {i} testet.'.format(i=i))
|
||||||
testers.append([i]);
|
testers.append([i]);
|
||||||
logDebug('Warte auf Effekte');
|
logDebug('Warte auf Effekte');
|
||||||
@ -62,10 +64,10 @@ def FindPoison(L: List[bool]) -> int:
|
|||||||
# ALGORITHM find poison fast
|
# ALGORITHM find poison fast
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Giftsuche (O(log(n)) Vorkoster)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Giftsuche (O(log(n)) Vorkoster)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def FindPoisonFast(L: List[bool]) -> List[int]:
|
def FindPoisonFast(L: List[int]) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
Inputs: L = Liste von Getränken: durch 0-1 Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
||||||
Annahme: Genau ein Getränk sei vergiftet.
|
Annahme: Genau ein Getränk sei vergiftet.
|
||||||
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
|
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ def FindPoisonFast(L: List[bool]) -> List[int]:
|
|||||||
tester1 = [ k for k in range(n) if not (k in tester0) ];
|
tester1 = [ k for k in range(n) if not (k in tester0) ];
|
||||||
# NOTE: tester1 ist virtuell: aus den Effekten auf tester0 und den Annahmen lassen sich die Effekte auf tester0 erschließen.
|
# 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.
|
# Darum zählen wir nicht 2 sondern 1 Vorkoster.
|
||||||
AddToCounter(1);
|
AddSpaceCost(1);
|
||||||
logDebug('Füge Vorkoster hinzu, der alle Getränke k testet mit {i}. Bit = 0.'.format(i=i))
|
logDebug('Füge Vorkoster hinzu, der alle Getränke k testet mit {i}. Bit = 0.'.format(i=i))
|
||||||
testers.append(tester0);
|
testers.append(tester0);
|
||||||
testers.append(tester1);
|
testers.append(tester1);
|
||||||
@ -97,10 +99,10 @@ def FindPoisonFast(L: List[bool]) -> List[int]:
|
|||||||
# AUXILIARY METHOD wait for effects, evaluate effects
|
# AUXILIARY METHOD wait for effects, evaluate effects
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def waitForEffects(L: List[bool], testers: List[List[int]]) -> List[int]:
|
def waitForEffects(L: List[int], testers: List[List[int]]) -> List[int]:
|
||||||
'''
|
'''
|
||||||
Inputs:
|
Inputs:
|
||||||
- L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
- L = Liste von Getränken: durch 0-1 Werte wird dargestellt, ob ein Getränk vergiftet ist.
|
||||||
- testers = Liste von Vorkostern. Jeder Vorkoster kostet eine 'Teilliste' der Getränke.
|
- 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.
|
Outputs: effects = eine Liste, die jedem Vorkoster zuordnet, wie viele vergiftete Getränke er konsumiert hat.
|
||||||
|
@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
|||||||
# ALGORITHM sequential search
|
# ALGORITHM sequential search
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Sequenziellsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='Sequenziellsuchalgorithmus', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def SequentialSearch(L: List[int], x: int) -> int:
|
def SequentialSearch(L: List[int], x: int) -> int:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||||
@ -45,7 +45,7 @@ def SequentialSearch(L: List[int], x: int) -> int:
|
|||||||
'''
|
'''
|
||||||
n = len(L);
|
n = len(L);
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
AddToCounter();
|
AddTimeCost();
|
||||||
if L[i] == x:
|
if L[i] == x:
|
||||||
logDebug('Element in Position {} gefunden.'.format(i));
|
logDebug('Element in Position {} gefunden.'.format(i));
|
||||||
return i;
|
return i;
|
||||||
|
@ -33,7 +33,7 @@ def postChecks(L: List[int], **_):
|
|||||||
# ALGORITHM max sub sum
|
# ALGORITHM max sub sum
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme)', outputnames=('maxSum', 'index_from', 'index_to'), checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme)', outputnames=['maxSum', 'index_from', 'index_to'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def MaxSubSum(L: List[float]) -> Tuple[float, int, int]:
|
def MaxSubSum(L: List[float]) -> Tuple[float, int, int]:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen
|
Inputs: L = Liste von Zahlen
|
||||||
@ -58,7 +58,7 @@ def MaxSubSum(L: List[float]) -> Tuple[float, int, int]:
|
|||||||
# ALGORITHM max sub sum (D & C)
|
# ALGORITHM max sub sum (D & C)
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme mit D & C)', outputnames=('maxSum', 'index_from', 'index_to'), checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme mit D & C)', outputnames=['maxSum', 'index_from', 'index_to'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
def MaxSubSumDC(L: List[float]) -> Tuple[float, int, int]:
|
def MaxSubSumDC(L: List[float]) -> Tuple[float, int, int]:
|
||||||
'''
|
'''
|
||||||
Inputs: L = Liste von Zahlen
|
Inputs: L = Liste von Zahlen
|
||||||
@ -127,7 +127,7 @@ def lRandSum(L: List[float]) -> Tuple[float, int, int]:
|
|||||||
'''
|
'''
|
||||||
n = len(L);
|
n = len(L);
|
||||||
## berechne kumulative Summen (vorwärts)
|
## berechne kumulative Summen (vorwärts)
|
||||||
AddToCounter(n);
|
AddTimeCost(n);
|
||||||
total = L[0];
|
total = L[0];
|
||||||
maxSum = total;
|
maxSum = total;
|
||||||
u = 0;
|
u = 0;
|
||||||
@ -148,7 +148,7 @@ def rRandSum(L: List[float]) -> Tuple[float, int, int]:
|
|||||||
'''
|
'''
|
||||||
n = len(L);
|
n = len(L);
|
||||||
## berechne kumulative Summen (rückwärts)
|
## berechne kumulative Summen (rückwärts)
|
||||||
AddToCounter(n);
|
AddTimeCost(n);
|
||||||
total = L[n-1];
|
total = L[n-1];
|
||||||
maxSum = total;
|
maxSum = total;
|
||||||
u = n-1;
|
u = n-1;
|
||||||
|
@ -10,10 +10,6 @@ from src.local.misc import *;
|
|||||||
from src.local.system import *;
|
from src.local.system import *;
|
||||||
from src.local.typing import *;
|
from src.local.typing import *;
|
||||||
|
|
||||||
from datetime import timedelta;
|
|
||||||
|
|
||||||
from src.core.metrics import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# GLOBAL VARIABLES
|
# GLOBAL VARIABLES
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -21,7 +17,6 @@ from src.core.metrics import *;
|
|||||||
_logging_prefix: str = '';
|
_logging_prefix: str = '';
|
||||||
_quietmode: bool = False;
|
_quietmode: bool = False;
|
||||||
_debugmode: bool = False;
|
_debugmode: bool = False;
|
||||||
_ctr: Counter = Counter();
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# METHOD get/set quiet mode, logging depth, timer
|
# METHOD get/set quiet mode, logging depth, timer
|
||||||
@ -43,24 +38,6 @@ def SetDebugMode(mode: bool):
|
|||||||
_debugmode = mode;
|
_debugmode = mode;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
def RestartCounter():
|
|
||||||
global _ctr;
|
|
||||||
_ctr.reset();
|
|
||||||
return;
|
|
||||||
|
|
||||||
def AddToCounter(n: int = 1):
|
|
||||||
global _ctr;
|
|
||||||
_ctr.add(n);
|
|
||||||
return;
|
|
||||||
|
|
||||||
def NumberOfSteps() -> int:
|
|
||||||
return _ctr.numberOfStep;
|
|
||||||
|
|
||||||
def TimeElapsed() -> timedelta:
|
|
||||||
global _ctr;
|
|
||||||
_ctr.stop();
|
|
||||||
return _ctr.elapsedTime;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# Logging
|
# Logging
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -5,17 +5,24 @@
|
|||||||
# IMPORTS
|
# IMPORTS
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
from __future__ import annotations;
|
||||||
from datetime import datetime;
|
from datetime import datetime;
|
||||||
from datetime import timedelta;
|
from datetime import timedelta;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# GLOBAL VARIABLES
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
_ctr_time = None;
|
||||||
|
_ctr_space = None;
|
||||||
|
_tmr = None;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# CLASS counter
|
# CLASS counter
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
class Counter(object):
|
class Counter(object):
|
||||||
_nr_steps: int;
|
_nr_steps: int;
|
||||||
_timeelapsed: timedelta;
|
|
||||||
_timecurrent: datetime;
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.reset();
|
self.reset();
|
||||||
@ -27,27 +34,87 @@ class Counter(object):
|
|||||||
def numberOfStep(self) -> int:
|
def numberOfStep(self) -> int:
|
||||||
return self._nr_steps;
|
return self._nr_steps;
|
||||||
|
|
||||||
@property
|
|
||||||
def elapsedTime(self) -> timedelta:
|
|
||||||
return self._timeelapsed;
|
|
||||||
def start(self):
|
|
||||||
self._timecurrent = datetime.now();
|
|
||||||
return self;
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
t0 = self._timecurrent;
|
|
||||||
t1 = datetime.now();
|
|
||||||
self._timecurrent = t1;
|
|
||||||
self._timeelapsed += (t1 - t0);
|
|
||||||
return self;
|
|
||||||
|
|
||||||
def add(self, n: int = 1):
|
def add(self, n: int = 1):
|
||||||
self._nr_steps += n;
|
self._nr_steps += n;
|
||||||
return self;
|
return self;
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
t = datetime.now();
|
|
||||||
self._timeelapsed = t - t;
|
|
||||||
self._nr_steps = 0;
|
self._nr_steps = 0;
|
||||||
self._timecurrent = t;
|
|
||||||
return self;
|
return self;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# CLASS timer
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
class Timer(object):
|
||||||
|
_time_elapsed: timedelta;
|
||||||
|
_time_current: datetime;
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.reset();
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return str(self._time_elapsed);
|
||||||
|
|
||||||
|
@property
|
||||||
|
def elapsedTime(self) -> timedelta:
|
||||||
|
return self._time_elapsed;
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._time_current = datetime.now();
|
||||||
|
return self;
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
t0 = self._time_current;
|
||||||
|
t1 = datetime.now();
|
||||||
|
delta = t1 - t0;
|
||||||
|
self._time_current = t1;
|
||||||
|
self._time_elapsed += delta;
|
||||||
|
return self;
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
t = datetime.now();
|
||||||
|
delta = t - t;
|
||||||
|
self._time_current = t;
|
||||||
|
self._time_elapsed = delta;
|
||||||
|
return self;
|
||||||
|
|
||||||
|
## Initialisierung:
|
||||||
|
_ctr_time = Counter();
|
||||||
|
_ctr_space = Counter();
|
||||||
|
_tmr = Timer();
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# METHODS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
def RestartMetrics():
|
||||||
|
global _ctr_time;
|
||||||
|
global _ctr_space;
|
||||||
|
global _tmr;
|
||||||
|
|
||||||
|
_ctr_time.reset();
|
||||||
|
_ctr_space.reset();
|
||||||
|
_tmr.reset();
|
||||||
|
return;
|
||||||
|
|
||||||
|
def AddTimeCost(n: int = 1):
|
||||||
|
global _ctr_time;
|
||||||
|
_ctr_time.add(n);
|
||||||
|
return;
|
||||||
|
|
||||||
|
def AddSpaceCost(n: int = 1):
|
||||||
|
global _ctr_space;
|
||||||
|
_ctr_space.add(n);
|
||||||
|
return;
|
||||||
|
|
||||||
|
def GetTimeCost() -> int:
|
||||||
|
return _ctr_time.numberOfStep;
|
||||||
|
|
||||||
|
def GetSpaceCost() -> int:
|
||||||
|
return _ctr_space.numberOfStep;
|
||||||
|
|
||||||
|
def GetTimeElapsed() -> timedelta:
|
||||||
|
global _tmr;
|
||||||
|
_tmr.stop();
|
||||||
|
return _tmr.elapsedTime;
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
from src.local.typing import *;
|
from src.local.typing import *;
|
||||||
|
|
||||||
from src.core.log import *;
|
from src.core.log import *;
|
||||||
|
from src.core.metrics import *;
|
||||||
from src.setup.cli import GetArgumentParser;
|
from src.setup.cli import GetArgumentParser;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -67,8 +68,9 @@ def DisplayEndOfAlgorithm(*_: Any, **outputs: Any):
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
def DisplayMetrics():
|
def DisplayMetrics():
|
||||||
logPlain('Dauer der Ausführung: t = \033[1m{}\033[0m'.format(TimeElapsed()));
|
logPlain('Dauer der Ausführung: t = \033[1m{}\033[0m'.format(GetTimeElapsed()));
|
||||||
logPlain('Anzahl der Schritte: T(n) = \033[1m{}\033[0m'.format(NumberOfSteps()));
|
logPlain('Kosten (Zeit): T(n) = \033[1m{}\033[0m'.format(GetTimeCost()));
|
||||||
|
logPlain('Kosten (Platz): S(n) = \033[1m{}\033[0m'.format(GetSpaceCost()));
|
||||||
return;
|
return;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
Loading…
x
Reference in New Issue
Block a user