master > master: code - decorators angewandt
This commit is contained in:
parent
2693b6768c
commit
85bd7d4f41
@ -9,6 +9,7 @@ from local.maths import *;
|
||||
from local.typing import *;
|
||||
|
||||
from code.core.log import *;
|
||||
from code.algorithms.methods import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
@ -16,10 +17,24 @@ from code.core.log 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
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@algorithmInfos(name='Binärsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
def BinarySearch(L: List[int], x: int) -> int:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
@ -36,9 +51,9 @@ def BinarySearch(L: List[int], x: int) -> int:
|
||||
return m;
|
||||
elif x < L[m]:
|
||||
logDebug('Suche in L[0], L[1], ..., L[m] fortsetzen, m = {}.'.format(m));
|
||||
index = BinarySearch(L[:m], x);
|
||||
index = BinarySearch(L=L[:m], x=x);
|
||||
return index;
|
||||
else: # x > L[m]
|
||||
logDebug('Suche in L[m+1], L[m+2], ..., L[len(L)-1] fortsetzen, m = {}.'.format(m));
|
||||
index = BinarySearch(L[m+1:], x);
|
||||
index = BinarySearch(L=L[m+1:], x=x);
|
||||
return (m + 1) + index; # NOTE: muss Indexwert kompensieren
|
||||
|
@ -9,6 +9,7 @@ from local.maths import *;
|
||||
from local.typing import *;
|
||||
|
||||
from code.core.log import *;
|
||||
from code.algorithms.methods import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
@ -16,10 +17,24 @@ from code.core.log 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
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@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:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||||
|
@ -10,6 +10,7 @@ from local.typing import *;
|
||||
|
||||
from code.core.log import *;
|
||||
from code.algorithms.search.sequential import SequentialSearch;
|
||||
from code.algorithms.methods import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
@ -17,10 +18,25 @@ from code.algorithms.search.sequential import SequentialSearch;
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# 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
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@algorithmInfos(name='Sprungsuche', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
|
||||
@ -37,7 +53,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
||||
elementAfterBlock = block[-1] + 1;
|
||||
if x < elementAfterBlock:
|
||||
logDebug('Element muss sich im Block {} befinden.'.format(i));
|
||||
index = SequentialSearch(block, x);
|
||||
index = SequentialSearch(L=block, x=x);
|
||||
return offset + index; # NOTE: muss wegen Offset kompensieren
|
||||
logDebug('Element befindet sich nicht im Block {}.'.format(i));
|
||||
i += 1;
|
||||
|
@ -9,6 +9,7 @@ from local.maths import *;
|
||||
from local.typing import *;
|
||||
|
||||
from code.core.log import *;
|
||||
from code.algorithms.methods import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
@ -16,10 +17,24 @@ from code.core.log 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
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@algorithmInfos(name='Sequenziellsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
def SequentialSearch(L: List[int], x: int) -> int:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
|
45
code/main.py
45
code/main.py
@ -51,51 +51,20 @@ def LoopThroughCases(path: str):
|
||||
|
||||
command = GetAttribute(case, 'command', expectedtype=str, default='');
|
||||
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
|
||||
checks = GetAttribute(case, 'check', expectedtype=bool, default=True);
|
||||
|
||||
RestartCounter();
|
||||
try:
|
||||
if command == 'algorithm-search-sequential':
|
||||
L, x = inputs['L'], inputs['x'];
|
||||
DisplayStartOfAlgorithm('Sequenziellsuche', L=L, x=x);
|
||||
p = SequentialSearch(L=L, x=x);
|
||||
value = L[p] if p >= 0 else None;
|
||||
if checks:
|
||||
assert value == x, 'Der Algorithmus hat versagt.';
|
||||
DisplayEndOfAlgorithm(p = p);
|
||||
SequentialSearch(L=inputs['L'], x=inputs['x']);
|
||||
elif command == 'algorithm-search-binary':
|
||||
L, x = inputs['L'], inputs['x'];
|
||||
DisplayStartOfAlgorithm('Binärsuche', L=L, x=x);
|
||||
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);
|
||||
BinarySearch(L=inputs['L'], x=inputs['x']);
|
||||
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);
|
||||
InterpolationSearch(L=inputs['L'], x=inputs['x'], u=0, v=len(inputs['L'])-1);
|
||||
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);
|
||||
JumpSearchLinear(L=inputs['L'], x=inputs['x'], m=inputs['m']);
|
||||
else:
|
||||
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
|
||||
except Exception as e:
|
||||
logError(e);
|
||||
return;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
Loading…
x
Reference in New Issue
Block a user