<s> """<STR_LIT>""" <EOL> from itertools import * <EOL> from collections import Iterable <EOL> def take ( n , iterable ) : <EOL> "<STR_LIT>" <EOL> return list ( islice ( iterable , n ) ) <EOL> def tabulate ( function , start = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> return imap ( function , count ( start ) ) <EOL> def consume ( iterator , n ) : <EOL> "<STR_LIT>" <EOL> if n is None : <EOL> collections . deque ( iterator , maxlen = <NUM_LIT:0> ) <EOL> else : <EOL> next ( islice ( iterator , n , n ) , None ) <EOL> def nth ( iterable , n , default = None ) : <EOL> "<STR_LIT>" <EOL> return next ( islice ( iterable , n , None ) , default ) <EOL> def quantify ( iterable , pred = bool ) : <EOL> "<STR_LIT>" <EOL> return sum ( imap ( pred , iterable ) ) <EOL> def padnone ( iterable ) : <EOL> """<STR_LIT>""" <EOL> return chain ( iterable , repeat ( None ) ) <EOL> def ncycles ( iterable , n ) : <EOL> "<STR_LIT>" <EOL> return chain . from_iterable ( repeat ( tuple ( iterable ) , n ) ) <EOL> def dotproduct ( vec1 , vec2 ) : <EOL> return sum ( imap ( operator . mul , vec1 , vec2 ) ) <EOL> def flatten ( listOfLists ) : <EOL> "<STR_LIT>" <EOL> return chain . from_iterable ( listOfLists ) <EOL> def repeatfunc ( func , times = None , * args ) : <EOL> """<STR_LIT>""" <EOL> if times is None : <EOL> return starmap ( func , repeat ( args ) ) <EOL> return starmap ( func , repeat ( args , times ) ) <EOL> def pairwise ( iterable ) : <EOL> "<STR_LIT>" <EOL> a , b = tee ( iterable ) <EOL> next ( b , None ) <EOL> return izip ( a , b ) <EOL> def grouper ( iterable , n , fillvalue = None ) : <EOL> "<STR_LIT>" <EOL> args = [ iter ( iterable ) ] * n <EOL> return izip_longest ( fillvalue = fillvalue , * args ) <EOL> def roundrobin ( * iterables ) : <EOL> "<STR_LIT>" <EOL> pending = len ( iterables ) <EOL> nexts = cycle ( iter ( it ) . next for it in iterables ) <EOL> while pending : <EOL> try : <EOL> for next in nexts : <EOL> yield next ( ) <EOL> except StopIteration : <EOL> pending -= <NUM_LIT:1> <EOL> nexts = cycle ( islice ( nexts , pending ) ) <EOL> def powerset ( iterable ) : <EOL> "<STR_LIT>" <EOL> s = list ( iterable ) <EOL> return chain . from_iterable ( combinations ( s , r ) for r in range ( len ( s ) + <NUM_LIT:1> ) ) <EOL> def unique_everseen ( iterable , key = None ) : <EOL> "<STR_LIT>" <EOL> seen = set ( ) <EOL> seen_add = seen . add <EOL> if key is None : <EOL> for element in ifilterfalse ( seen . __contains__ , iterable ) : <EOL> seen_add ( element ) <EOL> yield element <EOL> else : <EOL> for element in iterable : <EOL> k = key ( element ) <EOL> if k not in seen : <EOL> seen_add ( k ) <EOL> yield element <EOL> def unique_justseen ( iterable , key = None ) : <EOL> "<STR_LIT>" <EOL> return imap ( next , imap ( itemgetter ( <NUM_LIT:1> ) , groupby ( iterable , key ) ) ) <EOL> def iter_except ( func , exception , first = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if first is not None : <EOL> yield first ( ) <EOL> while <NUM_LIT:1> : <EOL> yield func ( ) <EOL> except exception : <EOL> pass <EOL> def random_product ( * args , ** kwds ) : <EOL> "<STR_LIT>" <EOL> pools = map ( tuple , args ) * kwds . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> return tuple ( random . choice ( pool ) for pool in pools ) <EOL> def random_permutation ( iterable , r = None ) : <EOL> "<STR_LIT>" <EOL> pool = tuple ( iterable ) <EOL> r = len ( pool ) if r is None else r <EOL> return tuple ( random . sample ( pool , r ) ) <EOL> def random_combination ( iterable , r ) : <EOL> "<STR_LIT>" <EOL> pool = tuple ( iterable ) <EOL> n = len ( pool ) <EOL> indices = sorted ( random . sample ( xrange ( n ) , r ) ) <EOL> return tuple ( pool [ i ] for i in indices ) <EOL> def random_combination_with_replacement ( iterable , r ) : <EOL> "<STR_LIT>" <EOL> pool = tuple ( iterable ) <EOL> n = len ( pool ) <EOL> indices = sorted ( random . randrange ( n ) for i in xrange ( r ) ) <EOL> return tuple ( pool [ i ] for i in indices ) <EOL> def tee_lookahead ( t , i ) : <EOL> """<STR_LIT>""" <EOL> for value in islice ( t . __copy__ ( ) , i , None ) : <EOL> return value <EOL> raise IndexError ( i ) <EOL> """<STR_LIT>""" <EOL> _marker = object ( ) <EOL> def chunked ( iterable , n ) : <EOL> """<STR_LIT>""" <EOL> for group in ( list ( g ) for g in izip_longest ( * [ iter ( iterable ) ] * n , <EOL> fillvalue = _marker ) ) : <EOL> if group [ - <NUM_LIT:1> ] is _marker : <EOL> del group [ group . index ( _marker ) : ] <EOL> yield group <EOL> class peekable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterable ) : <EOL> self . _it = iter ( iterable ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __nonzero__ ( self ) : <EOL> try : <EOL> self . peek ( ) <EOL> except StopIteration : <EOL> return False <EOL> return True <EOL> def peek ( self , default = _marker ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> try : <EOL> self . _peek = self . _it . next ( ) <EOL> except StopIteration : <EOL> if default is _marker : <EOL> raise <EOL> return default <EOL> return self . _peek <EOL> def next ( self ) : <EOL> ret = self . peek ( ) <EOL> del self . _peek <EOL> return ret <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
