-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathst.py
52 lines (37 loc) · 1.07 KB
/
st.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
"""
Execution: python st.py < input.txt
Data files: https://algs4.cs.princeton.edu/35applications/tinyST.txt
Sorted symbol table implementation using a python collections.OrderedDict
Does not allow duplicates.
"""
from collections import OrderedDict
class ST:
def __init__(self):
self.st = OrderedDict()
def put(self, key, value):
self.st[key] = value
def get(self, key):
if key is None:
raise ValueError("calls get() with null key")
return self.st.get(key)
def delete(self, key):
if key is None:
raise ValueError("calls get() with null key")
del self.st[key]
def contains(self, key):
return key in self.st
def is_empty(self):
self.size() == 0
def size(self):
return len(self.st.keys())
def keys(self):
return self.st.keys()
if __name__ == "__main__":
import sys
st = ST()
i = 0
for line in sys.stdin:
st.put(line, i)
i += 1
for key in st.keys():
print("%s : %s " % (key, st.get(key)))