Practicing Coding Interview Questions in Python
Kirill Smirnov
Data Science Consultant, Altran
Generator - a special iterable object created by a function having a yield
keyword in its body.
def func():
# Return a value from super complex calculations
return 0
result = func()
print(result)
0
Generator - a special iterable object created by a function having a yield
keyword in its body.
def func():
# Yield a value from super complex calculations
yield 0
result = func()
print(result)
<generator object result at 0x105736e10>
def func():
# Yield a value from super complex calculations
yield 0
result = func()
for item in result:
print(item)
0
def func():
yield 0
yield 1
yield 2
result = func()
for item in result:
print(item)
0
1
2
def func(n):
for i in range(0, n):
yield 2*i
result = func(3)
for item in result:
print(item)
0
2
4
def func(n):
for i in range(0, n):
yield 2*i
result = func(5)
list(result)
[0, 2, 4, 6, 8]
Generator is an Iterable AND an Iterator
def func(n):
for i in range(0, n):
yield 2*i
result = func(3)
next(result)
0
next(result)
2
next(result)
4
next(result)
StopIteration
def func(n):
for i in range(0, n):
yield 2*i
result = func(3)
for item in result:
print(item)
0
2
4
for item in result:
print(item)
# nothing
result = func(3)
for item in result:
print(item)
0
2
4
def func(n):
for i in range(0, n):
yield 2*i
result = func(3)
list(result)
[0, 2, 4]
list(result)
[]
result = func(3)
list(result)
[0, 2, 4]
result = [2*i for i in range(0, 3)]
print(result)
[0, 2, 4]
result = (2*i for i in range(0, 3))
print(result)
<generator object result at 0x105736e10>
result = (2*i for i in range(0, 3))
for item in result:
print(item)
0
2
4
next(result)
StopIteration
[1, 3, 2, 4, 3, 5]
def create_jump_sequence(n):
for i in range(1, n-1):
yield i
yield i+2
jump_sequence = create_jump_sequence(5)
list(jump_sequence)
[1, 3, 2, 4, 3, 5]
[1, 3, 2, 4, 3, 5, 4, 6, 5, 7, ...]
def create_jump_sequence(n):
for i in range(1, n-1):
yield i
yield i+2
jump_sequence = create_jump_sequence(500)
next(jump_sequence)
1
def create_inf_generator():
while True:
yield 'I am infinite!'
inf_generator = create_inf_generator()
next(inf_generator)
I am infinite
Practicing Coding Interview Questions in Python