Udacity CS 212: Learning more

A new toy we can play with, itertools.

The module itertools include methods to build iterators effectively. Its pretty hard to understand this description from the Python documentation, so we’ll just look at some examples from the website:

repeat(1,5) # object representing (1, 1, 1, 1, 1)
imap(func,(1,2,3),(4,5,6)) # object representing (func(1,4), func(2,5), func(3,6))
product('ABC','xy') # ('Ax', 'Ay', 'Bx', 'By', 'Cx', 'Cy')
permutations('ABC', 2) #('AB', 'AC', 'BA', 'BC', 'CA', 'CB')
combinations('ABC', 2) # ('AB', 'AC', 'BC')

Do remember to import itertools and prefix every method you want to use with itertools.

A very important concept to know is list comprehension.

[s for r,s in cards if s == 'H']
# this has the format [(term) for (for clause) ...optional if and for clauses]

The expression above is basically the same as the block below:

result = []
for r,s in cards:

if s == 'H':


The benefits of writing list comprehension is that it is simple to add more for and if conditions, just add it to the back of the line. The entire list comprehension is read from left to right, ignoring the first term.

A very similar concept generator expression.

The format of a generator expression is very similar to that of a list comprehension:

g = (s for r,s in cards if s == 'H')

Note the differences, using parentheses instead of square brackets. A generator does not evaluate the expression in the parentheses when you first assign it. Instead it represents a generator object, which is sort of like an understanding that g will evaluate the expression when called to. So the way to ask g to start evaluating is next(g). This will get the first s (since it is still on the 0th place as g has not started to evaluate. The next of 0th place is the first term)

A simpler way to iterate through a generator to print out every term in the expression is:

for i in g: print i

This ensures that you will not iterate through the end of g (by manually calling next), which will cause Python to throw a StopIteration error.
Alternatively, you can convert the expression into a list using list(g), this will return a list containing each term of g as an element.

A cool looking thing *args

You usually see *args in definitions:

def something(fn, *args):


In the definition, *args means that the function can take any number of arguments, and all the arguments should be joined together into a tuple called *args.
And when it appears in the block, it means to unpack the tuple, and apply the function fn to all the elements in the tuple.

The interesting thing to note here is that functions can be passed around as arguments, like strings and integers. So we are able to decide when to execute the functions, which is useful to checking how long a particular function takes to run, by calling time.clock() once before running, and again after running.

This Udacity post references Udacity CS 212 Unit 2.

Udacity CS 212: Starting out

I’m already halfway through CS 212 (today is day 2 of starting), and have learned so much more! Below I will document some new knowledge of Python I have gained.

First we start of with a new data type, tuple.

Tuples are constructed using parentheses, and contains items separated by commas, similar to lists. For example (1, 2, 3) is a tuple.
Tuples are also non-mutable, which means that like strings, once a tuple is created, you will not be able to modify the entries in the tuple.
A tuple and a list is similar in that they both below to a type of data called Sequence Type, and hence many methods which are available for lists can also be used on tuples:

x in tuple
x not in tuple
tuple[0] # first item in tuple
len(tuple) # number of items in the tuple

Additionally, I learned new methods which are available to both list and tuple types:

min(tuple) # returns the smallest item in tuple
max(tuple) # returns the largest item in tuple
tuple.index(i) # returns the index of the first occurrence of i in tuple
tuple.count(i) # returns the number of occurrence of i in tuple

Next, I learned another new data type, set, which is an unordered collection of distinct objects.

Continue reading

Udacity CS 101: Unit 7

I sped by Unit 7 really quickly, in about 2 hours. The videos were really interesting, introducing you to some history of computers, open source software, companies with social improvement goals, and university students with their projects! Right now whats left is the Exams. And we probably can’t discuss that! So I guess its the end of CS 101 for this blog! Its been a quick but fruitful journey, and I hope what I documented here will be helpful to some of you. I’ll be moving on to CS212, which began 3 weeks ago. Time for some catching up!

This Udacity post references Udacity CS 101 Unit 7.

Udacity CS 101: Unit 6

Okay so I finally completed Unit 6, and it was difficult. It seems like the advancement to PageRank was a very big jump. Maybe that’s why people at Google (and other search engine like DuckDuckGo) have to be really really smart. I didn’t understand what was the damping function was for, and even after watching the videos quite a few times I still did not get it. But I guess that is actually more Math and Computer Science, although one can argue that both fields are intertwined quite closely! I sure hope you understand it! Check out Wikipedia’s entry on Page Rank for more information!

This Udacity post references Udacity CS 101 Unit 6.

Udacity CS 101: Recursive

UPDATE: I wrote this post before heading to Chapter 12 of Unit 6, and I just realized that this post contains the answer to the question! So stop readying if you have not attempted!

You might remember seeing this term somewhere in my blog before, and today we are going to visit recursive functions again, under the guidance of Professor Evans. Recall the Fibonacci sequence

1, 1, 2, 3, 5, 8, 13, 21 ...

1, 1, 2, 3, 5, 8, 13, 21 …
Image courtesy of Wolfram MathWorld

1, 1, 2, 3, 5, 8, 13, 21 …

Each number is the sum of its two preceding numbers ( 2 = 1 + 1, 21 = 8 + 13).

In every recursive definition, there are two cases, the base case, and the recursive case. Continue reading

Udacity CS 101: Dictionary

We are introduced to another complex data types in Python: Dictionary. Dictionaries are created using curly braces, and entries in a dictionary are key-value pairs:


Something similar, but not.
Image courtesy of Wikipedia

alphabets = { 'a':1, 'b':2, 'c':3 }

Essentially a dictionary functions like a hash table, given a particular key, a its value is returned. A dictionary is mutable, just like a list. After creating a dictionary, we can append new key-value pairs, and we can modify individual key and values. Continue reading

Udacity CS 101: Hash

A hash function, is a function that maps a big set of data to a smaller set of data. For example, a hash function can map people’s names, to an integer between 1 and 10. Hash is explained in the video using terms like keys and buckets. In this case, the keys are the names, and there are 10 buckets, numbered 0 to 9.

A Hash Function

An example of a hash function.
Image Courtesy of Wikipedia

A good hash function has certain properties. It tries to spread the keys evenly over all the buckets, so the size of each buckets after hashing is roughly the same. And for each key, it will definitely output a hash value that is valid, i.e. less than or equals to the numbers of buckets available. Continue reading