Python在嵌套列表递归中获得第二个最小值

时间:2017-02-27 01:37:43

标签: python list recursion nested

second_smallest(input_list)函数必须从嵌套列表列表中返回第二个最小值。函数 MUST NOT 多次通过列表(不能展平再继续),必须使用默认的内置python函数(无导入),必须使用递归和没有LOOPS 。传递给函数的列表可以是以下形式:

>>> [1,2,3]
>>> [[1,2],3]
>>> [[1],2,3]
>>> [[],1,2,3]
>>> [[1],[2],[3]]
>>> [1,2,3,2,[4,5],[]]

所以input_list可以是所有这些形式,所有这些的返回应该是2

>>> [1,1,2,3]

会返回1

>>> second_smallest([[1],[2]])

有效,但

>>> second_smallest([1])

不是

我现在拥有的是:

def second_smallest(numbers):
    '''(list of int) -> int
    This function takes in 1 parameter, input_list, and returns the second
    smallest number in the list.
    '''
    # if the length of numbers is equal to 2, then set result equal to the
    # second element if the first is less than or equal to second, otherwise
    # set result equal to the first element
    if(len(numbers) == 2):
        if(numbers[0] <= numbers[1]):
            result = numbers[1]
        else:
            result = numbers[0]
    # if the length of numbers is greater than 2, then set result equal to
    # second_smallest_help of the first to second last element in numbers if
    # first is less than or equal to last and last is greater than or equal     to
    # second, otherwise, set result equal to second_smallest of the last to the
    # second last
    else:
        if(numbers[0] <= numbers[-1] >= numbers[1]):
            result = second_smallest(numbers[:-1])
        else:
            result = second_smallest([numbers[-1]] + numbers[:-1])
    return result

但此代码仅适用于未嵌套的列表。那么我如何调整我的实现(或完全改变)以解决这个问题呢?

我的方法是检查当前块的递归深度,有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

当您浏览列表中的条目时,请检查它们是否为列表。如果它们是列表,则以递归方式遍历列表。类似的东西:

def second_smallest(input):
    two_smallest_entries = [None, None]

    def encountered(entry):
        if two_smallest_entries[0] is None or two_smallest_entries[0] >= entry:
            two_smallest_entries[1] = two_smallest_entries[0]
            two_smallest_entries[0] = entry
        elif two_smallest_entries[1] is None or two_smallest_entries[1] >= entry:
            two_smallest_entries[1] = entry

    def traverse(input):
        for entry in input:
            if type(entry) == list:
                traverse(entry)
            else:
                encountered(entry)

    traverse(input)
    return two_smallest_entries[1]

def test(correct, input):
    print second_smallest(input) == correct

test(2, [1,2,3])
test(2, [[1,2],3])
test(2, [[1],2,3])
test(2, [[],1,2,3])
test(2, [[1],[2],[3]])
test(2, [1,2,3,2,[4,5],[]])
test(1, [1,1,2,3])
test(4, [6, 4, [4, 4, 3]])

答案 1 :(得分:2)

好的,如果禁止循环,我们可以平分:

def second(l):
    n = len(l)
    if n >= 2:
        f1, s1 = second(l[:n//2])
        f2, s2 = second(l[n//2:])
        if f1 is None:
            return f2, s2
        elif f2 is None:
            return f1, s1
        elif f1 < f2:
            return f1, (f2 if s1 is None or s1 > f2 else s1)
        else:
            return f2, (f1 if s2 is None or s2 > f1 else s2)
    if n == 0:
        return None, None
    elif isinstance(l[0], list):
        return second(l[0])
    else:
        return l[0], None

def master(l):
    return second(l)[1]