Please identify each problem as shown here, with a two-letter code prefixing the exercise number.
MX2.1: Because computer hardware is based on binary arithmetic, the operations of doubling a number, cutting one in half, and testing whether a number is even are very efficient. Hardware for multiplying by integers other than 2 is often based on the approach described in this problem. You are to use a while loop to write a Python procedure for multiplying two nonnegative integers, n and m, based on the following ideas:
Keep track of three integers, a, b, and c, which will have the property that a*b+c equals n*m. Each step of the iteration will change the values of a, b, and possibly c, but will do so in a way that keeps the quantity a*b+c unchanged; it will remain equal to our desired product, n*m.
Before the loop starts, initialize c to 0 and initialize a and b to n and m. That way the equality will hold initially.
Each time through the loop, a will decrease toward zero. The loop can stop when a is equal to 0. At that point a*b+c is equal not only to our desired product, but also to c, so you can return c.
There are two ways that a can decrease in the loop body. If a is even, then you should cut it in half and double b. That will leave a*b+c unchanged and will make rapid progress on decreasing a. But if a is odd, then you can just decrease it by 1 and add b into c.
MX2.2: Write a Python procedure, using a while loop, which calculates an approximation to the golden ratio; your procedure will be given one argument, the maximum allowable error in the approximation. You should use the following approach:
You will keep track of a current approximation (which is improved each time through the loop) and a conservative estimate of the error in that approximation. Initially, the approximation is 1.0 and the the error estimate is also 1.0, meaning that the true value of the golden ratio is known to lie between 0.0 and 2.0.
Each time through the loop, the two quantities are updated as follows:
The new error estimate is the old error estimate divided by the square of the old approximation to the golden ratio.
The new approximation to the golden ratio is one more than the reciprocal of the old approximation.
I'm leaving you to figure out how the loop should end.
TP7.4, that is, exercise 4 in chapter 7 of Think Python. If the user immediately types 'done' to the first prompt,
your function eval_loop should return None.
TP8.7, that is, exercise 7 in chapter 8 of Think Python
TP8.11, that is, exercise 11 in chapter 8 of Think Python. You may assume in each case that the procedure is passed a nonempty string as s.
MX2.3: Write a Python procedure, cummulativelySum, which takes a list of numbers (of any length) and modifies it as follows. Each element of the list is replaced by the sum of that element and all the numbers that originally preceded it in the list. Here is an example:
>>> a = [10,5,20] >>> cummulativelySum(a) >>> a [10, 15, 35]
In this example, 10 is the sum of just 10, 15 is the sum of 10 and 5, and 35 is the sum of 10, 5, and 20. Notice also that the cummulativelySum procedure did not return any value; instead it modified the list it was given.
MX2.4: Load the following definitions into Python:
def make_table_1(n,m):
table = []
for i in range(n):
row = [None]*m
table.append(row)
return table
def make_table_2(n,m):
table = []
row = [None]*m
for i in range(n):
table.append(row)
return table
table_1 = make_table_1(3,4)
table_2 = make_table_2(3,4)
Now answer the following questions:
Print out table_1 and table_2. How would
you describe the results?
Execute the following two statements:
table_1[2][1] = 17 table_2[2][1] = 17
Now print out table_1 and table_2 again. How would
you describe the results?
Draw list diagrams for table_1
and table_2 that help explain the observed results.
By experimenting with Python, determine whether the following
procedure behaves like make_table_1 or
like make_table_2:
def make_table_3(n,m):
return [[None]*m]*n
By experimenting with Python, determine whether the following
procedure behaves like make_table_1 or
like make_table_2:
def make_table_4(n,m):
return [[None]*m for i in range(n)]