Python 3.2.2 (default, Sep 4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def tee(it, n=2):
Q = [[] for i in range(n)]
def helper(i):
if not Q[i]:
x = next(it)
for q in Q:
q.append(x)
yield Q[i].pop(0)
return [helper(i) for i in range(n)]
>>> x,y,z = tee(iter("abcdef"), 3)
>>>
>>> next(x)
'a'
>>> next(x)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
next(x)
StopIteration
>>> def tee(it, n=2):
Q = [[] for i in range(n)]
def helper(i):
while True:
if not Q[i]:
x = next(it)
for q in Q:
q.append(x)
yield Q[i].pop(0)
return [helper(i) for i in range(n)]
>>> x,y,z = tee(iter("abcdef"), 3)
>>> next(x)
'a'
>>> next(x)
'b'
>>> next(x)
'c'
>>> next(x)
'd'
>>> next(y)
'a'
>>> next(y)
'b'
>>> next(z)
'a'
>>> next(y)
'c'
>>> next(y)
'd'
>>> next(y)
'e'
>>> x,y = tee(iter("abcdef"), 3)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
x,y = tee(iter("abcdef"), 3)
ValueError: too many values to unpack (expected 2)
>>> it = iter("abcdef")
>>> x,y = tee(it, 2)
>>>
>>> next(x)
'a'
>>> next(x)
'b'
>>> list(it)
['c', 'd', 'e', 'f']
>>> next(y)
'a'
>>> next(y)
'b'
>>> next(y)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
next(y)
File "<pyshell#11>", line 6, in helper
x = next(it)
StopIteration
>>> x
<generator object helper at 0x0000000003302EA0>
>>>
>>>
>>>
>>>
>>>
>>> def gray_code(k):
n = 0
for i in range(2**k):
n ^= ....
SyntaxError: invalid syntax
>>> 0b111000101 ^ 0b1000
461
>>> bin(_)
'0b111001101'
>>> 461 ^ 0b1000
453
>>> bin(_)
'0b111000101'
>>> ================================ RESTART ================================
>>>
>>> list(gray_code(3))
[0, 1, 3, 2, 6, 7, 5, 4]
>>> list(gray_code(5))
[0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16]
>>> [bin(x) for x in gray_code(5)]
['0b0', '0b1', '0b11', '0b10', '0b110', '0b111', '0b101', '0b100', '0b1100', '0b1101', '0b1111', '0b1110', '0b1010', '0b1011', '0b1001', '0b1000', '0b11000', '0b11001', '0b11011', '0b11010', '0b11110', '0b11111', '0b11101', '0b11100', '0b10100', '0b10101', '0b10111', '0b10110', '0b10010', '0b10011', '0b10001', '0b10000']
>>> for x in _: print(x)
0b0
0b1
0b11
0b10
0b110
0b111
0b101
0b100
0b1100
0b1101
0b1111
0b1110
0b1010
0b1011
0b1001
0b1000
0b11000
0b11001
0b11011
0b11010
0b11110
0b11111
0b11101
0b11100
0b10100
0b10101
0b10111
0b10110
0b10010
0b10011
0b10001
0b10000
>>> for x in gray_code(5):
print("{:05b}".format(x))
00000
00001
00011
00010
00110
00111
00101
00100
01100
01101
01111
01110
01010
01011
01001
01000
11000
11001
11011
11010
11110
11111
11101
11100
10100
10101
10111
10110
10010
10011
10001
10000
>>> [n ^ (n//2) for n in range(2**5)]
[0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16]
>>> list(gray_code(5))
[0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16]
>>>