# lab11.py
# YOUR NAME(S) AND NETID(S) HERE
# DATE COMPLETED HERE
"""Loop functions for lab 11"""
def numberof(thelist, v):
"""Returns: number of times v occurs in thelist.
Precondition: thelist is a list of ints
v is an int"""
count = 0 # Accumulator
k = 0 # Loop variable
# IMPLEMENT A WHILE LOOP HERE
# END WHILE LOOP
# Return result
return count
def replace_copy(thelist,a,b):
"""Returns: a COPY of thelist but with all occurrences of a replaced by b.
Example: replace([1,2,3,1], 1, 4) is [4,2,3,4].
Precondition: thelist is a list of ints
a and b are ints"""
copy = [] # Accumulator
k = 0 # Loop variable
# IMPLEMENT A WHILE LOOP HERE
# END WHILE LOOP
# Return result
return copy
def replace(thelist,a,b):
"""MODIFY thelist so that all occurrences of a replaced by b.
This function should have NO RETURN STATEMENT
Example: if a = [1,2,3,1] then a becomes [4,2,3,4] after the
function call replace(a,1,4).
Precondition: thelist is a list of ints
a and b are ints"""
# IMPLEMENT A WHILE LOOP HERE
# END WHILE LOOP
pass
def exp(x,err=1e-6):
"""Returns: the value of (e ** x) to with the given margin of error.
Do NOT return (math.E ** x). This function is more exact that that answer.
The value (e ** x) is given by the Power Series
1 + x + (x ** 2)/2 + (x ** 3)/3! + ... + (x ** n)/ n! + ...
We cannot add up infinite values in a program. So we APPROXIMATE (e ** x)
by choosing a value n and stopping at that:
1 + x + (x ** 2)/2 + (x ** 3)/3! + ... + (x ** n)/ n!
The error of this approximation is
abs( (x ** (n+1))/(n+1)!)
which we want less than err. So to compute e ** x, we just keep computing
term = (x ** n)/ n! in a loop until this value is less than our error. If it
is not less than the error, we add it to the accumulator, which we return at
the end.
Hint: (x**(n+1))/(n+1)! == (x**n)/n! * x/(n+1)
Use this fact to simplify your loop.
Precondition: x, err are numbers, err > 0."""
approx = 0.0 # Approximation of e ** x
term = 1.0 # 1 is the first term in the Power Series
n = 0 # term 1 corresponds to (x ** 0)/0!
# IMPLEMENT A WHILE LOOP HERE
# END WHILE LOOP
# Return result
return approx