The file circles.txt
contains measurements of circle radii. Your task is to write a script that reports the average area of the circles. You will not use the numpy
mean
function. Here are the requirements:
circles.txt
, read in the data, and convert the data to floats.myave
, that computes the average of the areas of the circles. At the very least, myave
should accept the list of radii as one argument and the circle function that you wrote in step 2 as another argument. There are other ways of doing this task, but I want you to do it this way.The goal of this problem is to write a simple bank account withdraw system. The problem is based off of one in Structure and Interpretation of Computer Programs.
Instructions: Do each part in a different cell block and clearly label each part.
Write a closure to make withdraws from a bank account. The outer function should be accept the initial balance as an argument (I'll refer to this argument as balance
in this problem statement, but you can call it whatever you want). The inner function should accept the withdraw amount as an argument and return the new balance.
NOTE1: For this part, do not try to reassign balance
in the inner function. Just see what happens when you return a new balance. You can store the new balance in a new name (call it new_bal
if you want) or just return the new balance directly.
NOTE2: You may want to check for basic exceptions (e.g. attempting to withdraw more than the current balance).
Once you write your functions, demo them in your notebook as follows:
wd = make_withdraw(init_balance)
wd(withdraw_amount)
wd(new_withdraw_amount)
You should observe that this does not behave correctly. Why not?
You can fix things up by updating balance
within the inner function. But this won't work. Try it out and explain why it doesn't work. Try to use the language that we used in lecture. Hint: Python Execution Model.
Now, make just one small change to your code from Part 2. Declare balance
as a nonlocal variable using the nonlocal keyword. That is, make the first line of the inner function, say nonlocal balance
. Here's some information on the nonlocal
statement: nonlocal
.
Now test things out like you did in Part 1. It should be behaving correctly now.
Finally, visualize your code with Python Tutor and embed your visualization in your notebook. Pay attention to the variable balance
.
Let's return to the data from Problem 1. Write two functions: 1.) The first function should return the average circle area (you can re-use the one you already wrote if you'd like, but you might need to update it slightly for this problem) and 2.) The second function should just use numpy
to compute the average.
Write a decorator to time the evaluation of each function. You can use the timing decorator from lecture.
circles.txt
, compute the area from each point, and store that area in an array. Do you know why this is more fair? Also, try to not use append
. Instead, preallocate space for your area
list/array.my_ave
function should accept your areas data as a list. Remember, to allocate a list you should do [0.0]*N
: if you use such a construct your list will will be filled in with zeros.np_ave
function should accept your areas data as a numpy
array. To allocate a numpy
array do areas_np = np.zeros(len(radii))
.Write a decorator to check if a quantity returned from a function is positive. An exception should be raised if the quantity is not positive.
Write three functions and decorate them with your decorator:
Show that your decorator behaves correctly. That is, for each function, show two cases (where applicable):