Summation number to match ouput

Hi all,

I am trying to add combination of numbers in way that it matches expected output

For example if List A has 2,3,5,1,6
and

List B has 3,8,7

Then List A should add combination of numbers and arrive close to expected List B

So From List A index 0+ index 3 = index 0 of list B

Added sample image and script for reference. Please help me with this

Summation to match output.gh (14.8 KB)

@PeterFotiadis @Joseph_Oster Please help

The most intuitive implementation would be to use a brute force algorithm. Under the assumption that list A is always larger than list B, you can check for all the possible ways to divide list A into a number of N sub-lists, where N is equal to the number of elements of B.

Then sum over the sub lists and compare to B

Note: this brute force algorithm is only feasible when the number of elements in list a is not terribly large, since we are dealing with permutations.

Yes it is not more than 10… can you please share the script

In this:

You asked an indicative C# that does it. So if you know how to code this “new” request of yours is just a variation of the solution provided.

us this to split partitions( from here)

def sorted_k_partitions(seq, k):
    """Returns a list of all unique k-partitions of `seq`.

    Each partition is a list of parts, and each part is a tuple.

    The parts in each individual partition will be sorted in shortlex
    order (i.e., by length first, then lexicographically).

    The overall list of partitions will then be sorted by the length
    of their first part, the length of their second part, ...,
    the length of their last part, and then lexicographically.
    """
    n = len(seq)
    groups = []  # a list of lists, currently empty

    def generate_partitions(i):
        if i >= n:
            yield list(map(tuple, groups))
        else:
            if n - i > k - len(groups):
                for group in groups:
                    group.append(seq[i])
                    yield from generate_partitions(i + 1)
                    group.pop()

            if len(groups) < k:
                groups.append([seq[i]])
                yield from generate_partitions(i + 1)
                groups.pop()

    result = generate_partitions(0)

    # Sort the parts in each partition in shortlex order
    result = [sorted(ps, key = lambda p: (len(p), p)) for ps in result]
    # Sort partitions by the length of each part, then lexicographically.
    result = sorted(result, key = lambda ps: (*map(len, ps), ps))

    return result

note: seq is A, k is len(B)
afterwards, sum each and compare to B