WTP Computer Science

Problem Set 4

Due: July 2, 2010




Exercise 1: Making desserts with python


This exercise will give you practice with creating functions. Let's start with the program below that prints a recipe for Tiramisu. You can go to the Problem set web page and copy the code instead of typing it in yourself.

print 'Tiramisu (Wolfgang Puck)'
print 'Ladyfingers: '
print '* 6 eggs, separated'
print '* 1 cup cake flour, sifted'
print '* Melted butter, for brushing pan'
print
print 'Mascarpone Cream: '
print '* 6 egg yolks'
print '* 1 cup sugar'
print '* 1/3 cup Marsala'
print '* 1/4 cup brandy'
print '* 2 pounds mascarpone cheese'
print
print 'Espresso Syrup: '
print '* 1 cup espresso, hot'
print '* 3 tablespoons brown sugar'
print '* 1 tablespoon suagar'
print '* 1 teaspoon lemon juice'
print '* 1 teaspoon vanilla extract'
print '* 1/2 cup grated bittersweet chocolate'

Exercise 1.1 Creating a ladyfingers function


Let's create a function called ladyfingers() that will contain the instructions for making the ladyfingers.

def ladyfingers():
    print 'Ladyfingers: '
    print '* 6 eggs, separated'
    print '* 1 cup cake flour, sifted'
    print '* Melted butter, for brushing pan'
    print


print 'Tiramisu (Wolfgang Puck)'

print 'Mascarpone Cream: '
print '* 6 egg yolks'
print '* 1 cup sugar'
print '* 1/3 cup Marsala'
print '* 1/4 cup brandy'
print '* 2 pounds mascarpone cheese'
print
print 'Espresso Syrup: '
print '* 1 cup espresso, hot'
print '* 3 tablespoons brown sugar'
print '* 1 tablespoon suagar'
print '* 1 teaspoon lemon juice'
print '* 1 teaspoon vanilla extract'
print '* 1/2 cup grated bittersweet chocolate'

Save the program in a file called recipes.py and run it. Does it print the whole recipe? Why, or why not?

 

 

What do you need to change in order to see the whole recipe? Modify your program so that it does print the entire recipe.

 

 

 


Exercise 1.2 Adding functions for the cream and the syrup


Now create a mascarpone_cream() and an espresso_syrup() functions that print the instructions for the Mascarpone cream and the Espresso syrup, similarly to the ladyfingers() function in the previous exercise.

Modify the program so that uses these functions to print the Tiramisu recipe.


Exercise 1.3 Adding formatting


Add the following function to your code. You can place it above the function ladyfingers().

def line():     
print 20 * '-' + 6 * 'o' + 3 * 'O' + 6 * 'o' + 20 * '-'

Modify your code so that the function will be called three times in the program with the result of having lines drawn above and below the title, and at the end of the recipe.

Create a function called tiramisu() that prints the entire recipe together with the special formatting. Run your program and make sure it prints the recipe.


Exercise 1.4 New recipe


Now let's add a recipe for Ladyfinger Parfaits.

Ladyfinger Parfaits
Ladyfingers
Parfait Cream:
* 2 cups melted vanilla ice cream
* 4 tablespoons orange flavored liqueur
* 1 cup raspberries

Create a function called ladyfinger_parfaits() that prints the entire recipe. Create a separate function for the parfait cream called parfait_cream().

Make your program print both recipes including the instructions for making ladyfingers in each recipe. Here is what your output should look like:

--------------------ooooooOOOoooooo--------------------
Tiramisu (Wolfgang Puck)
--------------------ooooooOOOoooooo--------------------
Ladyfingers:
* 6 eggs, separated
* 1 cup cake flour, sifted
* Melted butter, for brushing pan

Mascarpone Cream:
* 6 egg yolks
* 1 cup sugar
* 1/3 cup Marsala
* 1/4 cup brandy
* 2 pounds mascarpone cheese

Espresso Syrup:
* 1 cup espresso, hot
* 3 tablespoons brown sugar
* 1 tablespoon suagar
* 1 teaspoon lemon juice
* 1 teaspoon vanilla extract
* 1/2 cup grated bittersweet chocolate
--------------------ooooooOOOoooooo--------------------

--------------------ooooooOOOoooooo--------------------
Ladyfinger Parfaits
--------------------ooooooOOOoooooo--------------------
Ladyfingers:
* 6 eggs, separated
* 1 cup cake flour, sifted
* Melted butter, for brushing pan

Parfait Cream:
* 2 cups melted vanilla ice cream
* 4 tablespoons orange flavored liqueur
* 1 cup raspberries
--------------------ooooooOOOoooooo--------------------


Exercise 2: Mystery program

This is exercise 6.4 from the text. Draw a stack diagram for the following program. For illustration purposes, do not erase the stack frame for each function as it returns. At the end you should have a frame for each function call that is made during the program's execution. What does the program print? Again, don't actually run the program. Note: You can do this at home.

def b(z):
    prod = a(z, z)
    print z, prod
    return prod


def a(x, y):
    x = x + 1
    prod = x * y
    return prod


def c(x, y, z):
    sum = x + y + z
    pow = b(sum)**2
    return pow


