Skip to content

Commit e025493

Browse files
committed
change prototype of binary_recursive function including parameters for low and high index
1 parent 1f2c5e7 commit e025493

3 files changed

Lines changed: 52 additions & 17 deletions

File tree

run_back/checks/__init__.py

Whitespace-only changes.

run_back/checks/main.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def has_function():
2929
def test_sample_1():
3030
"""sample test: finds 8 in list"""
3131
module = check50.py.import_(FILE_NAME)
32-
result = module.binary_recursive([6, 7, 8, 9, 10], 8)
32+
list = [6, 7, 8, 9, 10]
33+
result = module.binary_recursive(list, 8, 0, len(list) - 1)
3334
if result != 2:
3435
raise check50.Mismatch(2, result)
3536

@@ -38,7 +39,8 @@ def test_sample_1():
3839
def test_sample_2():
3940
"""sample test: finds 17 in list"""
4041
module = check50.py.import_(FILE_NAME)
41-
result = module.binary_recursive([11, 13, 15, 17, 18], 17)
42+
list = [11, 13, 15, 17, 18]
43+
result = module.binary_recursive(list, 17, 0, len(list) - 1)
4244
if result != 3:
4345
raise check50.Mismatch(3, result)
4446

@@ -47,7 +49,8 @@ def test_sample_2():
4749
def test_sample_3():
4850
"""sample test: returns -1 for missing element"""
4951
module = check50.py.import_(FILE_NAME)
50-
result = module.binary_recursive([3, 4, 5, 6, 7], 1)
52+
list = [3, 4, 5, 6, 7]
53+
result = module.binary_recursive(list, 1, 0, len(list) - 1)
5154
if result != -1:
5255
raise check50.Mismatch(-1, result)
5356

@@ -56,7 +59,8 @@ def test_sample_3():
5659
def test_sample_4():
5760
"""sample test: empty list returns -1"""
5861
module = check50.py.import_(FILE_NAME)
59-
result = module.binary_recursive([], 13)
62+
list = []
63+
result = module.binary_recursive(list, 13, 0, len(list) - 1)
6064
if result != -1:
6165
raise check50.Mismatch(-1, result)
6266

@@ -65,7 +69,8 @@ def test_sample_4():
6569
def test_first():
6670
"""finds first element"""
6771
module = check50.py.import_(FILE_NAME)
68-
result = module.binary_recursive([1, 2, 3], 1)
72+
list = [1, 2, 3]
73+
result = module.binary_recursive(list, 1, 0, len(list) - 1)
6974
if result != 0:
7075
raise check50.Mismatch(0, result)
7176

@@ -74,7 +79,8 @@ def test_first():
7479
def test_last():
7580
"""finds last element"""
7681
module = check50.py.import_(FILE_NAME)
77-
result = module.binary_recursive([10, 20, 30, 40, 50], 50)
82+
list = [10, 20, 30, 40, 50]
83+
result = module.binary_recursive(list, 50, 0, len(list) - 1)
7884
if result != 4:
7985
raise check50.Mismatch(4, result)
8086

@@ -84,7 +90,7 @@ def test_duplicates():
8490
"""handles duplicates correctly (returns any valid index)"""
8591
module = check50.py.import_(FILE_NAME)
8692
lst = [1, 2, 2, 2, 3, 4]
87-
result = module.binary_recursive(lst, 2)
93+
result = module.binary_recursive(lst, 2, 0, len(lst) - 1)
8894
if result not in (1, 2, 3):
8995
msg = f"Expected index 1, 2 or 3 but got {result}"
9096
raise check50.Failure(msg)
@@ -94,7 +100,8 @@ def test_duplicates():
94100
def test_negative_numbers():
95101
"""works with negative numbers"""
96102
module = check50.py.import_(FILE_NAME)
97-
result = module.binary_recursive([-10, -5, 0, 5, 10], -5)
103+
list = [-10, -5, 0, 5, 10]
104+
result = module.binary_recursive(list, -5, 0, len(list) - 1)
98105
if result != 1:
99106
raise check50.Mismatch(1, result)
100107

@@ -109,7 +116,7 @@ def test_random():
109116
lst = sorted(random.sample(range(0, 500), 30))
110117
key = random.choice(lst)
111118
expected = lst.index(key)
112-
result = module.binary_recursive(lst, key)
119+
result = module.binary_recursive(lst, key, 0, len(lst) - 1)
113120

114121
if result != expected:
115122
raise check50.Mismatch(expected, result)
@@ -131,7 +138,7 @@ def __getitem__(self, index):
131138
lst = WatchList(range(100_000))
132139
lst.accesses = 0
133140

134-
module.binary_recursive(lst, 99999)
141+
module.binary_recursive(lst, 99999, 0, len(lst) - 1)
135142

136143
if lst.accesses > 1000:
137144
msg = "Too many list accesses; algorithm may not be binary search"
@@ -155,7 +162,8 @@ def wrapper(*args, **kwargs):
155162
module.binary_recursive = wrapper
156163

157164
# Perform a search that requires recursion
158-
result = module.binary_recursive([1, 2, 3, 4, 5, 6, 7], 6)
165+
list = [1, 2, 3, 4, 5, 6, 7]
166+
result = module.binary_recursive(list, 6, 0, len(list) - 1)
159167

160168
# Restore original function (cleanliness, not required but good practice)
161169
module.binary_recursive = original

run_back/html/template.html

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@
88

99
<p>
1010
{% include "file.html" %}
11-
Implementieren Sie die folgende Funktion in Ihrer Datei:
11+
Implementieren Sie die folgende Funktion in Ihrer Datei. Die Überpüfung der Array Grenzen
12+
ist bereits erfolgt, vervollständigen Sie den Code für die rekursive Suche:
1213
<code>
13-
def binary_recursive(lst, key):
14+
def binary_recursive(lst, key, low, high):
15+
16+
if len(lst) == 0:
17+
# List is empty no search needed
18+
return -1
19+
20+
if low > len(lst)-1:
21+
if low != 0:
22+
print("Index out of range: low ", low)
23+
return -1
24+
25+
if high > len(lst)-1:
26+
print("Index out of range: high ", high)
27+
return -1
28+
29+
# TODO Implementieren Sie hier die rekursive binäre Suche
1430
</code>
1531
</p>
1632

@@ -57,10 +73,21 @@
5773
<li>
5874
Testen Sie die Funktion zum Beispiel mit:
5975
<pre><code>
60-
print(binary_recursive([6, 7, 8, 9, 10], 8)) # Erwartet: 2
61-
print(binary_recursive([11, 13, 15, 17, 18], 17)) # Erwartet: 3
62-
print(binary_recursive([3, 4, 5, 6, 7], 1)) # Erwartet: -1
63-
print(binary_recursive([], 13)) # Erwartet: -1
76+
list = [6, 7, 8, 9, 10]
77+
high_idx = len(list) - 1
78+
print(binary_recursive(list, 8, low = 0, high = high_idx)) # Erwartet: 2
79+
80+
list = [11, 13, 15, 17, 18]
81+
high_idx = len(list) - 1
82+
print(binary_recursive(list, 17, low = 0, high = high_idx)) # Erwartet: 3
83+
84+
list = [3, 4, 5, 6, 7]
85+
high_idx = len(list) - 1
86+
print(binary_recursive(list, 1, low = 0, high = high_idx)) # Erwartet: -1
87+
88+
list = []
89+
high_idx = len(list) - 1
90+
print(binary_recursive(list, 13, low = 0, high = high_idx)) # Erwartet: -1
6491
</code></pre>
6592
</li>
6693
</ul>

0 commit comments

Comments
 (0)