Compare commits

..

3 Commits

5 changed files with 84 additions and 10 deletions

View File

@ -9,5 +9,6 @@ from code.algorithms.search.sequential import SequentialSearch;
from code.algorithms.search.binary import BinarySearch;
from code.algorithms.search.interpol import InterpolationSearch;
from code.algorithms.search.jump import JumpSearchLinear;
from code.algorithms.search.jump import JumpSearchExponentiell;
from code.algorithms.search.ith_smallest import FindIthSmallest;
from code.algorithms.search.ith_smallest_dc import FindIthSmallestDC;

View File

@ -39,7 +39,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
@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.
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
Annahmen:
- L sei aufsteigend sortiert.
@ -54,9 +54,39 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
block = L[offset:][:m];
elementAfterBlock = block[-1] + 1;
if x < elementAfterBlock:
logDebug('Element muss sich im Block {} befinden.'.format(i));
logDebug('Element muss sich im Block [{i0}, {i1}) befinden.'.format(i0 = i*m, i1 = (i+1)*m));
index = SequentialSearch(L=block, x=x);
return offset + index; # NOTE: muss wegen Offset kompensieren
logDebug('Element befindet sich nicht im Block {}.'.format(i));
logDebug('Element befindet sich nicht im im Block [{i0}, {i1}) befinden.'.format(i0 = i*m, i1 = (i+1)*m));
i += 1;
return -1;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ALGORITHM jump search - exponentiell
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Sprungsuche (exponentiell)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def JumpSearchExponentiell(L: List[int], x: int) -> int:
'''
Inputs: L = Liste von Zahlen, x = Zahl.
Annahmen:
- L sei aufsteigend sortiert.
- L enthält keine Duplikate.
Outputs: Position von x in L, sonst 1 wenn x nicht in L.
'''
i0 = 0;
i1 = 1;
while i0 < len(L):
AddToCounter();
block = L[i0:i1];
elementAfterBlock = block[-1] + 1;
if x < elementAfterBlock:
logDebug('Element muss sich im Block [{i0}, {i1}) befinden.'.format(i0 = i0, i1 = i1));
index = SequentialSearch(L=block, x=x);
return i0 + index; # NOTE: muss wegen Offset kompensieren
logDebug('Element befindet sich nicht im Block [{i0}, {i1}) befinden.'.format(i0 = i0, i1 = i1));
i0 = i1;
i1 *= 2;
return -1;

View File

@ -27,16 +27,39 @@ parts:
L: [1, 3, 5, 7, 11, 13, 16, 17, 23, 33, 34, 35]
x: 17
m: 3
## Aus freiwilligem ÜB 2:
- command: 'algorithm-search-jump-exp'
inputs:
L: [1, 3, 5, 7, 11, 13, 16, 17, 23, 33, 34, 35]
x: 17
m: 3
## Freiwilliges ÜB 2, Aufgabe 5
- command: 'algorithm-search-binary'
description: 'Freiwilliges ÜB2, A5a'
inputs: &ref_inputs_ueb2_5
L: [4, 11, 22, 23, 25, 27, 29, 36, 41, 52, 64, 76, 82, 86, 94, 96, 102, 117, 123, 147, 188, 211, 216, 222, 224, 226, 233, 239, 255, 263, 277, 289]
x: 117
- command: 'algorithm-search-jump'
description: 'Freiwilliges ÜB2, A5b'
inputs:
<<: *ref_inputs_ueb2_5
m: 5
- command: 'algorithm-search-interpolation'
description: 'Freiwilliges ÜB2, A5c'
inputs: *ref_inputs_ueb2_5
## Freiwilliges ÜB 2, Aufgabe 6
- command: 'algorithm-search-ith-element'
description: 'Freiwilliges ÜB2, A6a'
inputs: &ref_inputs_ueb2_6a
L: [16, 15, 9, 5, 1, 10, 7, 14, 19, 8]
i: 2
- command: 'algorithm-search-ith-element-dc'
description: 'Freiwilliges ÜB2, A6a (D&C)'
inputs: *ref_inputs_ueb2_6a
- command: 'algorithm-search-ith-element'
description: 'Freiwilliges ÜB2, A6b'
inputs: &ref_inputs_ueb2_6b
<<: *ref_inputs_ueb2_6a
i: 5
- command: 'algorithm-search-ith-element-dc'
description: 'Freiwilliges ÜB2, A6b (D&C)'
inputs: *ref_inputs_ueb2_6b

View File

@ -47,11 +47,12 @@ def LoopThroughCases(path: str):
config = ReadConfigFile(path);
cases = GetAttribute(config, 'parts', 'cases', expectedtype=list, default=[]);
for caseindex, case in enumerate(cases):
DisplayStartOfCase(caseindex);
command = GetAttribute(case, 'command', expectedtype=str, default='');
descr = GetAttribute(case, 'description', expectedtype=str, default='');
inputs = GetAttribute(case, 'inputs', expectedtype=dict, default={});
DisplayStartOfCase(caseindex, descr);
try:
if command == 'algorithm-sum-maxsub':
MaxSubSum(L=inputs['L']);
@ -65,6 +66,8 @@ def LoopThroughCases(path: str):
InterpolationSearch(L=inputs['L'], x=inputs['x'], u=0, v=len(inputs['L'])-1);
elif command == 'algorithm-search-jump':
JumpSearchLinear(L=inputs['L'], x=inputs['x'], m=inputs['m']);
elif command == 'algorithm-search-jump-exp':
JumpSearchExponentiell(L=inputs['L'], x=inputs['x']);
elif command == 'algorithm-search-ith-element':
FindIthSmallest(L=inputs['L'], i=inputs['i']);
elif command == 'algorithm-search-ith-element-dc':

View File

@ -23,15 +23,32 @@ def DisplayHelpMessage():
# METHODS display case
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def DisplayStartOfCase(name: Any):
def DisplayStartOfCase(index: int, descr: Any):
DisplayBar(80);
logPlain('\033[92;1mCASE {}\033[0m'.format(name))
if not isinstance(descr, str) or descr == '':
logPlain('\033[92;1mCASE {index}\033[0m.'.format(index=index));
else:
logPlain('\033[92;1mCASE {index}\033[0m (\033[1;2m{descr}\033[0m).'.format(index=index, descr=descr));
return;
def DisplayEndOfCase():
DisplayBar(80);
return;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHODS display value
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def RepresentValue(value: Any) -> Any:
if value is None or isinstance(value, (str, bool, int, float)):
return value;
elif isinstance(value, list):
if len(value) > 10:
value = value[:3] + [ '...' ] + value[-2:];
# return '[{}, ..., {}]'.format(', '.join(value[:3]), ', '.join(value[-2:])
return value;
return value;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHODS display algorithm start/end
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -40,13 +57,13 @@ def DisplayStartOfAlgorithm(name: str, *_: Any, **inputs: Any):
logPlain('Ausführung vom Algorithmus: \033[92;1m{}\033[0m'.format(name));
logPlain('INPUTS');
for varname, value in inputs.items():
logPlain(' - {} = {}'.format(varname, value))
logPlain(' - {} = {}'.format(varname, RepresentValue(value)))
return;
def DisplayEndOfAlgorithm(*_: Any, **outputs: Any):
logPlain('OUTPUTS:')
for varname, value in outputs.items():
logPlain(' - {} = {}'.format(varname, value))
logPlain(' - {} = {}'.format(varname, RepresentValue(value)))
return;
def DisplayMetrics():