-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuchen_beispiele.py
189 lines (139 loc) · 4.27 KB
/
suchen_beispiele.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Folie 6
def linear_search(data, elem):
for i in range(len(data)):
if elem == data[i]:
return True
return False
def linear_search_2(data, elem):
for i in data:
if elem == i:
return True
return False
print(linear_search([3, 2, 1, 4, 2, 5, 3, 8], 8))
print(linear_search([3, 2, 1, 4, 2, 5, 3, 8], 7))
print(linear_search_2([3, 2, 1, 4, 2, 5, 3, 8], 8))
print(linear_search_2([3, 2, 1, 4, 2, 5, 3, 8], 7))
# Folie 9
def linear_search(data, elem):
for i in range(len(data)):
if elem == data[i]:
return i
return -1
def linear_search_2(data, elem):
for i, item in enumerate(data):
if elem == item:
return i
return -1
print(linear_search([3, 2, 1, 4, 2, 5, 3, 8], 8))
print(linear_search([3, 2, 1, 4, 2, 5, 3, 8], 7))
print(linear_search_2([3, 2, 1, 4, 2, 5, 3, 8], 8))
print(linear_search_2([3, 2, 1, 4, 2, 5, 3, 8], 7))
# Folie 10
def linear_search(data, elem):
for i in data:
if i == elem:
return True
if i > elem:
return False
return False
print(linear_search([1, 2, 3, 4, 5, 6, 7, 8], 5))
print(linear_search([1, 2, 3, 4, 5, 6, 7, 8], 0))
print(linear_search([1, 2, 3, 4, 5, 6, 7, 8], 9))
# Folie 15
def binary_search(data, elem):
low = 0
high = len(data) - 1
while low <= high:
mid = (low + high) // 2
if elem == data[mid]:
return True
elif elem < data[mid]:
high = mid - 1
else:
low = mid + 1
return False
vals = [1, 4, 6, 7, 9, 11, 12, 13, 15, 17, 18, 20, 22, 24, 25]
print(binary_search(vals, 9))
print(binary_search(vals, 1))
print(binary_search(vals, 25))
print(binary_search(vals, 2))
print(binary_search(vals, 30))
# Folie 21
def binary_search(data, elem):
def binary_search_helper(data, elem, low, high):
if low > high:
return False
else:
mid = (low + high) // 2
if elem == data[mid]:
return True
elif elem < data[mid]:
return binary_search_helper(data, elem, low, mid - 1)
else:
return binary_search_helper(data, elem, mid + 1, high)
return binary_search_helper(data, elem, 0, len(data) - 1)
vals = [1, 4, 6, 7, 9, 11, 12, 13, 15, 17, 18, 20, 22, 24, 25]
print(binary_search(vals, 9))
print(binary_search(vals, 1))
print(binary_search(vals, 25))
print(binary_search(vals, 2))
print(binary_search(vals, 30))
# Folie 33, 34
class IntTable:
def __init__(self, num_buckets):
self.buckets = []
self.num_buckets = num_buckets
for i in range(num_buckets):
self.buckets.append([])
def add_entry(self, key):
hash_bucket = self.buckets[key % self.num_buckets]
if key not in hash_bucket:
hash_bucket.append(key)
def __contains__(self, key):
hash_bucket = self.buckets[key % self.num_buckets]
return key in hash_bucket
def __str__(self):
return "\n".join([str(b) for b in self.buckets])
import random
d = IntTable(13)
for i in range(20):
k = random.choice(range(10 ** 4))
d.add_entry(k)
d.add_entry(12)
d.add_entry(12)
print(12 in d)
print(123456 in d)
print('Content of the table:')
print(d)
# Folie 35, 36
class IntTable:
def __init__(self, num_buckets):
self.buckets = []
self.num_buckets = num_buckets
for i in range(num_buckets):
self.buckets.append([])
def add_entry(self, key, val):
hash_bucket = self.buckets[key % self.num_buckets]
for i in range(len(hash_bucket)):
if hash_bucket[i][0] == key:
hash_bucket[i] = (key, val)
return
hash_bucket.append((key, val))
def get_val(self, key):
hash_bucket = self.buckets[key % self.num_buckets]
for e in hash_bucket:
if e[0] == key:
return e[1]
return None
def __str__(self):
return "{" + ", ".join([":".join(map(str, e)) for b in self.buckets for e in b]) + "}"
import random
d = IntTable(13)
for i in range(20):
k = random.choice(range(10 ** 4))
d.add_entry(k, i)
print('The value of the intDict is:')
print(d)
print('\n', 'The buckets are:')
for hash_bucket in d.buckets:
print(' ', hash_bucket)