As for reviewing concepts which were bugging me, I looked over another student's blog, with a very good explanation of binary tree traversal for all neophytes, and a nice narrative piece about coding for a tree class which also helped with my comprehension of the material.
After the very melodramatic piece for last week, I have decided to write something a bit more relevant to course material, regarding assignment 2 (which I did NOT rip off from brekeycolortran: I started this draft at the beginning of week 8).
Assessing assignment 2 left me quite addled. Upon initial perusal, it seemed to me that the class constructors would take regex strings and make a literal copy in an internal attribute. Not the case at all.
Looking over the discussion board quickly highlighted certain things: a node and reference model was optimal, and each operator or regex 'type' (as implied in the write up) required its own subclass.
Initially, I created a garbage code with no useful inheritance:
I tried to create a more useful superclass by taking the parameters of (self=Regex, operator=None, left_child=None, right_child=None), but again, this wouldn't work with the regex of size one, which would then have to choose between giving a value to the right_child, left_child, or even operator for its root.class Regex:'''creates a regex'''#need some way to check legal input '012e' or proper regexdef __init__(self: 'Regex', regex) -> None:self._regex = regexdef __repr__(self: 'Regex') -> str:return 'Regex(' + repr(self._regex) + ')'def __eq__(self: 'Regex', other: 'Regex') -> bool:return self._regex == other._regexclass RegexOne(Regex):'''creates a regex tree of length one'''def __init__(self: 'RegexOne', regex) -> None:self._root = regexdef __repr__(self: 'RegexOne') -> str:'''returns a constructor form of the object'''return 'RegexOne(' + repr(self._root) + ')'def __eq__(self: 'RegexOne', other: str) -> bool:'''return True iff each node in self is equal to each node in other'''return other._root == self._rootclass RegexStar(Regex):def __init__(self: 'RegexStar', only_child ) -> None:'''creates a regex with root *'''self._root = '*'self._only_child = only_childdef __repr__(self: 'RegexStar') -> str:return 'RegexStar(' + repr(self._only_child) + ')'def __eq__(self: 'RegexStar', other) -> bool:return (other._root == self._root and \self._only_child == other._only_child)class RegexBarDot(Regex):def __init__(self: 'RegexBarDot', operator, left_child, right_child) -> None:'''creates a regex with root | or . '''if operator != '|' and operator != '.':raise Exception('Choose root | or .')self._root = operatorself._left_child = left_childself._right_child = right_childdef __repr__(self: "RegexBarDot") -> str:return 'RegexBarDot(' + repr(self._root) + ',' + repr(self._left_child) + ',' + repr(self._right_child)\+ ')'def __eq__(self, other):return other._root == self._root and other._left_child == self._left_child \and other._right_child == self._right_childclass RegexBar(RegexBarDot):def __init__(self:'RegexBar', left_child, right_child) -> None:#why must these variables be instantiated again in the subclassself._root = '|'self._left_child = left_childself._right_child = right_childclass RegexDot(RegexBarDot):def __init__(self: 'RegexDot', left_child, right_child) -> None:self._root = '.'self._left_child = left_childself._right_child = right_child
Looking over Dan's code (after week8) made me look at the relationships between the classes in a different way: Binary Tree inherited from Regex Tree, and even used a Regex Tree type as a parameter for its constructor. As well, we were allowed to use some helped functions, which I failed to notice. Another important thing I missed was the distinction of Unary and Binary Trees within the code, which seems obvious now.
As a side node, The Shattered Medallion comes out May 20th. SQL.