Ass 3
Ass 3
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