Maximize It!

  • + 0 comments

    I think the important python learning here is that while the docs only give product() examples with two iterables, product() can take an arbitrary list of iterables to produce all combinations. E.g., modifying an example from the docs:

    list(product('ABCD', 'xy', '123'))
    [('A', 'x', '1'), ('A', 'x', '2'), ('A', 'x', '3'), ('A', 'y', '1'), ('A', 'y', '2'), ('A', 'y', '3'), ('B', 'x', '1'), ('B', 'x', '2'), ('B', 'x', '3'), ('B', 'y', '1'), ('B', 'y', '2'), ('B', 'y', '3'), ('C', 'x', '1'), ('C', 'x', '2'), ('C', 'x', '3'), ('C', 'y', '1'), ('C', 'y', '2'), ('C', 'y', '3'), ('D', 'x', '1'), ('D', 'x', '2'), ('D', 'x', '3'), ('D', 'y', '1'), ('D', 'y', '2'), ('D', 'y', '3')]
    

    This then lets us create the things to sum over and test for max all at once.

    It should be noted that product() isn't magical with respect to run time, this is still a polynomial algorithm in O(n^K).