Design

Thursday, February 13, 2014

Recursive Turtle Tree

I know the title of this post may sound like random words thrown together, but it's actually a really cool activity to learn about recursion using the turtle module in Python (Linux users need to install tkinter). I've been learning about algorithms and abstract data structures as part of my faking a CS degree personal mission. Problem Solving with Algorithms and Data Structures has been a fantastic resource so far (and free). Although the authors' explanation of recursion is the best I've ever seen, I think I could just add a little to the tree drawing example. Here's the program they provide:

import turtle

def tree(branchLen,t):
    if branchLen > 5:
        t.forward(branchLen)
        t.right(20)
        tree(branchLen-15,t)
        t.left(40)
        tree(branchLen-15,t)
        t.right(20)
        t.backward(branchLen)

def main():
    t = turtle.Turtle()
    myWin = turtle.Screen()
    t.left(90)
    t.up()
    t.backward(100)
    t.down()
    t.color("green")
    tree(75,t)
    myWin.exitonclick()

main()

Now I'm going to focus on the tree function. Notice tree calls itself twice. Now to step through how this function works, I am going to give each line a number and each recursive call a number such that 1.2 is the second time the first recursive call has been made and 2.3 is the third time the second call has been made. 

def tree(branchLen,t):
0    if branchLen > 5:
    1    t.forward(branchLen)
    2    t.right(20)
    3    tree(branchLen-15,t)     first recursive call
    4    t.left(40)
    5    tree(branchLen-15,t)    second recursive call
    6    t.right(20)
    7    t.backward(branchLen)

Let's trace branchLen = 30, following the code and what the turtle does. I've color coded each recursive call:

Code Line    Action                                Recursive Call
0               Passes > 5 test                       none
1               Forward 30                           none
2               Turns right 20 degrees           none
3               Calls tree(15)                        none
0               Passes > 5 test                      1.1
1               Forward 15                           1.1
2               Turns right 20 degrees            1.1
3               Calls tree(0)                           1.1
0               Fails > 5 test                          1.2
4               Turns Left 40 degrees             1.1
5               Calls tree(0)                           1.1
0               Fails > 5 test                          2.1
6               Turns right 20 degrees            1.1
7               Backward 15                         1.1
4               Turns Left 40 degrees             none
5               Calls tree(15)                         none
0               Passes > 5 test                        2.2
1               Forward 15                            2.2
2               Turns right 20 degrees             2.2
3               Calls tree(0)                            2.2
0               Fails > 5 test                           1.3               
4               Turns Left 40 degrees             2.2
5               Calls tree(0)                           2.2
0               Fails > 5 test                           2.3
6               Turns right 20 degrees            2.2
7               Backward 15                         2.2
6               Turns right 20 degrees            none
7               Backward 30                         none

If you choose a color such as red or orange, you can see how the function is executed in its entirety, but with interruptions. The colors that appear nested, such as orange inside the red, appear higher in the stack frame. They are completed first even though they started second, but the program continues to the red after the orange is completed. The next value of branchLen to create more recursive calls is 45. You could also try tracing tree(45), using tree(30) as a shortcut to emphasize the benefit of recursion.
               

Coursera -- Introduction to Interactive Programming in Python

A couple months ago, I took a free course on Coursera.com offered by Rice University and taught by Joe Warren, Scott Rixner, John Greiner, and Stephen Wong. Coursera offers the course a couple times a year, with 2014 offerings including March 24th and October 7th. The course is now part of the Fundamentals of Computing specialization and you earn a certificate after completion with the ability to earn additional "distinction" for scoring an A in the course (I did).

I recommend the course for anyone between "Isn't Python a snake?" to "Why should I use classes?" Students learn Python by developing simple games in CodeSkulptor, which includes a simple GUI like PyGame without using a terminal or setting up a coding environment. Homework includes quizzes over video material, building games in Python, and evaluating peers' code. I enjoyed the peer reviews because you can recognize common mistakes and see how other people solved the same problem you did. The game development approach finally helped me understand the point of object oriented programming. I went from fear of using classes, to empowered by building my own classes in the last three weeks of the course.

I am looking forward to the next course in the Fundamentals of Computing specialization, Principles of Computing. It is exciting to have an option for continuing to learn after a course. Us self-taught learners often waste time trying to decide what is the most appropriate and useful for us to learn next. If you can tolerate some corny Big Bang references and want to take your Python development to the level of using and understanding classes, you can enroll for free on Coursera.com

