简体   繁体   中英

recursion function that take a list and return a sorted list

I am working on the sort function, which is a recursion function that takes a list and return a sorted list.(Using recursion is a requirement).

def separate(p : callable, l : [object]) -> ([object],[object]):
    if l == []:
        return([],[])
    else:
        if p(l[0]):
            return (separate(p, l[1:])[0]+[l[0]],separate(p, l[1:])[1])
        else:
            return (separate(p, l[1:])[0], separate(p, l[1:])[1]+[l[0]])

def sort(l : [object]) -> [object]:

    z = separate((lambda x: [y > x[0] for y in x]),l)
    return sort(z[0]) + sort(z[1])

separate function 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. for example:

separate((lambda x:x>=0),[1,-3,-2,4,0,-1,8])

returns

([1,4,0,8],[-3,-2,-1])

the sort function call separate function to separate the list into two list. the lambda function in sort is to compare whether the element in the list is bigger than the first element in the list. Then, Recursively call sort on each of these two lists and return a single list that contains the sorted values in the first list (the smaller ones), followed by the value used to separate the list, followed by the sorted values in the second list (the larger ones)

sort([4,5,3,1,2,7,6]) would call separate first, to compute the lists [3,1,2] and [5,7,6] (note that 4, the first value in the list, used to do the separation, is not in either list) which when sorted would be [1,2,3] and [5,6,7], resulting in the returned list

[1,2,3,4,5,6,7] 

(the 4 is put in the result list, in the correct place).

I have tested my separate function and it is working correctly.

Now, I try to write the function sort and it shows that 'int' object is not iterable, I added the error I got below:

29 # Test sort
30 *Error: sort([1,2,3,4,5,6,7]) raised exception TypeError: 'int' object is not iterable
31 *Error: sort([7,6,5,4,3,2,1]) raised exception TypeError: 'int' object is not iterable
32 *Error: sort([4,5,3,1,2,7,6]) raised exception TypeError: 'int' object is not iterable
33 *Error: sort([1,7,2,6,3,5,4]) raised exception TypeError: 'int' object is not iterable
36 *Error: l = sort(l) raised exception TypeError: 'int' object is not iterable
37 *Error: l -> [8, 5, 9, 4, 29, 28, 11, 19, 7, 15, 25, 6, 12, 10, 22, 17, 21, 3, 27, 23, 1, 20, 13, 2, 30, 16, 26, 24, 18, 14] but should -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

I am not sure why I got this error, can someone help me to fix my sort function? Many thanks.

First, your separate() function is a mess. You recompute separate(p, l[1:]) , just to index it twice, rather than use a local variable:

return (separate(p, l[1:])[0] + [l[0]], separate(p, l[1:])[1])

Your sort() routine needs a base case; you can't get the first element of an empty list; you're grabbing the first element of the wrong list; you're violating the logic of your own predicate function design by having it process an entire list instead of one element!

Here's a rework of your code that attempts to solve the above issues and clean up the style a bit:

def separate(predicate: callable, array: [object]) -> ([object], [object]):

    if not array:
        return list(), list()

    positive, negative = separate(predicate, array[1:])

    if predicate(array[0]):

        return [array[0]] + positive, negative

    return positive, [array[0]] + negative


def sort(array: [object]) -> [object]:

    if not array:
        return array  # avoid next statement if array is empty

    first = array[0]

    lesser, greater = separate(lambda x: x < first, array[1:])

    return sort(lesser) + [first] + sort(greater)


print(sort([4, 5, 3, 1, 2, 7, 6]))

As desired, it returns:

[1, 2, 3, 4, 5, 6, 7]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM