master > master: src py - minor änderungen
This commit is contained in:
@@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
||||
# 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:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
@@ -49,7 +49,7 @@ def BinarySearch(L: List[int], x: int) -> int:
|
||||
if len(L) == 0:
|
||||
logDebug('x nicht in L');
|
||||
return -1;
|
||||
AddToCounter();
|
||||
AddTimeCost();
|
||||
m = math.floor(len(L)/2);
|
||||
if L[m] == x:
|
||||
logDebug('x in Position m gefunden');
|
||||
|
||||
@@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
||||
# 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:
|
||||
'''
|
||||
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.
|
||||
Outputs: Interpolierte Position, um Suchinterval ausgeglichen aufzuteilen.
|
||||
'''
|
||||
AddToCounter();
|
||||
AddTimeCost();
|
||||
r = (x - L[u])/(L[v]-L[u]);
|
||||
p = math.floor(u + r*(v-u))
|
||||
return p;
|
||||
|
||||
@@ -33,10 +33,10 @@ def postChecks(L: List[int], i: int, value: int, **_):
|
||||
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:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, i = Ordinalzahl
|
||||
@@ -51,7 +51,7 @@ def FindIthSmallest(L: List[int], i: int) -> int:
|
||||
'''
|
||||
index = 0;
|
||||
minValue = L[0];
|
||||
AddToCounter(len(L));
|
||||
AddTimeCost(len(L));
|
||||
for i_ in range(1, len(L)):
|
||||
if L[i_] < minValue:
|
||||
index = i_;
|
||||
@@ -65,10 +65,10 @@ def FindIthSmallest(L: List[int], i: int) -> int:
|
||||
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:
|
||||
'''
|
||||
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.
|
||||
Beachte 1.kleinstes <==> Minimum.
|
||||
'''
|
||||
AddToCounter();
|
||||
AddTimeCost();
|
||||
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 ];
|
||||
|
||||
@@ -39,7 +39,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
|
||||
# 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:
|
||||
'''
|
||||
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;
|
||||
while i*m < len(L):
|
||||
AddToCounter();
|
||||
AddTimeCost();
|
||||
offset = i*m;
|
||||
block = L[offset:][:m];
|
||||
elementAfterBlock = block[-1] + 1;
|
||||
@@ -70,7 +70,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
|
||||
# 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:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
@@ -84,7 +84,7 @@ def JumpSearchExponentiell(L: List[int], x: int) -> int:
|
||||
i0 = 0;
|
||||
i1 = 1;
|
||||
while i0 < len(L):
|
||||
AddToCounter();
|
||||
AddTimeCost();
|
||||
block = L[i0:i1];
|
||||
elementAfterBlock = block[-1] + 1;
|
||||
if x < elementAfterBlock:
|
||||
|
||||
@@ -21,23 +21,25 @@ from src.algorithms.methods import *;
|
||||
# CHECKS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def preChecks(L: List[bool], **_):
|
||||
assert sum(L) > 0, 'Mindestens ein Getränk muss vergiftet sein!';
|
||||
assert sum(L) == 1, 'Höchstens ein Getränk darf vergiftet sein!';
|
||||
def preChecks(L: List[int], **_):
|
||||
s = sum(L);
|
||||
assert s > 0, 'Mindestens ein Getränk muss vergiftet sein!';
|
||||
assert s <= 1, 'Höchstens ein Getränk darf vergiftet sein!';
|
||||
return;
|
||||
|
||||
def postChecks(L: List[bool], index: int, **_):
|
||||
assert L[index] == True, 'Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.';
|
||||
def postChecks(L: List[int], index: int, **_):
|
||||
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;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# ALGORITHM find poison
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@algorithmInfos(name='Giftsuche (O(n) Vorkoster)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
def FindPoison(L: List[bool]) -> int:
|
||||
@algorithmInfos(name='Giftsuche (O(n) Vorkoster)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
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.
|
||||
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);
|
||||
testers = [];
|
||||
for i in range(n):
|
||||
AddToCounter();
|
||||
AddSpaceCost();
|
||||
logDebug('Füge Vorkoster hinzu, der nur Getränk {i} testet.'.format(i=i))
|
||||
testers.append([i]);
|
||||
logDebug('Warte auf Effekte');
|
||||
@@ -62,10 +64,10 @@ def FindPoison(L: List[bool]) -> int:
|
||||
# ALGORITHM find poison fast
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@algorithmInfos(name='Giftsuche (O(log(n)) Vorkoster)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
def FindPoisonFast(L: List[bool]) -> List[int]:
|
||||
@algorithmInfos(name='Giftsuche (O(log(n)) Vorkoster)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
|
||||
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.
|
||||
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) ];
|
||||
# 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.
|
||||
AddToCounter(1);
|
||||
AddSpaceCost(1);
|
||||
logDebug('Füge Vorkoster hinzu, der alle Getränke k testet mit {i}. Bit = 0.'.format(i=i))
|
||||
testers.append(tester0);
|
||||
testers.append(tester1);
|
||||
@@ -97,10 +99,10 @@ def FindPoisonFast(L: List[bool]) -> List[int]:
|
||||
# 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:
|
||||
- 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.
|
||||
|
||||
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
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@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:
|
||||
'''
|
||||
Inputs: L = Liste von Zahlen, x = Zahl.
|
||||
@@ -45,7 +45,7 @@ def SequentialSearch(L: List[int], x: int) -> int:
|
||||
'''
|
||||
n = len(L);
|
||||
for i in range(n):
|
||||
AddToCounter();
|
||||
AddTimeCost();
|
||||
if L[i] == x:
|
||||
logDebug('Element in Position {} gefunden.'.format(i));
|
||||
return i;
|
||||
|
||||
Reference in New Issue
Block a user