-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr-10.21.py
76 lines (60 loc) · 2.11 KB
/
r-10.21.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
"""
Consider the following variant of the _find_index method from Code Fragment 10.8 in the context of
the SortedTableMap class:
def _find_index(self, key, low, high):
if high < low:
return high + 1
else:
mid = (high + low) // 2
if self.table[mid].key < key:
return self._find_index(key, mid + 1, high)
else:
return self._find_index(key, low, mid - 1)
"""
class SortedTableMap():
"""
partial implementation of the SortedTableMap
"""
def __init__(self, *args, **kwargs):
self.table = []
class Item(object):
def __init__(self, key, value):
self.key = key
self.value = value
def _find_index(self, key, low, high):
"""
original find index method
"""
if high < low:
return high + 1
else:
mid = (high + low) // 2
if self.table[mid].key == key:
return mid
elif self.table[mid].key < key:
return self._find_index(key, mid + 1, high)
else:
return self._find_index(key, low, mid - 1)
def _find_index_2(self, key, low, high):
"""
modified method
"""
if high < low:
return high + 1
else:
mid = (high + low) // 2
# if self.table[mid].key == key:
# return mid
if self.table[mid].key < key:
return self._find_index_2(key, mid + 1, high)
else:
return self._find_index_2(key, low, mid - 1)
if __name__ == "__main__":
map = SortedTableMap()
vals = [5, 6, 7, 8, 11, 13, 15, 19, 21, 23]
for i in range(0, len(vals)):
map.table.append(SortedTableMap.Item(vals[i], str(vals[i])))
for val in vals:
assert map._find_index(val, 0, len(map.table)-1) == map._find_index_2(val, 0, len(map.table)-1)
# answer yes it does, you can even return low when high < low, but high + 1 makes more sense,
# if given key is not in the list, that will be the next available position