Вы находитесь на странице: 1из 2

CSL101 - Assignment 4

February 11, 2013


Due Date: Saturday Mar 2, 11:55pm
Total Points: 50
1. Consider the Taylor expression for approximating the value of cos(x) around x = 0 as detailed
in Minor 1.
cos(x) = 1 x2 /2! + x4 /4! + + (1)k x2k /(2k)! +
(a) Write a Python program which inputs n (index) and v (value) and evaluates the above
expression up-to the nth term at x = v using the idea of iterative computations. (4
points)
(b) Write another program to achieve the above functionality using the idea of recursive
(deferred) computations. (Hint: For this part, you may first want to calculate the nth
term recursively and then write another function to calculate the sum.) (4 points)
Note: You will need to do operations over real valued numbers (as opposed to integers) to
calculate the correct value of the expression above. Read about the float type in Python and
how to convert an integer expression into a float before you implement this program.
2. Consider the representation of a polynomial as described in Assignment 1. Instead of using
a number n to represent the coefficients of a polynomial, we will be using a list i.e. the
k th element of the list represents the coefficient of the term xk in the polynomial. Write a
Python program which inputs two lists of numbers L1 and L2 and a value v from the user
and performs the following functionalities. Here L1 and L2 represent the coefficients of two
polynomials as described above. v represents the value at which we would like to evaluate the
polynomials.
Note: For the parts below, you should write the functions using purely recursive computations
(i.e. no tail recursion or iterative computations).
(a) Write a recursive Python function which inputs a list L representing a polynomial and
returns the string representation of the polynomial e.g. if L = [1, 2, 3, 5] your function
should return the string 5x3 + 3x2 + 2x + 1. (4 points)
(b) Write a recursive function which inputs a polynomial (represented as a list), a value v
and evaluates the polynomial at x = v. (4 points)
(c) Write a recursive function which inputs two polynomials and outputs another polynomial
(represented as a list) which is the addition of the two input polynomials. (4 points)
1

(d) Write another recursive function which inputs two polynomials and outputs another
polynomial (again represented as a list) which is the multiplication of the two input
polynomials. (6 points)
(e) Run your program on two input lists of degree 5, and verify that your addition and
multiplication implementations are correct by evaluating the polynomials separately
at v = 3 and adding/multiplying the result and then, comparing the result by first
adding/multiplying the polynomials using your functions and then evaluating them at
v = 3. Include your test case in a separate write-up file (2 points).
3. Write a recursive Python program which inputs a list L from the user and reverses the order
of elements in the input list L. What is the time complexity of your program? Argue. (6
points)
4. Consider a list of strings which stores the names of people. We would like to write a program
to check whether a given input name matches any of the names present in the list. Note that
here, we are not only looking for exact matches, but also substring matches i.e. whether the
input name is a substring (or exact match) of any of the names in the list. For example, if
the input name is Anupam and your list has an element Anupam Sethi then you should
declare it a match. Write a Python program which inputs a list of names and a name to be
matched from the user and achieves the above functionality. (6 points)
5. Consider the frog jumping example from Minor 1. Two frogs are sitting at the bottom of a
flight of 10 steps and debating in how many ways can they jump up the stairs. They can
jump one, two or three steps at a time. For example, they can cover the 10 steps by jumping
(3, 3, 3, 1) or (2, 3, 3, 2) or other suitable combinations. Their mathematics is not very strong
(being frogs) and they approach you for help. Note that (3, 3, 3, 1) is distinct from (1, 3, 3, 3)
and likewise and that we only want to count the number of solutions and not report solutions.
(a) Write a recursive program (no tail-recursion) to give a general solution to the above
problem for n steps. (4 points)
(b) Write a tail-recursive program to solve the same problem. (4 points)
(c) Try out the two solutions written above for n = 10, 20, 30 100. Verify that two programs return the same solution (you should report the answers returned by the two
versions for each n). What happens to the computational time? Comment. (2 points)
Note: You should include all your time complexity analysis and test cases in a separate file ass4writeup.txt

Вам также может понравиться