-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankings.py
56 lines (48 loc) · 1.31 KB
/
rankings.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
#!/usr/bin/env python3
"""Specialized data structure to make sure the search runs in linear time"""
import types
class rankings():
"""Sorted fixed-size array"""
def __init__(self, size):
self._len = size
self._lst = list()
def insert(self, item, value):
high = len(self._lst)
if (len(self._lst) != 0
and self._lst[0].value < value): #edge case
high = 0
low = 0
while high > low + 1:
guess = (int) ((high + low) / 2)
if self._lst[guess].value < value:
high = guess
else:
low = guess
self._lst.insert(high,
types.SimpleNamespace(
item=item,
value=value
)
)
if len(self._lst) > self._len:
del self._lst[-1]
def getList(self):
return list(x.item for x in self._lst)
if __name__ == "__main__":
from random import randint
r = rankings(10)
lst = list()
for i in range(30):
val = randint(1, 25)
lst.append(val)
print("unsorted list")
print(lst)
print()
print("rankings")
for x in lst:
r.insert(chr(x + ord('a')), x)
print(r.getList())
print()
print("sanity check")
lst.sort(reverse=True)
print(lst)