Practice for Exam 1
The following problems are typical of those that will be on the exam. In addition to answering these questions, you should also review the course's online notes and the solutions to the two assignments. We have also made a study guide.
The following is a formula for computing compound interest: A = P(1+r/n)n·t, where,
- P = principal amount (initial investment),
- r = annual nominal interest rate (e.g., 10% is represented as 0.2),
- n = number of times the interest is compounded per year,
- t = number of years, and
- A = amount after time t.
-
def compoundedAmount(P,r,n,t): """Given the principal amount P, annual interest rate r, number of compounding periods per year n, and number of years t, return the investment amount with compounded interest.""" return P * (1 + float(r)/n)**(n*t)
Complete the following function that takes a list of numbers
[x0, …]
and a number n.
It returns a list that is
[x0n, …]
.
-
def mapExponent(aList,exponent): """Given aList=[x0,...] and a number exponent, returns [x0^exponent,...].""" ### You need to complete this function. ###
-
def mapExponent(aList,exponent): """Given aList=[x0,...] and a number exponent, returns [x0^exponent,...].""" result = [] for x in aList: result.append(x**exponent) return result
To compute the maximum element in a non-empty list, we could just
use the Python
built-in function max
. But, how would we define this
function ourselves? Complete the following function myMax
,
which returns the largest element in its input list. You cannot use the
built-in function max
. You should not sort the list.
-
def myMax(aList): """Returns the largest element in aList. Assumes aList is non-empty.""" ### You need to complete this function. ###
-
def myMax(aList): """Returns the largest element in aList. Assumes aList is non-empty.""" largest = aList[0] for x in aList[1:]: if x>largest: largest = x return largest
Complete the following function that returns the number of positive elements in a list.
-
def countPositives(aList): """Returns the number of positive elements in aList.""" ### You need to complete this function. ###
-
def countPositives(aList): """Returns the number of positive elements in aList.""" count = 0 for x in aList: if x>0: count = count+1 return count
Describe what it means to decompose a problem. Why do we do it?
-
Decomposing a problem is simply breaking is down into smaller pieces. It provides several interrelated advantages: * Each piece is smaller, and thus simpler. * It allows us to think of each subproblem abstractly, independently of each other. * We can reuse each subproblem's solution elsewhere, as needed. * We can modify each subproblem's solution independently.
COMP 130 only:
Explain the benefits of the full functional decomposition of the
second-order predator-prey model plus starvation effects, where the
system was ultimately broken down to a representation using small
functions to calculate Ψ
, Φ
,
partial derivatives, and starvation effects.
-
First, it had the general decomposition benefits as described in the previous problem. Specifically for this problem, this had two benefits. The formulae for the next populations were shorter, and thus easier to read and debug. Also, the formulae used a couple of the functions multiple times, rather than duplicating code. In addition, the separation enforced the fact that most of the system was actually independent of the model (Lotka-Volterra, or its variations) being used. This means that when effects such as starvation are included or modified, they only affect very small portions of the code. This makes it much easier to study the role and effects of these contributions and makes it easier to maintain the overall integrity of the model.