Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON 3 ** USE RECURSION! ** Use no for loops or comprehensions (which include for loops) in your code. Do not call the min or

PYTHON 3

**USE RECURSION! **Use no for loops or comprehensions (which include for loops) in your code. Do not call the min or max or sorted functions or the sort method on lists. If you use local variables, each can be assigned a value only once in a call, and it cannot be re-assigned or mutated; try to use no local variables (except where they are mentioned in hints). Of course, do not mutate any parameters. 1. Define a recursive function named separate; it is passed a predicate and a list; it returns a 2- tuple whose 0 index is a list of all the values in the argument list for which the predicate returns True, and whose 1 index is a list of all the values in the argument list for which the predicate returns False.

Calling separate((lambda x:x>=0),[1,-3,-2,4,0,-1,8]) returns ([1,4,0,8],[-3,-2,-1]). Actually, the values in the returned lists can be in any order, but this order leads to simpler code. Hint: For the non-base case, first call separate recursively and bind the resulting 2-tuple (and never change that binding); then looking at the first value in the argument list, determine what result to return, using the binding: one index in the 2-tuple will remain the same, the other will include first value in the argument list. You can use + to concatenate two lists to create a third, but cannot mutate any list (e.g., no calls to append).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions