-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest_TST.py
executable file
·77 lines (66 loc) · 1.62 KB
/
test_TST.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
#!/usr/bin/env python
#*****************************************************************************
# Compilation: javac TST.java
# Execution: java TST < words.txt
# Dependencies: StdIn.java
#
# Symbol table with string keys, implemented using a ternary search
# trie (TST).
#
#
# % java TST < shellsST.txt
# keys(""):
# by 4
# sea 6
# sells 1
# she 0
# shells 3
# shore 7
# the 5
#
# longestPrefixOf("shellsort"):
# shells
#
# keysWithPrefix("shor"):
# shore
#
# keysThatMatch(".he.l."):
# shells
#
# % java TST
# theory the now is the time for all good men
#
# Remarks
# --------
# - can't use a key that is the empty string ""
#
#*****************************************************************************/
import sys
from AlgsSedgewickWayne.TST import TST
from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array
def main(fin, prt=sys.stdout):
"""Unit tests the TST data type."""
# build symbol table from standard input
arr = cli_get_array(fin)
print(arr)
st = TST()
for i, key in enumerate(arr):
st.put(key, i)
# print results
if st.size() < 100:
prt.write('keys(""):')
for key in st.keys():
prt.write("{} {}".format(key, st.get(key)))
prt.write("\n")
prt.write('longestPrefixOf("shellsort"):')
prt.write(st.longestPrefixOf("shellsort"))
prt.write("\n")
prt.write('keysWithPrefix("shor"):')
for s in st.keysWithPrefix("shor"):
prt.write(s)
prt.write("\n")
prt.write('keysThatMatch(".he.l."):')
for s in st.keysThatMatch(".he.l."):
prt.write(s)
if __name__ == '__main__':
main("../thirdparty/shellsST.txt")