Exactly three adjacent 3 in lists: Difference between revisions

→‎{{header|Python}}: Pruned out a dependency
(→‎{{header|Python}}: Pruned out a dependency)
Line 223:
=={{header|Python}}==
<lang python>'''N instances of N and all contiguous'''
 
from itertools import dropwhile, takewhile
 
 
Line 234 ⟶ 236:
 
def go(xs):
mbifromFirstMatch = findIndexlist(p)dropwhile(xs)
if None is mbi lambda v: not p(v),
return Falsexs
else:))
ns = list(takewhile(p, fromFirstMatch))
rest = [p(x) for x in xs[mbi:]]
rest return n <= fromFirstMatch[len(restns) and all(rest[0:n]) and (
 
not any(rest[n:])
return p(len(ns)) and )(
restnot = [any(p(x) for x in xs[mbi:]]rest)
)
 
return go
Line 261 ⟶ 265:
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# findIndex :: (a -> Bool) -> [a] -> (None | Int)
def findIndex(p):
'''Just the first index at which an
element in xs matches p,
or None if no elements match.
'''
def go(xs):
try:
return next(
i for i, v in enumerate(xs) if p(v)
)
except StopIteration:
return None
return go
 
 
9,655

edits