Thursday, 27 February 2014

SLOG #5: Recursion



   Recursion, a method used to define an object in terms of itself. It is done this way so you can achieve a certain objective once you use recursion for several times. For instance, we can find factorials such 5! recursively because
n! = n x (n - 1) x (n-2) x … x 2 x 1 = (n)[(n - 1)!]
where n is an integer and n >= 1. Since programming language generally support recursion, this means functions can be used to call themselves to solve problems where recursion is involved. Most of the times, coding recursion will require precision, since the answers to the problem depends on the solution to their smaller instance calls. 

   If we were to code factorials by means of recursion in python, we can return the product of the number multiply by the recursive call using the number that is 1 less than the current number. If the current number is 0 or 1, return 1. This means we can code it as the following:

def factorial(n) :
               if n == 0 or n == 1:
                              return 1
               elif n > 1:
                              return n * factorial(n - 1)   

   It is also possible to do recursion such as factorial using loops, but not all recursive problems can be solved without applying recursive calls.  For example, to the sum of all the numbers in a list that contains lists of numbers and numbers. For such recursive data structures, recursive calls can be used when the recursion depth is either unknown or too large to code without recursion.
Often, it is possible such that our coded recursive function may receive errors such as 

RuntimeError: maximum recursion depth exceeded

when the default maximum recursion call limit is reached once a recursion call is recursively called for a certain amount. The error can be fixed by either change the default recursion call limit or create a more efficient recursive function that requires less calls needed. If the object we performed recursion calls on have an infinite recursion depth, recursion will not work if the Runtime Error was not caught.  

   From the lectures in the recent weeks, we learnt about Tree structures, which have a recursive data structure built in such a way that each Tree object either links to sub Tree objects or nothing. It can be used to represent to group data collectively in groups and subgroups. 

   From the past weeks, we have done countless samples regarding recursion in class, and was test on recursion in the recent midterm. It is import for csc students such as myself to know how to use recursion and write code of it.

Friday, 14 February 2014

SLOG # 4: Week 6



   This week, I spend most of the time finalizing my assignment 1 files and handed in the required files on time knowing that it is due Thursday night. What still bothers me right now is realizing how sensitive pep8.py can be towards white spacing.  Although I managed to complete the assignment on time, I spent large amount of times over the weeks to understand what we are asked to do in some of the steps and analyzing how recursion will be used to move cheeses. 

   In the lab this week, we dealt with writing functions that do not use comprehensions, zip, and filter. Our goal was to write functions that would do the same as the function with comprehensions, zip, and filter, but without using them. The only difficulties I had is to understand how these comprehensions, zips, and filters were used, but the part where requires us rewriting the functions was easily done once I read over how the functions were used. Functions such as any(iterable), all(iterable), and filter(function, iterable) can be used to compress for loops and if statement in one single statement, however using these functions for large nested for looped iterables seems rather unnecessary since a normal iteration through the nested for loops would do just the same. 

   During this week’s lectures, we learned about trees. I learned that a tree is defined containing nodes that have a specific value as well as reference to nodes that serve as the children of that node. The node at the very top that link down to all other nodes below it is known as the ‘root’. It is the only node in the tree that does not have a parent. A root can be a single node (a leaf) or be parent of some nodes where the nodes cannot be parent of its predecessors. Because a tree exhibits a recursive structure, tree’s value can be manipulated and traced by means of recursion. 

   This week was a fairly busy week. Just from the assignment alone I had realized I need to start practice more often if I ever want to do well in this course and become a better programmer.

Thursday, 6 February 2014

SLOG # 3: Week 5



   This week, we learned more about recursion and scopes. Although I have not yet finished the assignment, I have gained some insights to how the assignment should be able to do after Monday’s lecture. In the assignment, we are expected to write codes that let the user to play the ‘Towers of Anne Hoy’ game. The game we have to create is based on the ideas of ‘Tower of Hanoi’.  ‘Tower of Hanoi’ starts with a stack of disks stacked on one rod of three in ascending order with the smallest on the top and largest at the bottom.  The main objective is to stack all the disks onto another rod in the same order using the least numbers of moves possible.  

   Using recursion, possible least numbers of moves can be predicted and puzzle can be solved as numbers of disks increases. During the lecture, we learned top disks can be distributed to the intermediate rods so the bottom disk can be stacked on the rod we wanted all the disks be stacked on. Using recursion, the rest the disks can then be redistributed the same way, so the next largest disk can be stacked on the previously stacked disk.

    During this week’s lab, we worked on tracing recursive function calls as well as writing recursive codes. In comparison the lab of the previous week, I experienced fewer difficulties during this week’s lab.  Having used doctest to check for test cases this lab saved us tons of time checking through the codes’ correctness. Under circumstances when the actual result is different from the expected result, I notice tracing can be applied during the process of debugging.