# 15-110 Principles of Computing Summer 2018

## Problem Set 2

Read pages 19-42 of chapter 2 of the book Blown To Bits.

### Exercises

1. (2 pts) For each of the following Python expressions, show how they are evaluated in Python (think about the rules for operator precedence and associativity!) by evaluating one operation at a time and showing the reduced expression until you have a final answer. For example, if the Python expression is 3 * 5 + 5, you answer would look like this:

3 * 5 + 5
15 + 5
20


1. 7 + 10 / 4 * 8 - 8

2. 58 // 2 ** 3 / 5

3. 85 % 9 // 2 * 7

4. 3 - 3 * 3**2 + 3**3

2. (2 pts) The purpose of this question is to get you to think about various ways programs can go wrong.

For a mortgage with a principal amount of P$P$ dollars, an interest rate of r$r$ per year (expressed as a decimal fraction such as 0.04 for 4%) for n$n$ years, the final debt amount M$M$ including the principal can be calculated by the formula

M=P(1+r)n.

The following Python function implements this equation to compute the final amount of a mortgage after a number of years, based on the principal and a given interest rate.

def final_amount(principal, interest_rate, years):
return principal * (1 + interest_rate)**years


1. Suppose we use this function to compute the final value of a mortgage with an interest rate of 10% on a principal of \$1000 after three years (ignore the financial unreality of this!):

final_amount(1000, .1, 3)

Will we get an integer result or a floating point result? Why?

2. Suppose we want to do the computation in part (a) but we call our function without the number of years:

final_amount(1000, .1)

Does the function use a default value for the years, or does Python complain about this function call? Explain.

3. Suppose we want to do the computation in part (a) but we call our function with the arguments in the wrong order:

final_amount(3, .1, 1000)

Does Python report an error? Why or why not?

4. Suppose we replace the return statement with a print statement as shown below:

def final_amount(principal, interest_rate, years):
print(principal * (1 + interest_rate)**years)


What value is stored in the variable amount if we execute the following instruction? Why?

amount = final_amount(1000, .1, 3)

3. (2 pts) For each of the following invalid Python expressions, evaluate them in python3 and explain the errors that you see.

1. return = "bye bye"

2. 1 // 0

3. "15+19" / "cat**110"

4. import math
math.sqrt(-25)


4. (2 pts) This question is meant to help you understand how the return statement works, and in particular, how it interacts with loops. If you investigate the question carefully, it will save you a world of trouble in upcoming programming assignments.

Consider the following Python function definition that uses a loop:

def mystery(k, n):
for i in range(n):


1. What does this function display if we call it as follows:

mystery(4, 6)

2. What does mystery(k, n) display, in general, if k and n are positive integers?

3. Using python3, see what happens if we replace the print function in mystery with a return statement instead:

def mystery1(k, n):
for i in range(n):


Store the function in a file, then load it in python3 and call it with different positive integers and observe the results. In general, if k and n are positive integers, what does mystery1 return? What does this tell you about how the return statement works?

4. Now again using python3, see what happens if we indent the mystery1 function differently so that the return is aligned with for as seen below:

def mystery2(k, n):