From 2cdfd5c56f32fafb86be28d12976951664073e45 Mon Sep 17 00:00:00 2001 From: shellfly Date: Fri, 31 Jan 2020 14:25:49 +0800 Subject: [PATCH] add comment for sequential search st --- README.md | 2 +- algs4/separate_chaining_hash_st.py | 3 ++- algs4/sequential_search_st.py | 39 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 93683b0..968126b 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Try to keep the interface and variable name consistent with the original book wh * 3 SEARCHING * [FrequencyCounter](algs4/frequency_counter.py) - * [SequentialSearchST](algs4/sequential_search.py) + * [SequentialSearchST](algs4/sequential_search_st.py) * [BinarySearchST](algs4/binary_search_st.py) * [BST](algs4/bst.py) * [RedBlackBST](algs4/red_black_bst.py) diff --git a/algs4/separate_chaining_hash_st.py b/algs4/separate_chaining_hash_st.py index 2ed498e..95758bd 100644 --- a/algs4/separate_chaining_hash_st.py +++ b/algs4/separate_chaining_hash_st.py @@ -74,11 +74,12 @@ def resize(self, chains): tmp = SeparateChainingHashST(chains) for i in range(self.m): for key in self.st[i].keys(): - tmp.put(key, st[i].get(key)) + tmp.put(key, self.st[i].get(key)) self.m = tmp.m self.n = tmp.n self.st = tmp.st + if __name__ == '__main__': import sys diff --git a/algs4/sequential_search_st.py b/algs4/sequential_search_st.py index d4d0a71..1e89486 100644 --- a/algs4/sequential_search_st.py +++ b/algs4/sequential_search_st.py @@ -1,3 +1,28 @@ +""" + * Execution: python3 sequential_search_st.py + * Data files: https://algs4.cs.princeton.edu/31elementary/tinyST.txt + * + * Symbol table implementation with sequential search in an + * unordered linked list of key-value pairs. + * + * % more tinyST.txt + * S E A R C H E X A M P L E + * + * % python3 sequential_search_st.py < tinyST.txt + * L 11 + * P 10 + * M 9 + * X 7 + * H 5 + * C 4 + * R 3 + * A 8 + * E 12 + * S 0 + * + """ + + from algs4.utils.st import Node, STKeyIterator @@ -51,3 +76,17 @@ def keys(self): def is_empty(self): return self.size == 0 + + +if __name__ == '__main__': + import sys + + st = SequentialSearchST() + i = 0 + for line in sys.stdin: + for key in line.split(): + st.put(key, i) + i += 1 + + for key in st.keys(): + print(key + " " + str(st.get(key)))