Tuesday, June 12, 2012

Moving :)

I have been busy with reStructuredText lately because I'm moving to housewifehacker.com. I've also been busy with reading entrepreneur blogs and books because our conference management system has been accepted to StartUp Chile to receive $40,000 in equity free funding. July is going to be busy with hacking on different technologies as well as getting our work visas to live in Chile. My new blog will have categories for Code and StartUp, maybe Personal/Travel too, so you can subscribe to which categories you are interested in. I dont have feed burner or RSS set up on the new domain yet, but you can check out my most recent code tutorial on there now. It's a guess the number script using the new preferred string formatting for python. It also addresses some exception handling, the process of developing a script, and a teensy bit about global variables and scope. Please follow me on twitter @housewifehacker

Tuesday, May 15, 2012

Simple Dictionary Practice: Is Anagram?

I do not use dictionaries very often. Friday, I was without internet all day, so I took the opportunity to play with dir() and help() to discover some dictionary properties. My short-lived obsession with Draw Something on the iPhone has gotten me interested in anagrams (kicked the habit by reading programming books). I believe using dictionaries is the fastest and most accurate way to determine if two words are anagrams of each other.

A dictionary is an unordered set of key: value pairs. Keys must be an immutable type. Values can be anything. Review on mutable versus immutable here. Being unordered causes some interesting properties for working with dictionaries, different from any other python data structure. Instead of being indexed by numbers, dictionaries are indexed by keys. Because they are indexed by keys, each key is unique within it's dictionary. If two dictionaries with the same keys are added to each other, the values of the same data type combine. This is convenient for our anagram activity. But first, some dictionary review.


>>> sample_dict = {}        # creates an empty dictionary
>>> type( sample_dict )
<type 'dict'>
>>> sample_dict['Name'] = 'Jessie'        # creating a key:value pair
>>> sample_dict['Age'] = 23        # another key:value pair
>>> sample_dict
{'Age': 23, 'Name': 'Jessie'}
>>> sample_dict2 = {'Name': 'Jessie', 'Age': 23}        # another way to create dict
>>> type (sample_dict2)
<type 'dict'>
>>> sample_dict2
{'Age': 23, 'Name': 'Jessie'}
>>> sample_dict + sample_dict2        # cannot add dictionaries, only values
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> sample_dict['Age'] + sample_dict2['Age']        # adds values
46
>>> sample_dict.keys()
['Age', 'Name']
>>> sample_dict.values()
[23, 'Jessie']
>>> type( sample_dict.values() )        # keys and values are returned as lists
<type 'list'>
>>> sample_dict.get('Age')        # gets the value at a specific key
23
>>> type( sample_dict.get('Age'))         # value maintains data type in dictionary
<type 'int'>
>>> sample_dict.has_key('Age')        # D.has_key() returns boolean
True
>>> sample_dict3 = {'Children': 'Graham'}
>>> sample_dict3.update(sample_dict)        # update keys and values
>>> sample_dict
{'Age': 23, 'Name': 'Jessie'}
>>> sample_dict3        # Children field is added as a key:value pair
{'Age': 23, 'Children': 'Graham', 'Name': 'Jessie'}
>>> {'Age': 23, 'Name': 'Jessie'} == {'Name': 'Jessie', 'Age': 23}        # different order is equal
True


How do we know if two words are anagrams? Consider the anagrams odor and door. We could say that they are reshuffled strings. Each word uses the same letters, but in a different order: 2 o's, 1 r, and 1 d. My simple program creates empty dictionaries for the two words being compared, stores the letters as keys, and adds to the value for each occurrence of the same letter, then checks that the dictionaries are equivalent. I have not included exception handling and I made the design decision to count white space as part of the anagram such that 'abc def' is not an anagram of 'fdeabc,' but is an anagram of 'abc fed.'


def get_dict(word, count):
     for i in word.lower():
         if count.has_key(i):
             count[i] += 1
         else:
             count[i] = 1
     return count

 def main():
     word1 = raw_input("What is the first word? \n")
     word2 = raw_input("What is the second? \n")
     count1 = {}
     count2 = {}
     count1 = get_dict(word1, count1)                              
     count2 = get_dict(word2, count2)
     if count1 == count2:
         print("Yes, those are anagrams!\n")
     else:
         print("No, you've failed \n")

 if __name__ == "__main__":
     main()


