aboutsummaryrefslogtreecommitdiff
path: root/ass2/q2
diff options
context:
space:
mode:
Diffstat (limited to 'ass2/q2')
-rw-r--r--ass2/q2/suffix_array.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/ass2/q2/suffix_array.py b/ass2/q2/suffix_array.py
index edfaa80..78cf6c4 100644
--- a/ass2/q2/suffix_array.py
+++ b/ass2/q2/suffix_array.py
@@ -3,6 +3,9 @@ import sys
def depth_first_apply(node: Node, func):
+ """
+ Apply a given function to every leaf node in the suffix tree via DFS
+ """
if not node.children:
func(node.suffix_index)
else:
@@ -11,11 +14,17 @@ def depth_first_apply(node: Node, func):
def read_in_string(filename):
+ """
+ Reads in a string from a given file name
+ """
with open(filename, "r") as file:
return file.read()
def suffix_array(string):
+ """
+ Generates a suffix array from a given string using Ukkonen's and a depth first search
+ """
tree = ukkonen(string)
buffer = []
depth_first_apply(tree, buffer.append)
@@ -23,6 +32,9 @@ def suffix_array(string):
def write_suffix_array(string, filename):
+ """
+ Writes a suffix array for a given string to a given file name.
+ """
with open(filename, "w") as file:
buffer = suffix_array(string)
file.write("\n".join(map(str, buffer)))