Skip to content

Instantly share code, notes, and snippets.

@william-silversmith
Last active January 31, 2018 04:52
Show Gist options
  • Save william-silversmith/52c2cc8720197019d83cbb39720b0c87 to your computer and use it in GitHub Desktop.
Save william-silversmith/52c2cc8720197019d83cbb39720b0c87 to your computer and use it in GitHub Desktop.
Basic implementation of COUNTLESS8
from itertools import combinations
from functools import reduce
def countless8(a,b,c,d,e,f,g,h):
"""Extend countless5 to countless8. Same deal, except we also
need to check for matches of length 4."""
sections = [ a, b, c, d, e, f, g, h ]
# PICKN = PICK with N arguments
p2 = lambda q,r: q * (q == r) # PICK2
p3 = lambda q,r,s: q * ( (q == r) & (r == s) ) # PICK3
p4 = lambda p,q,r,s: p * ( (p == q) & (q == r) & (r == s) ) # PICK4
lor = lambda x,y: x + (x == 0) * y # short circuiting logical or
# important to use generator expressions here to reduce peak memory usage
results4 = ( p4(x,y,z,w) for x,y,z,w in combinations(sections, 4) )
results4 = reduce(lor, results4)
results3 = ( p3(x,y,z) for x,y,z in combinations(sections, 3) )
results3 = reduce(lor, results3)
# We can always use our shortcut of omitting the last element
# for N choose 2
results2 = ( p2(x,y) for x,y in combinations(sections[:-1], 2) )
results2 = reduce(lor, results2)
return reduce(lor, [ results4, results3, results2, h ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment