Google Foobar Challenge 3 - Find the Access Codes
<h1>Find the Access Codes</h1>

<p>Write a function answer(l) that takes a list of positive integers l and counts the number of "lucky triples" of (lst[i], lst[j], lst[k]) where i &lt; j &lt; k.  The length of l is between 2 and 2000 inclusive.  The elements of l are between 1 and 999999 inclusive.  The answer fits within a signed 32-bit integer. Some of the lists are purposely generated without any access codes to throw off spies, so if no triples are found, return 0. </p>

<p>For example, [1, 2, 3, 4, 5, 6] has the triples: [1, 2, 4], [1, 2, 6], [1, 3, 6], making the answer 3 total.</p>

<h1>Test cases</h1>

<p>Inputs:
    (int list) l = [1, 1, 1]
Output:
    (int) 1</p>

<p>Inputs:
    (int list) l = [1, 2, 3, 4, 5, 6]
Output:
    (int) 3</p>

<h1>My Attempt</h1>

<pre><code>from itertools import combinations

def answer(l):
    if len(l) &lt; 3:
        return 0
    found = 0
    for val in combinations(l,3):
        # Ordering Check
        if (val[0] &lt;= val[1] &lt;= val[2]) != True:
            continue
        # Answer Size Check against size of signed integer 32 bit
        if int(val[0].__str__() + val[1].__str__() + val[2].__str__()) &gt; 2147483647:
            continue
        # Division Check
        if (val[1] % val[1] != 0) or (val[2] % val[1] != 0):
            continue
        # Increment 'found' variable by one
        found += 1
    return found
</code></pre>

<p>Thing is: you let that library method <strong>combinations</strong> do all the "real" work for you. </p>

<p>And of course: normally that is exactly the way to go. You do <strong>not</strong> want to re-invent the wheel when there is an existing library function that gives you what you need. Your current code is pretty concise, and good to read (except maybe that you should call your list, well, "list", but not "l").</p>

<p>But this case is different: obviously, most of the execution time for this program will happen in that call. And it seems that google thinks whatever this call is doing .. can be done <strong>faster</strong>. </p>

<p>So, the answer for you is: you actually want to re-invent the wheel, by rewriting your code in a way that is <strong>better</strong> than what it is doing right now! A first starting point might be to check out the source code of <strong>combinations</strong> to understand if/how that call is doing things that you do not need in your context.</p>

<p><em>Guessing</em>: that call creates <strong>a lot</strong> of permutations that are not ideal. All of that is <strong>wasted</strong> time. You want to step back and consider how to build those lucky triples from your input <strong>without</strong> creating a ton of not so lucky triples!</p>

java python combinations