Starting Out with Python describes the use of IPO Charts to help design and document functions. The chart has three columns, input, processing, output:
the input column shows a description of data that is passed in to the function
the processing column describes the process that the function performs
finally, the output column shows the data that is returned from the function
IPO Charts Example
An example IPO chart for our version of absolute value:
And … Some Exercises
Factorial
Does anyone remember what the factorial of a number is? For example, what is 4!? →
24
Factorial Definition
The factorial of a number is the product of all positive integers less than or equal to that number.
it's denoted by an exclamation point (!)
the factorial of 0 is 1
The previous example of 4! is:
Write a Function That Calculates Factorial
We want a function that takes a number and gives back (not prints out!) the factorial of that number.
Example usage:
IPO Chart
How many arguments will the factorial function take, if any? What processing will it do? What will it return, if anything? →
Our no frills IPO chart for factorial:
input: a number to calculate the factorial of
processing: calculate the product of all positive numbers less than or equal to the input
output: return the product
How About Some Pseudocode
In pseudocode, what would the implementation of this function look like? →
Possible Solution
Here's a possible iterative solution:
Using Our New Factorial Function
How would I use this function in a program that asks the user for a number… and then prints out the factorial of that number? →
Is It a David Lynch Movie?
Write a program that determines whether or not a user inputted movie was made by David Lynch.
Who?
American filmmaker and television director
surrealist style
you might know him from:
Blue Velvet
Eraserhead
Dune (yes!)
Twin Peaks
the voice of the bartender in Family Guy (!?)
etc.
Requirements
define a function that determines if a string given is the title of a David Lynch movie
the function will return True or False
use the function in a program that…
continually asks the user:
"Give me a movie title or q to quit"
if the user types q, end the program
if it's a David Lynch movie, print out "(movie) is a David Lynch movie!"
otherwise, print out "There's a fish in the percolator"
Example Output
Go!→
Example Usage of the Function
IPO Chart
How many arguments will it take, if any? What processing will it do? What will it return, if anything? →
Our no frills IPO chart for is_a_david_lynch_movie:
input: a movie title
processing: determine whether or not a movie title is directed by David Lynch
output: return either True (if it's a David Lynch movie) or False (if it's not)
Possible Solution
Greatest Common Divisor?
What's the greatest common divisor (GCD) for 12 and 8?
4
How Did You Figure That Out?
Maybe This Guy Could Help…
Euclid of Alexandria
Who was that? That was Euclid (obv!).
AKA "Father of Geometry"
was writing algorithms two millennia ago! (about 300 BC)
wrote a series of book called Euclid's Elements
one of the most influential works in the history of mathematics
was used to teach math up until the 19th or 20th century
Euclidean Algorithm
One method for finding the greatest common divisor is using Euclid's Algorithm:
start with a pair of positive integers
form a new pair that's made up of the smaller number and the difference between the larger and smaller numbers
repeat until the numbers are equal
the resulting number is the GCD
The Euclidean Algorithm in Action
By hand, find the GCD of 12 and 8 using Eclid's Algorithm. →
Another Example
By hand, find the GCD of 60 and 24 using Eclid's Algorithm. →
Requirements
Create a function that calculates the greatest common divisor of two numbers by using Euclid's algorithm.
Example Usage of the Function
IPO Chart
How many arguments will it take, if any? What processing will it do? What will it return, if anything? →
Our no frills IPO chart for gcd:
input: two numbers to find the greatest common divisor of
processing: use Euclid's algorithm to find the greatest common divisor
output: return the greatest common divisor
Pseudocode
Possible Solution
Um. That's cool… but I just wanna dance! (To some algorithmically generated rhythms).
Euclidean Rhythms
Apparently, the Euclidean Algorithm can be used to generate rhythms. A paper by Godfried Toussaint explores the concept of Euclidean Rhythms.
Euclid's Algorithm can be used to generate traditional music rhythms
The rhythms generated cover almost all of the most important World Music rhythms
Euclidean Rhythms Continued
The Euclidean Algorithm can be used to distribute a set number of notes as evenly as possible over a set period of time (where time is divided into equal parts).
for example, 4 notes in 16 units of time (four notes in one measure of 16 sixteenth notes):