aboutsummaryrefslogtreecommitdiff
path: root/ass2/q2/suffix_array.py
diff options
context:
space:
mode:
Diffstat (limited to 'ass2/q2/suffix_array.py')
-rw-r--r--ass2/q2/suffix_array.py44
1 files changed, 18 insertions, 26 deletions
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()