diff options
| author | akiyamn | 2021-04-27 15:07:27 +1000 |
|---|---|---|
| committer | akiyamn | 2021-04-27 15:07:27 +1000 |
| commit | 3ec37656910e3c71569408bd9be2dfa8db06efc2 (patch) | |
| tree | 832fc57db4e5cf61eabfa7efe30b865e74a3d62c | |
| parent | 1695a8e7c9e0d345918946ef6fbc8f56c7751e32 (diff) | |
| download | fit3155-3ec37656910e3c71569408bd9be2dfa8db06efc2.tar.gz fit3155-3ec37656910e3c71569408bd9be2dfa8db06efc2.zip | |
Ass 2: Task 2 pretty much done
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | ass2/q2/suffix_array.py | 44 |
2 files changed, 19 insertions, 27 deletions
@@ -1,4 +1,4 @@ -output_kruskals.txt +*.txt .idea # ---> Python diff --git a/ass2/q2/suffix_array.py b/ass2/q2/suffix_array.py index 1c67433..e743180 100644 --- a/ass2/q2/suffix_array.py +++ b/ass2/q2/suffix_array.py @@ -1,15 +1,5 @@ from ass2.ukkonen import Node, ukkonen - - -def rank(char): - return ord("a") - ord(char) if char != "$" else 26 - - -# tree = ukkonen("abacabad") -# # tree.print_tree() -# for node in Node.all_nodes: -# if not node.children: -# print(node) +import sys def depth_first_apply(node: Node, func): @@ -20,21 +10,23 @@ def depth_first_apply(node: Node, func): depth_first_apply(child, func) -# def calc_suffix_index(): -# lookup = {} -# counter = 0 -# # leaves = filter(lambda n: not n.children, Node.all_nodes) -# for node in Node.all_nodes: -# if not node.children: -# lookup -# -# print(list(leaves)) +def read_in_string(filename): + with open(filename, "r") as file: + return file.read() + + +def write_suffix_array(tree, filename): + with open(filename, "w") as file: + buffer = [] + depth_first_apply(tree, buffer.append) + file.write("\n".join(map(str, buffer))) + +def main(): + assert len(sys.argv) == 2 + string = read_in_string(sys.argv[1]) + tree = ukkonen(string) + write_suffix_array(tree, "output_suffix_array.txt") -def accum(): - array = [] - depth_first_apply(tree, lambda x: array.append(x)) - return array -tree = ukkonen("mississippi") -print(accum()) +main() |
