Question: CODE IN PYTHON PLEASE!!!!! This is a programming problem. Create a new Python file named p1.py and write the solution in it. When done, upload
CODE IN PYTHON PLEASE!!!!!
This is a programming problem. Create a new Python file named p1.py and write the solution in it. When done, upload the file by clicking on the button below. Make sure your code works, follows strictly all requirements, and complies with the Python coding style taught in class. Do not change the signature (name, parameter list) of any function, if given. Do not worry if Canvas changes the file name after you upload it. To get credit for this problem the code must NOT use: for loops, while loops, functions from other modules, the map function, or any functions except type(), len(), and list slices. A flat list has elements that are not lists themselves, while a nested list has some elements that are lists. E.g. [1,2,3] is a flat list, while [1, [12], 3], 4] is a nested list. For this problem a list Ist is defined flat if for each element x in lst, type(x) !- list. [] is considered to be flat. The flat version of [1, [I2], 3], 4] is [1, 2, 3, 4] a) Write a recursive function mapflatilst(lst, fun) that takes as parameters a list Ist (assumed to be flat) and a function fun and returns a list with elements from Ist mapped through the function fun. This means that if Ist has elements [x,y,z], the function returns [fun(x), fun(y), fun(z)]. mapflatlst([ ], fun) returns [], the empty list. Example: suppose we have function times2 defined like this: def times2(x): return 2*x Then, mapflatlst([1,2,3,4,5], times2) returns [2, 4, 6, 8, 10] b) Write a recursive function maplst(lst, fun) that takes as parameters a list Ist (that can be nested) and a function fun and returns a flat list with elements from the flattened version of Ist mapped through the function fun maplst I 1, fun) returns [1, the empty list. Example: with function times2 defined like above, maplst([I1]], [2, [3, 4], 5], 6], times2) returns [2, 4, 6, 8, 10, 12]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