Friday, May 11, 2012

Book Review: The Practice of Programming

Affiliate Link

The Practice of Programming, written by Brian W. Kernighan and Rob Pike, was originally published in 1999. Although most programming books more than a couple years old are obsolete with out of date technology, The Practice of Programming is a pragmatic guide to become a better programmer regardless of your chosen language, framework, or supporting technologies. To quote the epilogue, "The world of computing changes all the time... but there are some constants, some points of stability, where lessons and insight from the past can help with the future." I highly recommend this book to any programmer who wants to establish good habits for writing understandable and consistent code. I also recommend this for programmers who want to or need to be able to work on projects with other developers. Although the exercises and examples were written in languages other than python, I was able to learn a lot. I reflected a lot on my own code, figured out new questions to ask about python, and am more appreciative that python does not suffer from some of the common pitfalls of other languages.

Monday, May 7, 2012

Mutable versus Immutable Objects

Strings, lists, tuples, integers, float, dictionaries, and sets are all types of python objects with different properties and uses. When using an object in a program or in your terminal, your session assigns an Id to access the computer's memory. The Id will be unique each session and on each computer, so the actual number returned by the id() function is irrelevant. What is relevant is when that number changes. If the id changes as the value changes, the computer had to assign a new id to the immutable object. If the id stays the same, then it is a mutable object, because the value associated with the id can be changed without changing the id. Integers are immutable:

>>> x = 5
>>> y = x    # direct the reference of y to be the reference of x
>>> id(x) == id(y)    # x and y point to same reference
True
>>> x = 4    # change the value of x
>>> id(x) == id(y)    # the id of x changed when we changed the value
False
>>> x == y    # the value of y did not change with x
False


I'm the type of learner that skims through vocabulary lessons to get to the action, but understanding this next part will save you some headaches when trying to manipulate mutable objects. Look what happens when I try to do the same thing I just did to the integers, but now to a list:

>>> x = [5]
>>> y = x    # direct the reference of y to be the reference of x
>>> id(x) == id(y)    # x and y point to the same reference
True
>>> x.append(4)    #change x from [5] to [5, 4]
>>> id(x) == id(y)    # x and y still point to the same reference
True
>>> x == y    # y changed with x
True
>>> y
[5, 4]
>>> z = [5, 5]    
>>> id(z)    # will be a different number for everyone
3075316972L
>>> z.append(3)    #z is now [5, 5, 3]
>>> id(z)    # id is constant, the list is mutable
3075316972L


I stumbled upon this while using random.shuffle on a list, while wishing to keep a copy of the list in it's original form. As you can see by assigning x equal to y, the lists changed together. That was an ineffective way to make a copy because all I did was assign the same Id two different names. Try determining if the other object types are mutable or immutable. I don't want to spoil the fun for you.

Sunday, April 22, 2012

Python Style Guide

Did you realize Python has a style guide to clarify consistent and readable code? It covers topics such as white space of indentations in-line, capitalization of variable, function, and class names, and formatting of comments. Read Pep8

Now I look at my code examples and see a whole lot of ugly. Some good news is that some of the proper styles can be integrated into your vim configuration. A tab on your keyboard can type four spaces. A red line can be drawn vertically to show when a line exceeds the recommended 79 characters. The other good news is that you can run the pep8 script to find style errors. I am going to make an effort to follow the style guide, as well as improve my names. By using "is" to preface boolean tests, verbs such as "get" prefacing function names, and complete words or widely accepted abbreviations (such as str for string), my code can be more readable. I am also going to make my names something that I can sound out. As my code becomes more complicated, variable class and method names are easier to remember if you can pronounce them out loud. 

To use pep8, you simply install it using pip or easy install, run it against your python file, then read the corrections pep8 expects. Example corrections may be that two empty lines were expected instead of one, max line length was surpassed, or extra spacing was found. The script prints the lines that each instance is found on, so you can easily make changes if you want to comply with the preferred style. Documentation can be found here.

I am reaching a stage in my learning where I am reading more code than I am writing. Ambiguous names, inconsistent capitalization, incorrect spelling, and weird in-line spacing makes reading code more difficult. I did have the mindset that if Python didn't care, why should I? Well, now I do care. Just like when learning a spoken language, reading is an important part of learning how to communicate with the language properly.