IPND Notes

The purpose of this site is to show the notes that I, Felipe Fernandez, have taken during my study of the Intro to Programming Nanodegree course at Udacity.

Felipe's Projects Home
IPND Home
Section 1 - HTML/CSS
Section 2 - Programming
Section 3 - Object Oriented Programming
Section 4 - Allow Comments
Section 5 - Advanced Python Topics

Section 5

Advanced Python

Recursion

In the beginning python, we learned how we can have power using if/else statements and loops. In this advanced python section, we learn how to fully have infinite power using recursion. Why infinite? It is because recursion allows us to create infinite objects. The is done by definining two cases for a concept, a base case and a recursive case.

The base case is defined not in terms of itself and is the simplest input or unit in which we already know the answer. Examples are below:

word -> intelligence
ancestor -> parent
countdown -> 0
factorial of 0 -> 1

The recursive case is defined in terms of itself and the answer is computed. Using the base cases above, some examples are:

word -> counter-word
ancestor -> parent of ancestor
countdown -> countdown(n-1)
factorial of n -> n * (factorial of (n-1)) #n is an integer 0 or greater

The base case and recursive case are used to define concepts. Let's say we want to print a countdown. We start from an integer input and count down until we reach zero. To do this procedurally in python we need to use the base case as our stopping point. The countdown ends at zero so we will have countdown -> 0 as our base case. For the countdown, we need to subtract one from the integer each time. That will be our recursive case. So for our recursive case, we will have countdown -> countdown(n-1). This will result our procedure to print out a countdown starting from the input, in this case 5, down to 0.

          def countdown(n):
            print n
            if n==0:
              return 0
            else:
              return (countdown(n-1))

          countdown(5)
        

Recursion is another example of avoiding the use of repetitive code. It is also an elegant way to express a concept. And now for a recursion joke.

RecursionJoke

I can see some of my friends clicking on the recursion link before they get the joke.

LOL! Good one Google!

ADDITIONAL RESOURCE: http://openbookproject.net/thinkcs/python/english3e/recursion.html

This link reiterates recursion and the what the recursive defintion means. It also introduces the concept of fractals, which to me are like building blocks to help create a recursive solution for a problem. It also provides both a high-level and low-level view of recursion, which helps think recursively. It also provides additional procedural recursion examples. I like example 18.5 where recursion is used to list files in a directory. I also like how example 18.6 shows how recursion can be used to draw interesting shapes. It really expanded my view on how recursion can be used for a variety of applications.

Parallel Computing

Parallel computing involve taking a big task, breaking it into smaller tasks, and working on them at the same time. For example, let's say you have a math homework that has 100 problems. You tell yourself, "This work will take too long to finish and I have a hot date tonight." So then you call up 3 of your buddies and ask if they could help you. Because they are your good friends, they agree to help you. So the 4 of you guys gather and decide each person will do 25 problems, the work gets done quicker, and you get to go on your hot date. In this scenario, the four of you guys working together is a form of parallel computing. As you can see, a major advantage is that work gets done quicker.

ADDITIONAL RESOURCE: http://web.eecs.umich.edu/~qstout/parallel.html

This link breaks down the concept of parallel computing even further. It shows the pros and cons to parallel computing as compared to serial computing. It also shows how parallel computing has its limits. The link gives an example of a house. Although some systems of the house can be built in parallel, such as plumbing and electrical, the foundation of the house has to be built first to support the other systems. It wouldn't work if the foundation was built in parallel with the roof. The link also shows the need for parallel computing today as there is the demand to process data faster on computers.