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.
No comments:
Post a Comment