Day 5 - Udemy Chapter 13

Off-topic, I went to see Gozilla yesterday. It was an ‘ok’ rendition of the story, I guess. The one thing that heading out to the movies did for me was give me the idea to do something with the Rotten Tomatoes API . I’m not sure what I could do with it, but it seems like I could think of a cool little mashup to code up. I’ll think more about that later. For now, let me cover what I did today…

I’m in a mad dash to finish up the Udemy Ultimate Python Programming course. I’m just about ready to move on to bigger and better things, but I must make sure I understand the basics before doing so. As far as sections/chapters go, I’ve made it through 15 of 18 of them. I’ll finish up the remaining 3 sections of the course, and decide what I’m going to next by the end of the weekend.

As far as code writing, I went through a couple very simple scope examples that illustrates nested functions (functions defined inside functions) and lexical scoping (or what I like to call regional scoping- it’s not local, and it’s not global either):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" chapter 13, exercise 2 """
import math

def average(x,y):
    return (x + y) / 2

def square(number):
    return number * number

def sqrt(number):
    def closeEnough(guess):
        return (math.fabs((square(guess)) - number) < 0.001)
    def improve(guess):
        return (average(guess, (number / guess)))
    def sqrtHelper(guess):
        if closeEnough(guess):
            return guess
             return sqrtHelper(improve(guess))
    return sqrtHelper(1.0)

print sqrt(144)

This exercise is a implementation of Newton’s method of finding the square root of a number. It’s a mixed bag of using nested functions, recursion, and lexical scoping in order to find the square root of 144 using Newton’s method. Tomorrow, I’ll work on Chapter 14-15 exercises.

Jason T Clark

Jason T Clark

Father. Musician. Gamer. Coder.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin