master > master: code - Auswahlproblem hinzugefügt
This commit is contained in:
parent
64df98fcf3
commit
62e1e7dcca
@ -9,4 +9,5 @@ from code.algorithms.search.sequential import SequentialSearch;
|
|||||||
from code.algorithms.search.binary import BinarySearch;
|
from code.algorithms.search.binary import BinarySearch;
|
||||||
from code.algorithms.search.interpol import InterpolationSearch;
|
from code.algorithms.search.interpol import InterpolationSearch;
|
||||||
from code.algorithms.search.jump import JumpSearchLinear;
|
from code.algorithms.search.jump import JumpSearchLinear;
|
||||||
from code.algorithms.search.selection import SelectionAlgorithm;
|
from code.algorithms.search.ith_smallest import FindIthSmallest;
|
||||||
|
from code.algorithms.search.ith_smallest_dc import FindIthSmallestDC;
|
||||||
|
65
code/algorithms/search/ith_smallest.py
Normal file
65
code/algorithms/search/ith_smallest.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# IMPORTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
from local.maths import *;
|
||||||
|
from local.typing import *;
|
||||||
|
|
||||||
|
from code.core.log import *;
|
||||||
|
from code.algorithms.search.sequential import SequentialSearch;
|
||||||
|
from code.algorithms.methods import *;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
#
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# CHECKS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
def preChecks(L: List[int], i: int, **_):
|
||||||
|
assert 1 <= i and i <= len(L), 'Der Wert von i muss zw. {lb} und {ub} liegen.'.format(lb = 1, ub = len(L));
|
||||||
|
assert sorted(L) == sorted(list(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
|
||||||
|
return;
|
||||||
|
|
||||||
|
def postChecks(L: List[int], i: int, value: int, **_):
|
||||||
|
L_ = sorted(L);
|
||||||
|
assert L_[i-1] == value, 'Der Algorithmus hat versagt.';
|
||||||
|
return;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# ALGORITHM jump search
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||||
|
def FindIthSmallest(L: List[int], i: int) -> int:
|
||||||
|
'''
|
||||||
|
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||||
|
|
||||||
|
Annahmen:
|
||||||
|
|
||||||
|
- L enthält keine Duplikate.
|
||||||
|
- L enthält mindestens i Elemente.
|
||||||
|
|
||||||
|
Outputs: Wert des i. kleinste Element in L.
|
||||||
|
Beachte 1.kleinstes <==> Minimum.
|
||||||
|
'''
|
||||||
|
index = 0;
|
||||||
|
minValue = L[0];
|
||||||
|
AddToCounter(len(L));
|
||||||
|
for i_ in range(1, len(L)):
|
||||||
|
if L[i_] < minValue:
|
||||||
|
index = i_;
|
||||||
|
minValue = L[i_];
|
||||||
|
if i == 1:
|
||||||
|
logDebug('Das i. kleinste Element wurde gefunden.');
|
||||||
|
return minValue;
|
||||||
|
else:
|
||||||
|
logDebug('Entfernte Minimum: {value}.'.format(value = minValue));
|
||||||
|
i = i - 1;
|
||||||
|
return FindIthSmallest(L=L[:index] + L[(index+1):], i=i);
|
65
code/algorithms/search/ith_smallest_dc.py
Normal file
65
code/algorithms/search/ith_smallest_dc.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# IMPORTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
from local.maths import *;
|
||||||
|
from local.typing import *;
|
||||||
|
|
||||||
|
from code.core.log import *;
|
||||||
|
from code.algorithms.search.sequential import SequentialSearch;
|
||||||
|
from code.algorithms.methods import *;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# GLOBAL VARIABLES/CONSTANTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
#
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# CHECKS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
def preChecks(L: List[int], i: int, **_):
|
||||||
|
assert 1 <= i and i <= len(L), 'Der Wert von i muss zw. {lb} und {ub} liegen.'.format(lb = 1, ub = len(L));
|
||||||
|
assert sorted(L) == sorted(list(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
|
||||||
|
return;
|
||||||
|
|
||||||
|
def postChecks(L: List[int], i: int, value: int, **_):
|
||||||
|
L_ = sorted(L);
|
||||||
|
assert L_[i-1] == value, 'Der Algorithmus hat versagt.';
|
||||||
|
return;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# ALGORITHM jump search
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@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:
|
||||||
|
'''
|
||||||
|
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||||
|
|
||||||
|
Annahmen:
|
||||||
|
|
||||||
|
- L enthält keine Duplikate.
|
||||||
|
- L enthält mindestens i Elemente.
|
||||||
|
|
||||||
|
Outputs: Wert des i. kleinste Element in L.
|
||||||
|
Beachte 1.kleinstes <==> Minimum.
|
||||||
|
'''
|
||||||
|
AddToCounter();
|
||||||
|
p = L[len(L)-1]; # NOTE: Pivotelement kann beliebig gewählt werden
|
||||||
|
Ll = [ x for x in L if x < p ];
|
||||||
|
Lr = [ x for x in L if x > p ];
|
||||||
|
if len(Ll) == i - 1:
|
||||||
|
logDebug('Es gibt genau i-1 Elemente vorm Pivotelment, p. Also ist p das i. kleinste Element.');
|
||||||
|
return p;
|
||||||
|
elif len(Ll) >= i:
|
||||||
|
logDebug('Es gibt mindestens i Elemente vorm Pivotelement, p. Suche wird in linker Hälfte fortgesetzt.');
|
||||||
|
return FindIthSmallestDC(L=Ll, i=i);
|
||||||
|
else:
|
||||||
|
i = i - (len(Ll) + 1)
|
||||||
|
logDebug('Es gibt weniger als i-1 Elemente vorm Pivotelement, p. Suche wird in rechte Hälfte nach {i}. kleinstem Element fortgesetzt.'.format(i=i));
|
||||||
|
return FindIthSmallestDC(L=Lr, i=i);
|
@ -24,7 +24,7 @@ from code.algorithms.methods import *;
|
|||||||
|
|
||||||
def preChecks(L: List[int], **_):
|
def preChecks(L: List[int], **_):
|
||||||
assert L == sorted(L), 'Ungültiger Input: L muss aufsteigend sortiert sein!';
|
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!';
|
assert L == sorted(list(set(L))), 'Ungültiger Input: L darf keine Duplikate enthalten!';
|
||||||
return;
|
return;
|
||||||
|
|
||||||
def postChecks(L: List[int], x: int, index: int, **_):
|
def postChecks(L: List[int], x: int, index: int, **_):
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# IMPORTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
from local.maths import *;
|
|
||||||
from local.typing import *;
|
|
||||||
|
|
||||||
from code.core.log import *;
|
|
||||||
from code.algorithms.search.sequential import SequentialSearch;
|
|
||||||
from code.algorithms.methods import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# GLOBAL VARIABLES/CONSTANTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
#
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# 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='Auswahlproblem (i. Element)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
|
||||||
def SelectionAlgorithm(L: List[int], i: int) -> int:
|
|
||||||
'''
|
|
||||||
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
|
||||||
Outputs: Position des i. kleinste Element, x, in L, sonst −1. Beachte i=0 <==> Minimum.
|
|
||||||
'''
|
|
||||||
## TODO
|
|
||||||
return -1;
|
|
@ -65,6 +65,10 @@ def LoopThroughCases(path: str):
|
|||||||
InterpolationSearch(L=inputs['L'], x=inputs['x'], u=0, v=len(inputs['L'])-1);
|
InterpolationSearch(L=inputs['L'], x=inputs['x'], u=0, v=len(inputs['L'])-1);
|
||||||
elif command == 'algorithm-search-jump':
|
elif command == 'algorithm-search-jump':
|
||||||
JumpSearchLinear(L=inputs['L'], x=inputs['x'], m=inputs['m']);
|
JumpSearchLinear(L=inputs['L'], x=inputs['x'], m=inputs['m']);
|
||||||
|
elif command == 'algorithm-search-ith-element':
|
||||||
|
FindIthSmallest(L=inputs['L'], i=inputs['i']);
|
||||||
|
elif command == 'algorithm-search-ith-element-dc':
|
||||||
|
FindIthSmallestDC(L=inputs['L'], i=inputs['i']);
|
||||||
else:
|
else:
|
||||||
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
|
raise ValueError('Command \033[1m{}\033[0m nicht erkannt'.format(command));
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user