From 82962e376d000cb0087d8fb8fed0daa1b03d38bd Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Mon, 25 Oct 2021 11:33:22 +0200 Subject: [PATCH] master > master: code - assertions korrigiert (index == -1 ist in Ordnung, wenn erwartet) --- code/algorithms/search/binary.py | 7 +++++-- code/algorithms/search/interpol.py | 11 +++++++---- code/algorithms/search/ith_smallest.py | 4 ++-- code/algorithms/search/jump.py | 7 +++++-- code/algorithms/search/sequential.py | 7 +++++-- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/code/algorithms/search/binary.py b/code/algorithms/search/binary.py index c6762ab..a6aa89a 100644 --- a/code/algorithms/search/binary.py +++ b/code/algorithms/search/binary.py @@ -26,8 +26,11 @@ def preChecks(L: List[int], **_): 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.'; + if x in L: + assert index >= 0, 'Der Algorithmus sollte nicht -1 zurückgeben.'; + assert L[index] == x, 'Der Algorithmus hat den falschen Index bestimmt.'; + else: + assert index == -1, 'Der Algorithmus sollte -1 zurückgeben.'; return; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/code/algorithms/search/interpol.py b/code/algorithms/search/interpol.py index b0efc2e..8419c10 100644 --- a/code/algorithms/search/interpol.py +++ b/code/algorithms/search/interpol.py @@ -25,16 +25,19 @@ 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.'; +def postChecks(L: List[int], x: int, index: int, **_): + if x in L: + assert index >= 0, 'Der Algorithmus sollte nicht -1 zurückgeben.'; + assert L[index] == x, 'Der Algorithmus hat den falschen Index bestimmt.'; + else: + assert index == -1, 'Der Algorithmus sollte -1 zurückgeben.'; return; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ALGORITHM interpolation # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -@algorithmInfos(name='Interpolationssuchalgorithmus', outputnames='p', 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. diff --git a/code/algorithms/search/ith_smallest.py b/code/algorithms/search/ith_smallest.py index fe6c4c2..a3e46af 100644 --- a/code/algorithms/search/ith_smallest.py +++ b/code/algorithms/search/ith_smallest.py @@ -28,8 +28,8 @@ def preChecks(L: List[int], i: int, **_): return; def postChecks(L: List[int], i: int, value: int, **_): - L_ = sorted(L); - assert L_[i-1] == value, 'Der Algorithmus hat versagt.'; + L = sorted(L); + assert L[i-1] == value, 'Der Algorithmus hat versagt.'; return; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/code/algorithms/search/jump.py b/code/algorithms/search/jump.py index a579a90..ee9f282 100644 --- a/code/algorithms/search/jump.py +++ b/code/algorithms/search/jump.py @@ -28,8 +28,11 @@ def preChecks(L: List[int], **_): 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.'; + if x in L: + assert index >= 0, 'Der Algorithmus sollte nicht -1 zurückgeben.'; + assert L[index] == x, 'Der Algorithmus hat den falschen Index bestimmt.'; + else: + assert index == -1, 'Der Algorithmus sollte -1 zurückgeben.'; return; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/code/algorithms/search/sequential.py b/code/algorithms/search/sequential.py index aee6851..50efbf0 100644 --- a/code/algorithms/search/sequential.py +++ b/code/algorithms/search/sequential.py @@ -26,8 +26,11 @@ def preChecks(L: List[int], **_): 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.'; + if x in L: + assert index >= 0, 'Der Algorithmus sollte nicht -1 zurückgeben.'; + assert L[index] == x, 'Der Algorithmus hat den falschen Index bestimmt.'; + else: + assert index == -1, 'Der Algorithmus sollte -1 zurückgeben.'; return; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~