aboutsummaryrefslogtreecommitdiff
path: root/ass2/q2/suffix_array.py
blob: e7431806181aeb050a92b7df1e4acdc6e00a30b8 (plain)
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
from ass2.ukkonen import Node, ukkonen
import sys


def depth_first_apply(node: Node, func):
    if not node.children:
        func(node.suffix_index)
    else:
        for child in node.children.ordered_items():
            depth_first_apply(child, func)


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")


main()