Sunday, 16 March 2014

Week 9 Post - Regexes and Assignment Two

Working on the second part of Assignment Two is frustrating.

For all four functions, coding for regular expressions of length 1 or length 2 is extremely easy to do. They don't have any permutations, they can only be one specific thing to be a valid regex, they're easy to match with strings, and building a tree from them is so easy it's silly.

Unfortunately, dealing with bar trees and dot trees is much more difficult. And, of course, it's recursive! Yay! It's not even understanding the recursion that gets me, though, because I can understand why it would be recursive and how the function needs to go over each child in the regex. It's structuring the code to make it recursive that gets me.

For example, part of the permutations function requires swapping values. This can be done by using:
j = s[i]
s[i] == s[i + 2]
s[i + 2] == j
Which is totally fine for regexes like '(1|0)' or '(0.e)', but then if you have a regex like '(1*|2*)' it doesn't really work because the indexes get all funny. Or maybe it's not even about indexing. Maybe if is_regex returns True for the child, say, 1*, then the new string becomes 1* and that is swapped with 2*. The base would be that if len(s) == 1 or 2 then it doesn't swap itself in any way.

Huh.

Another thing that I think would really, really, really help me for a lot of these functions is having a way to find the root of a regex. With a regex like '(((0|1)|(0.1)).(1*))' I can tell that the highest root is the '.' beside the (1*) child. How to tell my code this is something I'm unsure of. One way would be to use the brackets as an indicator, since the dot is only contained within the outermost set of brackets.

Anyway, I'm going to be spending the next few days at the CSC Help Centre and office hours. Fun all around.

Saturday, 8 March 2014

I've got the Exercsie 3 blues...

Exercise 3 Part A is understandable. I get what I'm supposed to do, which essential is the reverse of inorder and preorder. Unfortunately, I am at a road block with how to go about forming a tree.

It is obvious that the first piece of information to gather about the tree is the root. This can be done by getting the item at index 0 of preorder. So that should be the first element of the tree list. Next, you can find out which items are in the left subtree and which are in the right by finding that root's position in the inorder list.

For example, given preorder [10, 6, 8, 12, 15, 11] and inorder [8, 6, 12, 10, 11, 15] I can see that the root is 10 thanks to preorder. In inorder, it is clear that the left tree has six as the root with 8 and 12 as children, and the right tree has 15 as root with 11 as child. Thus, the resulting Tree List would be:

[10, [6, [8, None, None], [12, None, None]], [15, [11, None, None], [None, None, None]]]

The order of numbers in this tree list is exactly the same order as preorder gives them in. What is challenging is putting these numbers in tree list format, because a tree list's 2nd and 3rd elements are tree lists themselves unless they are none. What about a tree list with tons of children on one side? Something like [3, 8, 6, 12, 4, 10, 11, 15]? I am unsure of how to put that into a tree list format.

I feel like this exercise is going to take me as much time as the 2nd assignment, which is saddening.