Question: Follow the instructions in the program. Use recursion to solve each problem. Uncomment the print statementsBonus points for mergeSort. This assignment is an exercise in
Follow the instructions in the program. Use recursion to solve each problem. Uncomment the print statementsBonus points for mergeSort.
This assignment is an exercise in recursion. Write each function recursively. Don't use built in functions.
Many of these problems have easy solutions in Python that don't require a recursive solution. Don't go there. I'm trying to get you to think recursively, so give them a try.
Bonus points for #10
Function 1 Write a function called makeSeqList which accepts two parameters (x y) and returns the list containing x, x+1, x+2,...,y. You can assume that x and y are integers and x <= y. Function 2 Write a function called makeWholeList which accepts one parameter (x) and returns a list containing 1, 2, 3, ..., x. You may assume that x is an integer and 1 < x. Function 3 Write a function called findIthElement which accepts a list as the first parameter and an integer, say i, as the second parameter, and which returns the element in the ith location of the given list. Return false if the list does not have an ith element. For example, findIthElement([8,9,6,3] 3) should return 6. Function 4 Write a function called splitList which is passed a list with an even number of items,and which returns two lists. The first list contains the items in the even positions (in order) and the second list contains the elements in the odd positions (in order). Example splitList([1,10,2,20,3,30,4,40,5,50]) returns a pair ([1,2,3,4,5],[10,20,30,40,50]) Function 5 Write a function called listLength which accepts a list as a parameter and which returns the number of items in the list. The function should work for empty lists. Function 6 Write a function called reverseList which accepts a list as a parameter and that returns the original list in reverse order. Function 7 Write a minElement and a maxElement function. Each function is passed a list of numbers and returns the appropriate number. You may assume that the parameter list is non-empty. Function 8 Write an even and an odd function. Each function is passed a list. The even function returns the list of elements at positions 2, 4 , 6, ... as a list. The odd function returns the list of elements at positions 1, 3, 5, ... as a list. For example even([8,3,5,4,6,9,1]) would return the [3,4,9] and odd ([8,3,5,4,6,9,1]) would return the [8,5,6,1]. Function 9 Write a function merge which is passed two lists (representing sorted lists) and that returns the single list obtained by merging the given lists. For example, (merge [1,4,5,9,19] [2,6,7,10]) returns the [1,2,4,5,6,7,9,10,19]. Function 10 Write a mergeSort function which is passed a list and which returns the list in sorted order from smallest number to largest. Use even and odd to split the original list into two parts. Sort them and merge them using the functions you have already written. ''' def makeSeqList(x,y) : if x == y : return [x] else : return makeSeqList(x,y-1) + [y]
''' print(makeSeqList(1,10)) print(makeWholeList(10)) print(findIthElement([10,20,30,40,50,60],4)) print(splitList([1,2,3,4,5,6,7,8,9,10])) print(listLength([1,2,3,4,5])) print(reverseList([1,2,3,4,5,6,7,8,9,10])) print(minElement([10,19,41,22,7,99,5,82,3,77,66,33])) print(maxElement([10,19,41,22,7,99,5,82,3,77,66,33])) print(even([1,2,3,4,5,6,7,8,9,10,11,12,13])) print(odd([1,2,3,4,5,6,7,8,9,10,11,12,13])) print(merge([1,4,9,20,33,44],[2,3,5,6,7,10,15,16,22,25])) #bonus print(mergeSort([8,3,7,19,37,21],[1,9,3,6,33,99,66,22])) '''
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
