Sunday, March 23, 2014

Week 10 – Assignment 2 Part 2

For the second part of Assignment 2, we had to implement four functions that performed operations on regex tree strings, ternary strings, or RegexTree objects.

I think the hardest part by far was finding the outermost root of binary trees for is_regex and build_regex_tree. When I first wrote it, I'd tried to find the root by using the built-in index function. My function involved finding the index of the rightmost ')' between the beginning of the string and r2's first '('. If r2 doesn't contain a '(', then just find the last ')' in the string. The index after that is the “root_index”. However, if there isn't any ')' in the string, then I just set the “root_index” as 1 as it should be right after the first item. Finally, I increased the index by 1 for every '*' in the string.


This was not one of the methods that the professor hinted to us in class, but I came to this conclusion on my own and it worked...although it's admittedly a very messy solution. In the end, my partner came up with a much better solution, where she simply found the indices of every '.' and '|' in the string. Then, if the difference between the '('s and ')'s before this index was equal to 1, then she knew that this was the outermost root index. The code for this method was, indeed, much shorter and more concise, so we went with this in the end. It was also one of the methods that the professor had suggested, but I hadn't understood it in class, so seeing my partner's code was very helpful to me.

No comments:

Post a Comment