-
Notifications
You must be signed in to change notification settings - Fork 69
/
bonusvraag_3_recursive_binary_search_student.py
78 lines (57 loc) · 2.24 KB
/
bonusvraag_3_recursive_binary_search_student.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Oriëntatie op AI
Bonusvraag: binary search recursief
(c) 2019 Hogeschool Utrecht
Tijmen Muller (tijmen.muller@hu.nl)
Opdracht: werk onderstaande functie uit.
Je kunt je functies testen met het gegeven raamwerk door het bestand
uit te voeren (of met behulp van pytest, als je weet hoe dat werkt).
Lever je werk in op Canvas als alle tests slagen.
Let op! Het is niet toegestaan om bestaande modules te importeren en te
gebruiken, zoals `math` en `statistics`.
"""
def recursive_binary_search(lst, target):
"""
Zoek een element in de gegeven lijst door middel van recursief binair zoeken.
Je mag ervan uit gaan dat de gegeven lijst al gesorteerd is.
Zorg dat de inhoud van de gegeven lijst niet verandert.
Args:
lst (list): Een lijst met elementen van gelijk type, bijvoorbeeld gehele getallen.
target (int): Het gezochte element.
Returns:
bool: Of het element in de lijst voorkomt.
"""
return False
"""
==========================[ HU TESTRAAMWERK ]================================
Onderstaand staan de tests voor je code -- hieronder mag je niets wijzigen!
Je kunt je code testen door deze file te runnen of met behulp van pytest.
"""
import random
def test_recursive_binary_search():
for _ in range(10):
lst_test = sorted(random.sample(range(20), 6))
target = random.randrange(20)
found = target in lst_test
lst_copy = lst_test.copy()
outcome = recursive_binary_search(lst_test, target)
assert outcome == found, \
f"Fout: recursive_binary_search({lst_test}, {target}) geeft {outcome} in plaats van {found}"
assert lst_copy == lst_test, "Fout: recursive_binary_search(lst, target) verandert de inhoud van lijst lst"
def __main():
try:
print("\x1b[32m")
test_recursive_binary_search()
print("Je functie recursive_binary_search werkt goed!\n")
except AssertionError as ae:
print("\x1b[31m") # Rode tekstkleur
if not ae:
print("Je code veroorzaakt onderstaande AssertionError:")
raise ae
else:
print(ae)
print("\x1b[0m") # Reset tekstkleur
if __name__ == '__main__':
__main()