CSC 148 H1, Fall 2013

Exercise 1

Due: by 11:59 p.m. on Friday 20 January 2013

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 L. Be careful to get this right or your submission will be considered incorrect!


Part A

Submit a file called "e1a.py" containing a function called "square_root" to MarkUs. Your function must take two parameters, x and eps, and it must return one value, the square root of x to within an "accuracy" of eps. ("Accuracy" is defined below.) For example, 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 eps. (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 math.sqrt. Do not use it. In fact, do not include the string "sqrt" or 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 ** (exponentiation operator). 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.


Part B

Submit a file called "e1b.py" containing two Python classes, "Toy" and "Dog" to MarkUs. The idea is that a Dog can play with a Toy.

A Toy has no instance variables and one method, play, which prints "Squeak!" (with the exclamation mark and the capital 'S', without spaces, and with a newline at the end). The constructor for Toy (its __init__ method) takes no parameter (except the usual self) and does nothing (so it can probably be omitted).

A Dog has one instance variable, its name, which must be provided as a parameter to the constructor. It has two methods:

We can change what your Toy class does, so don't build the "Squeak!" into your Dog class's play method! Instead, make an appropriate method call.