0
0
Apache Airflowdevops~10 mins

Mapped tasks for parallel processing in Apache Airflow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to map the task over the list of numbers.

Apache Airflow
from airflow.decorators import task

@task
 def square_number(number):
     return number [1] 2

results = square_number.expand(number=[1, 2, 3, 4])
Drag options to blanks, or click blank then click option'
A**
B//
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of power operator (**).
Using addition (+) which adds instead of squares.
2fill in blank
medium

Complete the code to define a mapped task that prints each item.

Apache Airflow
from airflow.decorators import task

@task
 def print_item(item):
     print(item)

print_item.expand(item=[1])
Drag options to blanks, or click blank then click option'
A["apple", "banana", "cherry"]
B"hello"
C42
Drange(5)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single string instead of a list.
Passing a number which is not iterable.
3fill in blank
hard

Fix the error in the mapped task call to correctly map over the list.

Apache Airflow
from airflow.decorators import task

@task
 def multiply_by_two(x):
     return x * 2

results = multiply_by_two.expand(x=[1])
Drag options to blanks, or click blank then click option'
A"123"
B5
C[1, 2, 3]
Drange(1)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single integer instead of a list.
Passing a string which is iterable but not intended here.
4fill in blank
hard

Fill both blanks to create a mapped task that filters even numbers and squares them.

Apache Airflow
from airflow.decorators import task

@task
 def process_number(n):
     if n [1] 2 == 0:
         return n[2]2

results = process_number.expand(n=[1, 2, 3, 4, 5])
Drag options to blanks, or click blank then click option'
A%
B**
C==
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) instead of power to square.
Using equality (==) instead of modulo (%) to check evenness.
5fill in blank
hard

Fill all three blanks to create a mapped task that returns a dictionary with uppercase keys and values greater than zero.

Apache Airflow
from airflow.decorators import task

@task
 def filter_and_transform(data):
     return { [1]: [2] for [3], v in data.items() if v > 0 }

results = filter_and_transform.expand(data=[{'a': 1, 'b': -1}, {'c': 3, 'd': 0}])
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item' instead of 'k' for the key variable.
Not converting keys to uppercase.
Mixing up keys and values in the comprehension.