all_combos_of_length_3

mail@pastecode.io avatar
unknown
python
a month ago
617 B
0
Indexable
Never
def printCombos(a, x):
    if len(a) == 0 or x > len(a):
        return

    buffer = [-1, -1, -1]
    printCombosHelper(a, buffer, 0, 0)

def printCombosHelper(a, buffer, startIndex, bufferIndex):
    # termination cases - buffer full
    if bufferIndex == len(buffer):
        print(buffer)
        return

    if startIndex == len(a):
        return

    # find candidates that go into current buffer index
    for i in range(startIndex, len(a)):
        # place item into buffer
        buffer[bufferIndex] = a[i]
        # recurse to next buffer index
        printCombosHelper(a, buffer, i + 1, bufferIndex + 1)
Leave a Comment