Blog Entry#6: Assignment 2
Nov 13
Hey Everyone, how was fall break? And more important how was term test?
Personally I felt a bit relief because I started to review last night and today's test was not as hard as I imaged xD.
@Rahul Chaudhary (http://rhlcy.blogspot.ca) also mentioned in his blog that the term test went well because he followed Danny's advice and reviewed all labs and exercises. I guess because I put a lot of efforts in all my exercises and labs, and the hardwork paid off :)
In this week's SLOG, I will share some of my opinions on the assignment 2 which was due last week. Feel free to leave any comment and I will be excited to read them!
This assignment consists two major tasks, and the first one is to take in a regular expression (a string) and represent it as a tree (object). Leaf nodes are represented by RegexTreeNode() '0' or '1' or 'e', and internal nodes are represented by subclasses of RegexTreeNode() such as StarNode() '*', BarNode() '|', and DotNode() '.'.
In the __init__() method, I first store every character in the given regular expression in a list.
self.regex = [i for i in regex]
And then I find all leaf nodes '0', '1', 'e', and stored them as RegexTreeNode.
for n in self.regex:
if n == '0' or n == '1' or n == 'e':
self.regex[self.regex.index(n)] = RegexTreeNode(n, [])
Now here's the hardest part (I think): when the size of my list is one, I don't have to do anything (e.g. [ RegexTreeNode('0') ]); when the size of my list is two, I must have a StarNode (e.g. [ RegexTreeNode('0'), '*' ]); when the size of my list is greater two, I must have a complicated string that involves parenthesis ([ '(', RegexTreeNode('1'), '|', RegexTreeNode('0'), ')' ]). The first two cases are simple to resolve, so I will elaborate on my solution for the third case. I start by looking for the first right parenthesis, and its corresponding left parenthesis by reversing the sublist before the first right parenthesis and find the first left parenthesis. Given the two indexes for the parenthesis, I can build a tree from the content inside, and replace the parenthesis and its content with this tree. and of course I have to recursively call the function to solve the rest.
if len(reg) > 2:
right = reg.index(')')
left = right - reg[:right][::-1].index('(') - 1
temp = reg[left + 1:right]
reg = reg[:left] + reg[right + 1:]
reg.insert(left, create_tree(temp))
built_tree(reg)
create_tree is a helper function that I use to construct the tree from the content inside the parenthesis. There are 2 case, either the content has 3 characters (one node and two children), or it has 4 characters when a starnode is involved.
if '*' in temp:
star_ind = temp.index('*')
star = StarNode(temp[star_ind - 1])
temp = temp[:star_ind - 1] + temp[star_ind + 1:]
temp.insert(star_ind - 1, star)
return create_tree(temp)
elif '|' in temp:
return BarNode(temp[0], temp[-1])
elif '.' in temp:
return DotNode(temp[0], temp[-1])
else:
return temp
So up to this point, the first part of this assignment is completed. I think my solution might not be the most simple way of solving this problem, but at least the logic is pretty sound. Please comment below if you have any question or opinion on this topic.