Lesson 21

Moving along, here! This lesson was mostly a refresher on function ordering, for me. At the very end, there is an extra credit portion where he recommends resolving a nested set of functions by hand before running the program.

print "Here is a puzzle."

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"

I knew that it was necessary to resolve inner parentheses before moving on to the next inner set of parentheses as just a giant composed function, but once I got to the non-commutative operations (ie, subtraction and division, in which order of operation matters) I flubbed it up a bit, and came up with a different answer than when I ran the program. Then I examined it a bit, and with a bit more analysis (that’s an overly heady word for it, ha ha!) in the form of the following:

age = add(30, 5) #35
height = subtract(78, 4) #74
weight = multiply(90, 2) #180
iq = divide(100, 2) #50

and then resolved the commutativity issue, I came up with the same answer that he did! woo-hoo!

Woo-hoo! Another lesson down!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s