-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLinkedListSet.py
44 lines (32 loc) · 1009 Bytes
/
LinkedListSet.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
from base import SetBase
from LinkedList import LinkedList
class LinkedListSet(SetBase):
def __init__(self):
self._list = LinkedList()
def get_size(self):
return self._list.get_size()
def is_empty(self):
return self._list.is_empty()
def contains(self, e):
return self._list.contains(e)
def add(self, e):
if self.contains(e):
return
self._list.add_first(e)
def remove(self, e):
self._list.remove(e)
if __name__ == '__main__':
words = ''
with open('./shakespeare.txt', 'r') as f:
words = f.read()
words = words.split()
from time import time
start_time = time()
bst_set = LinkedListSet()
for word in words:
bst_set.add(word)
print('Total words: ', len(words))
print('Unique words: ', bst_set.get_size())
print('Contains word "they": ', bst_set.contains('they'))
## 耗时100秒左右
print('Total time: {} seconds'.format(time() - start_time))