x = 1
y = x + 1
res = c(x, y+3, x+y)
print res



























































Exercise 3: Math module


In this exercise, we will play with some of the functions provided in the math module. A module is a python file with a collection of related functions. To use the module, you need to add the following line at the beginning of your program:

import math

Now if you want to find out what is sin 90, we first need to convert from degrees to radians and then use the sin function in the math module:

radians = (90.0 / 360.0) * 2 * math.pi 

print math.sin(radians)

Many computations can be expressed concisely using the “multadd” operation, which takes three operands and computes a * b + c. One of the purposes of this exercise is to practice pattern-matching: the ability to recognize a specific problem as an instance of a general category of problems.

For the mathematical functions, you can generally call math.func, where func is whatever function you want to call. For example, if you want the sine of an angle a (where a is in radians), you can call math.sin(a). Assume that the logs in “log 10 + log 20” are base 10, which you can calculate using math.log10. You can find information on all the functions in the math module here. Print out all the code you write and hand it in.

In the last part, you get a chance to write a method that invokes a method you wrote. Whenever you do that, it is a good idea to test the first method carefully before you start working on the second. Otherwise, you might find yourself debugging two methods at the same time, which can be very difficult.


Exercise 3.1


Create a new file multadd.py and create a function called multadd that takes three arguments and that returns the result of the multadditionization.

Test your function with paramaters like 1.0, 2.0, 3.0. The result should be 5.0. Is it?


Exercise 3.2


Use the multadd function to compute the following values and then print out the result:

 


Exercise 3.3


Write a new function called yikes that has one argument and uses the multadd to calculate the following and then print the result:

Hint: The function for raising e to a power is math.exp and the function for computing the square root is math.sqrt.

When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.

Submit the file electronically by typing:

 athena% submit ps4 multadd.py

Exercise 4: Fancy formatting


Try the following program in python interactive mode and see what each of the print statements outputs.


Formatting strings


name = 'Python'

# print a string
print 'the language is called %s.' % name

# print strings
print '!%s!' % name

# right justified - with spaces inserted to 
# make total of 20 or 30 characters
print '!%20s!' % name
print '!%30s!' % name

# left justified - spaces inserted to make total of 10 characters
print '!%-10s!' % name

Formatting integers


x    = 102
y    = 5

# print integers
print '%d + %d = %d' % ( x, y, x+y )
print '%d %d' % (x, y)

# print aligned integers with spaces inserted to 
# make total of 10 characters
print '%10d %10d' % ( x, y )

# print aligned integers with 0s inserted to make total of 4 characaters
print '%04d %04d' % (x, y)

# use * to pass the alignment integer as a parameter
print '%*d' % (y, x)

# you can use formatting operators more than once in a print
statement like this
print '%*d' % (y, x) + '%*d' % (y, x)

Formatting floating point numbers


pi   = 3.14159
e    = 2.718281828

# print floating point numbers
print '%f' % pi

# prints the nunmbers to three decimals places and
# inserts spaces so that the number of characters is 10
print '%10.3f' % e
print '%10.3f' % pi
print '%10.3f' % x
print '%10.3f' % y

The formatted output in python works with %d for integers, %f for floats, and %s for strings.

You don't have anything to turn in for this exercise.


Exercise 5: Power tables



Exercise 5.1 Printing the power table


Write a function called power_table that takes the value n as a paramater and prints out the n x n table of powers in which the entry in the row i and column j contains i raised to the power of j. For example, the entry in the third column of the second row should be 8, because 2**3 = 8. Save your program in a file power_table.py.

Hint: Remember for loops!

Make your program output a 6x6 power table.


Exercise 5.2 Formatting the table


Now look back at exercise 4 and figure out how to output the table so that the columns are aligned. For example, a multiplication table that would normally be printed like this:

1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36

After you add formatting, it should be printed as follows:

 1  2  3  4  5  6 
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36

Hint: What is the maximum number of digits that any entry in your power table has? You can use the following expression to count the number of digits in an integer:
max_digits = int(math.floor(math.log10(x))) + 1
Another hint: You can use the formatting options in exercise 4 and assign them to a variable.
s = '%*d' % (5, 10)

print s

Exercise 5.3 Fancy formatted table (optional)

If you do this, save your code in a separate file as power_table_optional.py. Now create borders around each cell of the table. Your table should look like this:

+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+
| 2 | 4 | 6 | 8 |10 |12 |
+---+---+---+---+---+---+
| 3 | 6 | 9 |12 |15 |18 |
+---+---+---+---+---+---+
| 4 | 8 |12 |16 |20 |24 |
+---+---+---+---+---+---+
| 5 |10 |15 |20 |25 |30 |
+---+---+---+---+---+---+
| 6 |12 |18 |24 |30 |36 |
+---+---+---+---+---+---+
Hint: To print the horizontal lines, you can create a function called print_line. Think what parameters you need to pass to it.

What to turn in


When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.

Submit the file electronically by typing:

 athena% submit ps4 recipes.py
 athena% submit ps4 multadd.py
 athena% submit ps4 power_table.py
 athena% submit ps4 power_table_optional.py