Учитывая список, я хочу написать функцию, которая будет возвращать все увеличивающиеся подпоследовательности списка.Порядок не имеет значения.
Например, inc_subseqs ([1, 3, 2]) -> [[], [1], [1, 2], [1, 3], [2],[3]]
Я нашел способ, подобный следующему:
def insert_into_all(item, nested_list):
"""Assuming that nested_list is a list of lists, return a new list
consisting of all the lists in nested_list, but with item added to
the front of each.
>>> nl = [[], [1, 2], [3]]
>>> insert_into_all(0, nl)
[[0], [0, 1, 2], [0, 3]]
"""
return [[item] + el for el in nested_list]
def inc_subseqs(s):
"""Assuming that S is a list, return a nested list of all subsequences
of S (a list of lists) for which the elements of the subsequence
are strictly nondecreasing. The subsequences can appear in any order.
>>> seqs = inc_subseqs([1, 3, 2])
>>> sorted(seqs)
[[], [1], [1, 2], [1, 3], [2], [3]]
>>> inc_subseqs([])
[[]]
>>> seqs2 = inc_subseqs([1, 1, 2])
>>> sorted(seqs2)
[[], [1], [1], [1, 1], [1, 1, 2], [1, 2], [1, 2], [2]]
"""
def subseq_helper(s, prev):
if not s:
return [[]]
elif s[0] < prev:
return subseq_helper(s[1:], prev)
else:
a = subseq_helper(s[1:], s[0]) # with first
b = subseq_helper(s[1:], prev) # without first
return insert_into_all(s[0], a) + b
return subseq_helper(s, 0)
Однако я не понимаю, как работает другая часть subseq_helper
и общий поток алгоритма?