- A1 Write A Recursive Function Myfactorial N For Computing The Factorial Of A Non Negative Integer Hint Look At The E 1 (131 KiB) Viewed 50 times
A1 Write a recursive function myfactorial(n) for computing the factorial of a non-negative integer. (Hint: look at the e
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
A1 Write a recursive function myfactorial(n) for computing the factorial of a non-negative integer. (Hint: look at the e
A1 Write a recursive function myfactorial(n) for computing the factorial of a non-negative integer. (Hint: look at the examples myfunction and anotherfunction at the start of Chapter 1 and think about how to adapt them.) A2 If an input other than a non-negative integer is entered into your function myfactorial, the process will never halt. Create a new function safefactorial which behaves identically to myfactorial on non-negative integers, but raises a ValueError when other values are entered. A3 Use safefactorial to write a function mybinom(n,r) that re- turns the binomial coefficient A4 Use mybinom(n,r) to write a function hockeystick(n,r) that n computes H(n,r) = (-) =r The next part of the coursework is about searching a list for a partic- ular value. Of course again Python has an inbuilt process, in, for doing this. For instance: x in thislist. You should not use this method! B1 Write a function binarysearch(mylist, myvalue) that im- plements the recursive binary search algorithm described in 1.4.1 above. You function should take as input a sorted list of numbers mylist and a number myvalue, and output True if myvalue is in mylist and False otherwise. For instance on ([0,1,4,7], 4) it should output True and on ([5,8,11], 2) it should output False. B2 Write a function linearsearch(mylist, myvalue), that has the same inputs and outputs as above, but works by looping through the items in the list one at a time and checking each in turn until it either finds myvalue or reaches the end of the list. Recall that the power set of a set X, is the set of all subsets of X. For example, the power set of {1,2} is {0,{1}, {2}, {1,2}}. C Write a recursive algorithm mypowerlist (n) that, given a nat- ural number n as input, returns the "powerlist" (i.e. the power- set expressed as a list) of {1,...,n}. So the output of mypowerlist (2) should be the following list of sets: [set(),{1}, {2}, {1,2}].