<s> def permutations ( iterable , r = None ) : <EOL> pool = tuple ( iterable ) <EOL> n = len ( pool ) <EOL> r = n if r is None else r <EOL> if r > n : <EOL> return <EOL> indices = range ( n ) <EOL> cycles = range ( n , n - r , - <NUM_LIT:1> ) <EOL> yield tuple ( pool [ i ] for i in indices [ : r ] ) <EOL> while n : <EOL> for i in reversed ( range ( r ) ) : <EOL> cycles [ i ] -= <NUM_LIT:1> <EOL> if cycles [ i ] == <NUM_LIT:0> : <EOL> indices [ i : ] = indices [ i + <NUM_LIT:1> : ] + indices [ i : i + <NUM_LIT:1> ] <EOL> cycles [ i ] = n - i <EOL> else : <EOL> j = cycles [ i ] <EOL> indices [ i ] , indices [ - j ] = indices [ - j ] , indices [ i ] <EOL> yield tuple ( pool [ i ] for i in indices [ : r ] ) <EOL> break <EOL> else : <EOL> return <EOL> def combinations ( iterable , r ) : <EOL> pool = tuple ( iterable ) <EOL> n = len ( pool ) <EOL> if r > n : <EOL> return <EOL> indices = range ( r ) <EOL> yield tuple ( pool [ i ] for i in indices ) <EOL> while True : <EOL> for i in reversed ( range ( r ) ) : <EOL> if indices [ i ] != i + n - r : <EOL> break <EOL> else : <EOL> return <EOL> indices [ i ] += <NUM_LIT:1> <EOL> for j in range ( i + <NUM_LIT:1> , r ) : <EOL> indices [ j ] = indices [ j - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> yield tuple ( pool [ i ] for i in indices ) </s>
