There are two unrelated parts to this exercise.
For each part, there is a single file you must submit — make sure that you read the submission instructions carefully!
The file names both start with "
e1" — that's lowercase letter "
e" followed by
the digit one,
NOT the lowercase letter
Be careful to get this right or
your submission will be considered incorrect!
Submit a file called "
a function called "
Your function must take two parameters,
it must return one value,
the square root of
to within an "accuracy" of
("Accuracy" is defined below.)
square_root(2, 0.01) should return
a number between about 1.404 and 1.424.
Calculate the square root using Newton's method. This is an iterative method: it starts with a guess at the answer and improves the guess over and over until it is "close enough". If we are looking for the square root of x, and the current guess is this_guess, then the next guess is given by the following expression:
next_guess = 0.5 × (this_guess + x/this_guess)
This becomes the new value for this_guess and
the whole process is repeated.
Your function should start with a first guess of 1.0, and
continue until the difference between two successive guesses
is less than the parameter
(That's the definition of "accuracy".)
If that takes more than 10 iterations,
your function should stop and return -1.0.
That is not a very satisfactory way to indicate failure, but
we are not going to give your function any very difficult cases, and
we don't want to ask you to use exceptions yet.
There is a library function
Do not use it.
In fact, do not include the string "
the string "
import" anywhere in your file, even
as part of another word such as the English "important" or
the possibly Klingon "besqrted".
Also, do not use the built-in
The idea is to calculate the square root using only
simpler arithmetic operations
that are present in every programming language,
not to try to find some roundabout way of
computing the answer!
Be aware that all the values in this part of the exercise are floats. If you accidentally use integers, you may run into serious mysterious troubles, so please be careful to provide decimal parts for numeric constants. (You should use integers where appropriate, of course; for example, count the iterations with an integer.)
Hint: This sounds like a hard problem, but it's surprisingly easy (and surprisingly good, at least for small positive numbers). You're not expected to know Newton's method, though you may have heard of it, but as far as Python programming techniques are concerned, this part of the exercise should be entirely review for you.
Submit a file called "
two Python classes, "
Toy" and "
The idea is that a
Dog can play with a
Toy has no instance variables and one method,
play, which prints "
(with the exclamation mark and the capital 'S', without spaces, and
with a newline at the end).
The constructor for
takes no parameter (except the usual
(so it can probably be omitted).
Dog has one instance variable, its name,
which must be provided as a parameter to the constructor.
It has two methods:
shoutis exactly "
Here, n!" (with the comma, the space and the exclamation mark, and no leading or trailing spaces), where
Dog's name. Otherwise, it returns
play(self, toy, n)prints "
Yip!" (with the trailing space) followed by the output from
toy.play()on the same line. This happens
ntimes, with the
noutputs on separate lines. If
nis negative, it is the same as if it were 0.
We can change what your
Toy class does, so
don't build the "Squeak!" into
Instead, make an appropriate method call.