forked from noyoshi/LoLBurst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
141 lines (121 loc) · 3.77 KB
/
base.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
#!/usr/bin/env python3
import json
import pprint
import time
def initialize_champs():
with open("champion.json") as f:
champdata = json.load(f)
champnames = {}
for thing in champdata["data"].keys():
champnames[champdata["data"][thing]["name"]] = thing
return champnames
def initialize_items():
with open('item.json') as f:
itemdata = json.load(f)
itemnames = {}
for item in itemdata['data']:
itemnames[itemdata['data'][item]['name']] = item
return itemnames
class Base:
def __init__(self):
self.data = None
def insert(self, data):
pass
def search(self, key):
start = time.time()
if (key):
results = []
key = key.lower()
for elm in self.data:
if elm.lower().startswith(key):
results.append(elm)
end = time.time()
print("Search: ", end - start)
return results
else:
end = time.time()
print("Search: ", end - start)
return []
def p(self):
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(', '.join(self.data))
class UnsortedList(Base):
def __init__(self):
self.data = []
def insert(self, data):
start = time.time()
for elm in data:
self.data.append(elm)
end = time.time()
print("Insert: ", end-start)
class SortedList(Base):
def __init__(self):
self.data = []
def insert(self, data):
start = time.time()
for elm in data:
self.data.append(elm)
self.data.sort()
end = time.time()
print("Insert: ",end-start)
def search(self, key):
start = time.time()
if key:
results = []
key = key.lower()
found = False
for index, elm in enumerate(self.data):
if elm.lower().startswith(key.lower()):
found = True
results.append(elm)
elif found:
end = time.time()
print("Search: ", end-start)
return results
end = time.time()
print("Search: ", end-start)
return results
else:
end = time.time()
print("Search: ", end-start)
return []
class Trie(Base):
def __init__(self):
self.root = {}
def insert(self, data):
start = time.time()
for elm in data:
currlist = self.root
for char in elm:
if char.lower() in currlist.keys():
currlist = currlist[char.lower()]
else:
currlist[char.lower()] = {}
currlist = currlist[char.lower()]
currlist[0] = elm
end = time.time()
print("Insert: ",end-start)
def search(self,data):
start = time.time()
currlist = self.root
for char in data:
if char.lower() in currlist.keys():
currlist = currlist[char.lower()]
else:
end = time.time()
print("Search: ", end-start)
return []
x = self.print_dictionary(currlist)
end = time.time()
print("Search: ", end-start)
return x
def print_dictionary(self, currlist):
x = []
if 0 in currlist.keys():
x.append(currlist[0])
for key in currlist.keys():
if key != 0:
x += self.print_dictionary(currlist[key])
return x
if __name__ == '__main__':
initialize_champs()