Challenge - 5 Problems
Set Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numpy structured array intersection
What is the output of this code that finds the intersection of two structured numpy arrays based on all fields?
NumPy
import numpy as np arr1 = np.array([(1, 'apple'), (2, 'banana'), (3, 'cherry')], dtype=[('id', 'i4'), ('fruit', 'U10')]) arr2 = np.array([(2, 'banana'), (3, 'cherry'), (4, 'date')], dtype=arr1.dtype) result = np.intersect1d(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
Think about which records appear in both arrays exactly.
✗ Incorrect
The intersection returns only the records present in both arrays. Here, (2, 'banana') and (3, 'cherry') are common.
❓ data_output
intermediate1:30remaining
Number of unique records after union
Given two structured numpy arrays, what is the number of unique records after performing a union operation?
NumPy
import numpy as np arr1 = np.array([(10, 'red'), (20, 'blue')], dtype=[('code', 'i4'), ('color', 'U10')]) arr2 = np.array([(20, 'blue'), (30, 'green')], dtype=arr1.dtype) union_result = np.union1d(arr1, arr2) print(len(union_result))
Attempts:
2 left
💡 Hint
Count all unique records from both arrays combined.
✗ Incorrect
The union combines all unique records from both arrays. Here, (10, 'red'), (20, 'blue'), and (30, 'green') are unique, so count is 3.
🔧 Debug
advanced2:00remaining
Error in set difference on structured arrays
What error does this code raise when trying to find the difference between two structured numpy arrays?
NumPy
import numpy as np arr1 = np.array([(1, 2.0), (3, 4.0)], dtype=[('a', 'i4'), ('b', 'f4')]) arr2 = np.array([(3, 4.0)], dtype=[('a', 'i4'), ('b', 'f4')]) diff = np.setdiff1d(arr1, arr2, assume_unique=True) print(diff)
Attempts:
2 left
💡 Hint
Check if the operation is valid for structured arrays and what output is expected.
✗ Incorrect
np.setdiff1d works on structured arrays and returns the records in arr1 not in arr2. No error occurs here.
❓ visualization
advanced2:30remaining
Visualizing set intersection of structured arrays
Which option correctly plots a Venn diagram showing the intersection size of two structured numpy arrays based on their 'id' field?
NumPy
import numpy as np import matplotlib.pyplot as plt from matplotlib_venn import venn2 arr1 = np.array([(1, 'x'), (2, 'y'), (3, 'z')], dtype=[('id', 'i4'), ('val', 'U1')]) arr2 = np.array([(2, 'y'), (3, 'z'), (4, 'w')], dtype=arr1.dtype) ids1 = set(arr1['id']) ids2 = set(arr2['id']) plt.figure(figsize=(5,5))
Attempts:
2 left
💡 Hint
Venn diagram requires sets of hashable elements, use the 'id' field.
✗ Incorrect
Option D correctly converts the 'id' fields to sets and plots the Venn diagram showing intersection sizes.
🧠 Conceptual
expert3:00remaining
Why does np.intersect1d require structured arrays to have the same dtype?
Why must two structured numpy arrays have the same dtype to use np.intersect1d correctly?
Attempts:
2 left
💡 Hint
Think about how numpy compares structured array elements at the memory level.
✗ Incorrect
np.intersect1d compares elements by their byte representation. Different dtypes mean different memory layouts, so comparisons fail or give wrong results.