1. Learn
  2. /
  3. Courses
  4. /
  5. Practicing Coding Interview Questions in Python

Exercise

Generator comprehensions

You are given the following generator functions (you can test them in the console):

def func1(n):
  for i in range(0, n):
    yield i**2
def func2(n):
  for i in range(0, n):
     if i%2 == 0:
       yield 2*i
def func3(n, m):
  for i in func1(n):
    for j in func2(m):
      yield ((i, j), i + j)

Note that func3() uses internally func1() and func2().

Instructions 1/3

undefined XP
  • 1

    Rewrite func1() as a generator comprehension with \(n\) = 10.

  • 2

    Rewrite func2() as a generator comprehension with \(n\) = 20.

  • 3

    Rewrite func3() as a generator comprehension with \(n\) = 8 and \(m\) = 10.