0% found this document useful (0 votes)
33 views

Ass 3

This document outlines the requirements for Assignment 3 in CSL102. It includes 6 problems to implement various recursive functions: 1) Write programs to evaluate Taylor series approximations of sin(x) recursively and iteratively. 2) Implement digit reduction and summation functions. 3) Write a function to create random lists recursively. 4) Write recursive functions to evaluate, add, and multiply polynomials represented as lists. 5) Write a recursive function to reverse a list and analyze its time complexity. 6) Implement merge sort to sort a list recursively, including functions for merging, sorting, and checking if a list is sorted. Analyze the time and space complexity.

Uploaded by

Jam Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Ass 3

This document outlines the requirements for Assignment 3 in CSL102. It includes 6 problems to implement various recursive functions: 1) Write programs to evaluate Taylor series approximations of sin(x) recursively and iteratively. 2) Implement digit reduction and summation functions. 3) Write a function to create random lists recursively. 4) Write recursive functions to evaluate, add, and multiply polynomials represented as lists. 5) Write a recursive function to reverse a list and analyze its time complexity. 6) Implement merge sort to sort a list recursively, including functions for merging, sorting, and checking if a list is sorted. Analyze the time and space complexity.

Uploaded by

Jam Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSL102 - Assignment 3

February 10, 2012


Due Date: Sunday February 26, 11:50pm Total Points: 160 1. Consider the Taylor expression for approximating the value of sin(x) around x = 0 as detailed in Minor 1. sin(x) = x x3 /3! + x5 /5! + + (1)k+1 x2k1 /(2k 1)! + (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. (10 points) (b) Write another program to achieve the above functionality using the idea of recursive (deferred) computations. (Hint: For this part, you may rst want to calculate the nth term recursively and then write another function to calculate the sum.) (10 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 oat type in Python and how to convert an integer expression into a oat before you implement this program. 2. Implement the reduce(n) and sumOf Digits(n) functions from Minor 1. You should write the complete program which takes an input n from the user and outputs the corresponding one digit value. (10 points) 3. Write a Python function createList(n) which recursively creates a list of size n, appending one element at a time to the list. Your function should take a single argument n. To generate each element of the list, use the random number generator function random.randint(a, b) which returns a random integer between a and b (end points included). For this and subsequent problems, you can x a = 10 and b = 10. (10 points) 4. Consider the representation of a polynomial as described in Quiz 1. Instead of using a number n to represent the coecients of a polynomial, we will be using a list i.e. the k th element of the list represents the coecient of the term xk in the polynomial. Write a Python program which inputs two numbers n and m and a value v from the user and performs the following functionalities. n and m represent the degrees of two distinct polynomials. v represents the value at which we would like to evaluate the polynomials. Create two polynomials of degrees n and m, respectively, whose coecients are randomly generated (you can use the createList(n) function above to generate these polynomials). 1

Note: You should write the following functions using purely recursive computations (i.e. no tail recursion or iterative computations). (a) Write a recursive function which inputs a polynomial (represented as a list), a value v and evaluates the polynomial at x = v. (10 points) (b) Write a recursive function which inputs two polynomials and outputs another polynomial which is the addition of the two input polynomials. (10 points) (c) Write another recursive function which inputs two polynomials and outputs another polynomial which is the multiplication of the two input polynomials. (20 points) (d) Generate two random polynomials of size 3 using the program above. Verify that your addition and multiplication implementations are correct by evaluating the polynomials separately at v = 2 and adding/multiplying the result and then, comparing the result by rst adding/multiplying the polynomials using your functions and then evaluating them at v = 2. Include your test case in a separate write-up le. (10 points) 5. Write a recursive function reverse(L) to reverse the order of elements in the input list L. Generate a random list of size 15 and verify that your output is correct. Include the test case in your write-up. What is the time complexity of your program? Argue. (25 points) 6. In this problem, we will implement an algorithm to sort a list of numbers in ascending order (as was described in one of the questions in Minor 1). The algorithm works by repeatedly splitting a list into two halves, sorting them recursively and then merging the resulting lists (in the right order). The algorithm is known in the Computer Science literature by the name Merge Sort. (a) Write a recursive function checkSorted(L) to check whether the elements in the input list are sorted in the ascending order. (10 points) (b) Write a recursive function merge(L1, L2) which inputs two sorted lists (in ascending order) and returns a new sorted list by merging the two lists in the right order. (10 points) (c) Write a recursive function sort(L) which inputs a list L and returns a new list with the elements of L sorted in ascending order, using the merge sort idea described above. (10 points) (d) Verify that your program behaves correctly on a randomly generated input list of size 15. Include your test case in your write-up. (5 points) (e) Analyze the time complexity of merge and sort functions as written above. (20 points) Note: You should include all your time complexity analysis and test cases in a separate le ass3writeup.txt

You might also like