Compare commits
No commits in common. "aa7c983e995d6900c634a3e94aaba75b12cae8a3" and "d18957351d5ace61ccefe20414ecb58776fbaf93" have entirely different histories.
aa7c983e99
...
d18957351d
@ -41,8 +41,8 @@ Python version 3.x.x (idealerweise zumindest 3.9.5) plus folgende Module:
|
|||||||
|
|
||||||
Diese lassen sich mittels
|
Diese lassen sich mittels
|
||||||
```bash
|
```bash
|
||||||
python3 -m pip install {name des Moduls}; # linux, osx
|
python3 -m pip install {name des Moduls} # linux, osx
|
||||||
py -3 -m pip install {name des Moduls}; # Windows
|
py -3 -m pip install {name des Moduls} # Windows
|
||||||
```
|
```
|
||||||
installieren.
|
installieren.
|
||||||
Man kann auch die Version mittels etwa `{name}==1.2.3` oder `{name}>=1.2.3` spezifizieren.
|
Man kann auch die Version mittels etwa `{name}==1.2.3` oder `{name}>=1.2.3` spezifizieren.
|
||||||
@ -61,5 +61,3 @@ chmod +x run.sh; # nur einmalig nötig
|
|||||||
./run.sh
|
./run.sh
|
||||||
```
|
```
|
||||||
aus.
|
aus.
|
||||||
|
|
||||||
Der Befehl `[python3 | py -3] code/main.py -h` zeigt die Gebrauchsanleitung an.
|
|
||||||
|
@ -1,90 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# IMPORTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
import functools;
|
|
||||||
|
|
||||||
from code.core.log import *;
|
|
||||||
from code.display.display import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CLASSES
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
class OneShot(object):
|
|
||||||
state: bool;
|
|
||||||
def __init__(self):
|
|
||||||
self.state = True;
|
|
||||||
|
|
||||||
def reload(self):
|
|
||||||
self.state = True;
|
|
||||||
|
|
||||||
def trigger(self):
|
|
||||||
self.state = False;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# DECORATORS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
## Trigger, der das fancy Zeug bei verschachtelten Algorithmen verhindert
|
|
||||||
nonnestedAlgorithms = OneShot();
|
|
||||||
|
|
||||||
def algorithmInfos(
|
|
||||||
name: str,
|
|
||||||
checks: bool = True,
|
|
||||||
metrics: bool = None,
|
|
||||||
outputnames: Union[str, Tuple[str]] = 'result',
|
|
||||||
preChecks: Any = None,
|
|
||||||
postChecks: Any = None
|
|
||||||
):
|
|
||||||
'''
|
|
||||||
Decorator für Algorthmen, der Folgendes macht:
|
|
||||||
|
|
||||||
- Zeigt vorm Start Console-Messages mit Namen des Algorithmus + Inputs.
|
|
||||||
- Zeit am Ende optional Metriken (bspw. Zeitkosten).
|
|
||||||
- Zeigt am Ende Console-Messages von Output/s.
|
|
||||||
- Prüft optional vor und nach Ausführung des Algorithmus, dass die Inputs/Outputs stimmen.
|
|
||||||
|
|
||||||
Bei rekursiven Definitionen werden die o. s. Punkte nur bei dem äußersten Aufruf gemacht.
|
|
||||||
'''
|
|
||||||
def func_decorator(func):
|
|
||||||
## Trigger, der das fancy Zeug bei verschachtelten Aufrufen verhindert
|
|
||||||
nonnestedRecursion = OneShot();
|
|
||||||
@functools.wraps(func)
|
|
||||||
def func_wrapper(**inputs):
|
|
||||||
try:
|
|
||||||
state = nonnestedRecursion.state and nonnestedAlgorithms.state;
|
|
||||||
if state:
|
|
||||||
# Initialisierung
|
|
||||||
DisplayStartOfAlgorithm(name.title(), **inputs);
|
|
||||||
RestartCounter();
|
|
||||||
# Prechecks
|
|
||||||
if checks and callable(preChecks):
|
|
||||||
preChecks(**inputs);
|
|
||||||
# Ausführung des Algorithmus:
|
|
||||||
nonnestedRecursion.trigger();
|
|
||||||
nonnestedAlgorithms.trigger();
|
|
||||||
outputs = func(*[], **inputs);
|
|
||||||
nonnestedAlgorithms.reload();
|
|
||||||
nonnestedRecursion.reload();
|
|
||||||
if state:
|
|
||||||
# benenne 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_) };
|
|
||||||
# Postchecks
|
|
||||||
if checks and callable(postChecks):
|
|
||||||
postChecks(**inputs, **outputsNamed);
|
|
||||||
# Letzte Messages
|
|
||||||
if metrics:
|
|
||||||
DisplayMetrics();
|
|
||||||
DisplayEndOfAlgorithm(**outputsNamed);
|
|
||||||
except Exception as e:
|
|
||||||
_switch.reload();
|
|
||||||
raise e;
|
|
||||||
return outputs;
|
|
||||||
return func_wrapper;
|
|
||||||
return func_decorator;
|
|
@ -9,7 +9,6 @@ from local.maths import *;
|
|||||||
from local.typing import *;
|
from local.typing import *;
|
||||||
|
|
||||||
from code.core.log import *;
|
from code.core.log import *;
|
||||||
from code.algorithms.methods import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# GLOBAL VARIABLES/CONSTANTS
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
@ -17,24 +16,10 @@ from code.algorithms.methods import *;
|
|||||||
|
|
||||||
#
|
#
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CHECKS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
def preChecks(L: List[int], **_):
|
|
||||||
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
|
||||||
return;
|
|
||||||
|
|
||||||
def postChecks(L: List[int], x: int, index: int, **_):
|
|
||||||
value = L[index] if index >= 0 else None;
|
|
||||||
assert value == x, 'Der Algorithmus hat versagt.';
|
|
||||||
return;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM binary search
|
# ALGORITHM binary search
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@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.
|
||||||
@ -51,9 +36,9 @@ def BinarySearch(L: List[int], x: int) -> int:
|
|||||||
return m;
|
return m;
|
||||||
elif x < L[m]:
|
elif x < L[m]:
|
||||||
logDebug('Suche in L[0], L[1], ..., L[m] fortsetzen, m = {}.'.format(m));
|
logDebug('Suche in L[0], L[1], ..., L[m] fortsetzen, m = {}.'.format(m));
|
||||||
index = BinarySearch(L=L[:m], x=x);
|
index = BinarySearch(L[:m], x);
|
||||||
return index;
|
return index;
|
||||||
else: # x > L[m]
|
else: # x > L[m]
|
||||||
logDebug('Suche in L[m+1], L[m+2], ..., L[len(L)-1] fortsetzen, m = {}.'.format(m));
|
logDebug('Suche in L[m+1], L[m+2], ..., L[len(L)-1] fortsetzen, m = {}.'.format(m));
|
||||||
index = BinarySearch(L=L[m+1:], x=x);
|
index = BinarySearch(L[m+1:], x);
|
||||||
return (m + 1) + index; # NOTE: muss Indexwert kompensieren
|
return (m + 1) + index; # NOTE: muss Indexwert kompensieren
|
||||||
|
@ -9,7 +9,6 @@ from local.maths import *;
|
|||||||
from local.typing import *;
|
from local.typing import *;
|
||||||
|
|
||||||
from code.core.log import *;
|
from code.core.log import *;
|
||||||
from code.algorithms.methods import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# GLOBAL VARIABLES/CONSTANTS
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
@ -17,24 +16,10 @@ from code.algorithms.methods import *;
|
|||||||
|
|
||||||
#
|
#
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CHECKS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
def preChecks(L: List[int], **_):
|
|
||||||
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
|
||||||
return;
|
|
||||||
|
|
||||||
def postChecks(L: List[int], x: int, p: int, **_):
|
|
||||||
value = L[p] if p >= 0 else None;
|
|
||||||
assert value == x, 'Der Algorithmus hat versagt.';
|
|
||||||
return;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM interpolation
|
# ALGORITHM interpolation
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@algorithmInfos(name='Interpolationssuchalgorithmus', outputnames='p', 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.
|
||||||
|
@ -10,7 +10,6 @@ from local.typing import *;
|
|||||||
|
|
||||||
from code.core.log import *;
|
from code.core.log import *;
|
||||||
from code.algorithms.search.sequential import SequentialSearch;
|
from code.algorithms.search.sequential import SequentialSearch;
|
||||||
from code.algorithms.methods import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# GLOBAL VARIABLES/CONSTANTS
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
@ -18,25 +17,10 @@ from code.algorithms.methods import *;
|
|||||||
|
|
||||||
#
|
#
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CHECKS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
def preChecks(L: List[int], **_):
|
|
||||||
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
|
||||||
assert L == list(sorted(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
|
|
||||||
return;
|
|
||||||
|
|
||||||
def postChecks(L: List[int], x: int, index: int, **_):
|
|
||||||
value = L[index] if index >= 0 else None;
|
|
||||||
assert value == x, 'Der Algorithmus hat versagt.';
|
|
||||||
return;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM jump search
|
# ALGORITHM jump search
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@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, [u, v] = Suchinterval.
|
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||||||
@ -53,7 +37,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
|||||||
elementAfterBlock = block[-1] + 1;
|
elementAfterBlock = block[-1] + 1;
|
||||||
if x < elementAfterBlock:
|
if x < elementAfterBlock:
|
||||||
logDebug('Element muss sich im Block {} befinden.'.format(i));
|
logDebug('Element muss sich im Block {} befinden.'.format(i));
|
||||||
index = SequentialSearch(L=block, x=x);
|
index = SequentialSearch(block, x);
|
||||||
return offset + index; # NOTE: muss wegen Offset kompensieren
|
return offset + index; # NOTE: muss wegen Offset kompensieren
|
||||||
logDebug('Element befindet sich nicht im Block {}.'.format(i));
|
logDebug('Element befindet sich nicht im Block {}.'.format(i));
|
||||||
i += 1;
|
i += 1;
|
||||||
|
@ -9,7 +9,6 @@ from local.maths import *;
|
|||||||
from local.typing import *;
|
from local.typing import *;
|
||||||
|
|
||||||
from code.core.log import *;
|
from code.core.log import *;
|
||||||
from code.algorithms.methods import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# GLOBAL VARIABLES/CONSTANTS
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
@ -17,24 +16,10 @@ from code.algorithms.methods import *;
|
|||||||
|
|
||||||
#
|
#
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CHECKS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
def preChecks(L: List[int], **_):
|
|
||||||
# Keine Checks!
|
|
||||||
return;
|
|
||||||
|
|
||||||
def postChecks(L: List[int], x: int, index: int, **_):
|
|
||||||
value = L[index] if index >= 0 else None;
|
|
||||||
assert value == x, 'Der Algorithmus hat versagt.';
|
|
||||||
return;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# ALGORITHM sequential search
|
# ALGORITHM sequential search
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@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.
|
||||||
|
@ -22,30 +22,21 @@ def DisplayCase(name: Any):
|
|||||||
# METHODS display algorithm start/end
|
# METHODS display algorithm start/end
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def DisplayStartOfAlgorithm(name: str, *_: Any, **inputs: Any):
|
def DisplayStartOfAlgorithm(name: str, **inputs: Any):
|
||||||
DisplayBar(80);
|
logInfo('Ausführung vom Algorithmus: \033[92;1m{}\033[0m'.format(name))
|
||||||
logPlain('Ausführung vom Algorithmus: \033[92;1m{}\033[0m'.format(name));
|
logInfo('INPUTS');
|
||||||
logPlain('INPUTS');
|
|
||||||
for varname, value in inputs.items():
|
for varname, value in inputs.items():
|
||||||
logPlain(' - {} = {}'.format(varname, value))
|
logPlain(' - {} = {}'.format(varname, value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
def DisplayEndOfAlgorithm(*_: Any, **outputs: Any):
|
def DisplayEndOfAlgorithm(**outputs: Any):
|
||||||
logPlain('OUTPUTS:')
|
logInfo('OUTPUTS:')
|
||||||
for varname, value in outputs.items():
|
for varname, value in outputs.items():
|
||||||
logPlain(' - {} = {}'.format(varname, value))
|
logPlain(' - {} = {}'.format(varname, value))
|
||||||
DisplayBar(80);
|
DisplayMetrics()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
def DisplayMetrics():
|
def DisplayMetrics():
|
||||||
logPlain('Dauer der Ausführung: t = \033[2m{}\033[0m'.format(TimeElapsed()));
|
logInfo('Dauer der Ausführung: t = \033[2m{}\033[0m'.format(TimeElapsed()))
|
||||||
logPlain('Anzahl der Schritte: T(n) = \033[1m{}\033[0m'.format(NumberOfSteps()));
|
logInfo('Anzahl der Schritte: T(n) = \033[1m{}\033[0m'.format(NumberOfSteps()))
|
||||||
return;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# METHODS Verschiedenes
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
def DisplayBar(n: int = 80):
|
|
||||||
logPlain('+{}+'.format('-'*n));
|
|
||||||
return;
|
return;
|
||||||
|
57
code/main.py
57
code/main.py
@ -51,20 +51,51 @@ def LoopThroughCases(path: str):
|
|||||||
|
|
||||||
command = GetAttribute(case, 'command', expectedtype=str, default='');
|
command = GetAttribute(case, 'command', expectedtype=str, default='');
|
||||||
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
|
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
|
||||||
|
checks = GetAttribute(case, 'check', expectedtype=bool, default=True);
|
||||||
|
|
||||||
try:
|
RestartCounter();
|
||||||
if command == 'algorithm-search-sequential':
|
if command == 'algorithm-search-sequential':
|
||||||
SequentialSearch(L=inputs['L'], x=inputs['x']);
|
L, x = inputs['L'], inputs['x'];
|
||||||
elif command == 'algorithm-search-binary':
|
DisplayStartOfAlgorithm('Sequenziellsuche', L=L, x=x);
|
||||||
BinarySearch(L=inputs['L'], x=inputs['x']);
|
p = SequentialSearch(L=L, x=x);
|
||||||
elif command == 'algorithm-search-interpolation':
|
value = L[p] if p >= 0 else None;
|
||||||
InterpolationSearch(L=inputs['L'], x=inputs['x'], u=0, v=len(inputs['L'])-1);
|
if checks:
|
||||||
elif command == 'algorithm-search-jump':
|
assert value == x, 'Der Algorithmus hat versagt.';
|
||||||
JumpSearchLinear(L=inputs['L'], x=inputs['x'], m=inputs['m']);
|
DisplayEndOfAlgorithm(p = p);
|
||||||
else:
|
elif command == 'algorithm-search-binary':
|
||||||
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
|
L, x = inputs['L'], inputs['x'];
|
||||||
except Exception as e:
|
DisplayStartOfAlgorithm('Binärsuche', L=L, x=x);
|
||||||
logError(e);
|
if checks:
|
||||||
|
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
||||||
|
p = BinarySearch(L=L, x=x);
|
||||||
|
value = L[p] if p >= 0 else None;
|
||||||
|
if checks:
|
||||||
|
assert value == x, 'Der Algorithmus hat versagt.';
|
||||||
|
DisplayEndOfAlgorithm(p = p);
|
||||||
|
elif command == 'algorithm-search-interpolation':
|
||||||
|
L, x = inputs['L'], inputs['x'];
|
||||||
|
u, v = 0, len(L)-1;
|
||||||
|
DisplayStartOfAlgorithm('Interpolationssuche', L=L, x=x, u=u, v=v);
|
||||||
|
if checks:
|
||||||
|
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
||||||
|
p = InterpolationSearch(L=L, x=x, u=u, v=v);
|
||||||
|
value = L[p] if p >= 0 else None;
|
||||||
|
if checks:
|
||||||
|
assert value == x, 'Der Algorithmus hat versagt.';
|
||||||
|
DisplayEndOfAlgorithm(p = p);
|
||||||
|
elif command == 'algorithm-search-jump':
|
||||||
|
L, x, m = inputs['L'], inputs['x'], inputs['m'];
|
||||||
|
DisplayStartOfAlgorithm('SprungSuche', L=L, x=x, m=m);
|
||||||
|
if checks:
|
||||||
|
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
||||||
|
assert L == list(sorted(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
|
||||||
|
p = JumpSearchLinear(L=L, x=x, m=m);
|
||||||
|
value = L[p] if p >= 0 else None;
|
||||||
|
if checks:
|
||||||
|
assert value == x, 'Der Algorithmus hat versagt.';
|
||||||
|
DisplayEndOfAlgorithm(p = p);
|
||||||
|
else:
|
||||||
|
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
|
||||||
return;
|
return;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
Loading…
x
Reference in New Issue
Block a user