Wednesday, 12 March 2014

Brian's CSC148 SLOG # 9

Hello and welcome to my 9th SLOG post!

I hope you all are doing well, this week has been havoc for me! I caught the flu, there was a snowstorm out this morning so imagine having a headache, sore throat, cough and runny nose while traversing through a flurry of snow blowing into your face. Nevertheless, I am still alive after gruesomely long commutes to and from school :P.

This week's topic will be about Assignment 2 Part 1 and a little rare surprise I had in my 8th CSC148 LAB.

Assignment 2 Part 1

Part 1 of Assignment 2 was a really unique opportunity for us students to design a RegexTree and represent it the way we wanted to. At first I thought there had to be only 1 absolute way to approach the task but we were actually given nearly boundless freedom to create the program. The only restriction that I am aware of is that we had to use inheritance and have a class hierarchy for our classes. Thus we couldn't create 1 single class to handle all the nodes.

So what knowledge was gained from completing this assignment?

Here is what I think:
  • Class hierarchy
  • Inheritance:
  • Reduction of duplicate code
  • An introduction to regular expressions used in various programming languages and utilities.

All of the above points work in tandem. Class hierarchy meant using inheritance and when calling functions from the classes, namespace would determine which function takes precedence over duplicate function names. For example: If I have a __repr__ method in my super class that calls on the __repr__ method of my Node classes, the __repr__ method of the Node classes will take precedence over the super class's __repr__ method even if we call it using the a RegexTree object.

Secondly, we can also easily reduce code by using inheritance and class heirarchy. I have actually completed two different designs of Assignment 2 Part 1 and have reviewed them with Professors Danny and Dustin. In one of my designs, I created an individual __repr__ method for every class which repeated code but on the other hand, showed how class hierarchy works. In the second design, I created a __repr__ class in the superclass RegexTree which would properly present a representation of all the different types of Regexes and since I have only 1 __repr__ method, there is less repetition of code in the program.

Another way to reduce code is in the __eq__ method. By writing a single __eq__ method in the superclass, we have reduced redundant code by rewriting one for every class. This method in particular could have been approached two ways. The first method is to simply compare the __repr__() methods of two different RegexTrees which meant that they had to have the exact same string to resolve to True.
The second method, which I think we could learn more from uses recursion. By simply checking if the symbol and children of a regex expression was equivalent, we could easily recurse over the children until both regular expressions had no children left and finally, return our desired bool.

Overall, Assignment 2 Part 1 was definitely a fun assignment to tackle given the freedom to design!

Lab 8

In this week's lab I had a rare opportunity to partner up with another friend of mine. He is a really crafty guy when it comes to programming and has knowledge of Java and uses comprehensions with ease.

His SLOG is here for those who are interested:
www.yanqilee.blogspot.ca

What made this an interesting experience is because we managed to solve the very last lab question in an interesting way. Apparently we were hinted to use the Stack class to solve BST's count_less method without using recursion. We struggled in trying to solve it using Stack but couldn't find an answer.
HOWEVER! We found a backdoor trick to solving it without using the Stack class at all!

The answer? BST's __str__ method. 

Our task was to count all the integers in a binary search tree lower than a given number for count_less method so we utilized __str__ instead. BST's __str__ method returned a representation of all the integers in a string that looked like a binary search tree. We simply split the string and removed all the newline characters. Afterwards, we looped over the integers and if it was lower than the number argument for count_less, we simply added one to count and returned count after the loop iteration.

Even the TA had a good laugh at how we accomplished the task!

That's all for this week so please check back soon for another SLOG installment!

All the best,
Brian

No comments:

Post a Comment