Monday 31 March 2014

Monday 24 March 2014

Week 7: Recursion for realz this time

You just keep doing it over and over again.
Essentially recursion is a function that calls itself in order to complete whatever task you desire.

In order to use recursion there has to be:

  • a pattern or specific sequence that repeats it self.
  • a base case so the computer knows where to start.
  • a function that does something

The concept of recursion is not too difficult to understand, but its not as simple as it looks.
This stuff gets pretty confusing when you have some whacky sequence. 

Creating a recursive function ezpz once you get the base case, luckily if you're good with recognizing patterns you can get that base case. Unfortunately I am not that person.

 Here for some examples.
    

    Next example:
    

Monday 17 March 2014

A2 Part 2

It wasn't that bad...

Week 6: Trees

Trees have branches and leaves. 
Built-in function. They makes code shorter and the program more efficient. See Built-in Functions .
          1. all(iterable)
    Return True if all elements of the iterable are true (or if the iterable is empty).
2. any(iterable)
    Return True if any element of the iterable is true. If the iterable is empty, returnFalse.
3. filter(functioniterable)
    Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements ofiterable that are false are removed.
4. map(functioniterable...)
    Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, seeitertools.starmap().
5. repr(object)
    Return a string containing a printable representation of an object.
6. zip(*iterables)
    Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.
                         >>> x = [1, 2, 3]
                         >>> y = [4, 5, 6]
                         >>> zipped = zip(x, y)
                         >>> list(zipped)
                         [(1, 4), (2, 5), (3, 6)]
                         >>> x2, y2 = zip(*zip(x, y))
                         >>> x == list(x2) and y == list(y2)
                         True
 
    We wrote some equivalent code during the lab using non-built-in functions. But use of built-in functions as well as built-in modules is of great importance to program in python.
 

Monday 10 March 2014

Week 5: Name Names

Variables are stored differently. Now you know.
def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)