all_combos_of_length_3

 avatar
unknown
python
a year ago
617 B
5
Indexable
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)
Editor is loading...
Leave a Comment