Challenge - 5 Problems
Intersection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.intersect1d() with simple arrays
What is the output of the following code?
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5, 6]) result = np.intersect1d(arr1, arr2) print(result)
NumPy
import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5, 6]) result = np.intersect1d(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
np.intersect1d() returns sorted unique values common to both arrays.
✗ Incorrect
The common elements between arr1 and arr2 are 3 and 4. np.intersect1d returns them sorted as [3 4].
❓ data_output
intermediate2:00remaining
Number of elements in intersection
Given two arrays, what is the number of elements in their intersection?
import numpy as np arr1 = np.array([10, 20, 30, 40, 50]) arr2 = np.array([30, 40, 50, 60, 70]) intersect = np.intersect1d(arr1, arr2) print(len(intersect))
NumPy
import numpy as np arr1 = np.array([10, 20, 30, 40, 50]) arr2 = np.array([30, 40, 50, 60, 70]) intersect = np.intersect1d(arr1, arr2) print(len(intersect))
Attempts:
2 left
💡 Hint
Count how many unique values appear in both arrays.
✗ Incorrect
The common elements are 30, 40, and 50, so the length of the intersection is 3.
🔧 Debug
advanced2:00remaining
Error type from incorrect np.intersect1d() usage
What error does the following code raise?
import numpy as np arr1 = [1, 2, 3] arr2 = 4 result = np.intersect1d(arr1, arr2) print(result)
NumPy
import numpy as np arr1 = [1, 2, 3] arr2 = 4 result = np.intersect1d(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
Check if the second argument is iterable as expected by np.intersect1d.
✗ Incorrect
np.intersect1d expects both inputs to be array-like. Passing an integer causes a TypeError because it is not iterable.
🚀 Application
advanced2:00remaining
Using np.intersect1d() to find common words
You have two lists of words from two documents:
What will be printed?
doc1 = ['apple', 'banana', 'cherry', 'date'] doc2 = ['banana', 'date', 'fig', 'grape'] common_words = np.intersect1d(doc1, doc2) print(common_words)
What will be printed?
NumPy
doc1 = ['apple', 'banana', 'cherry', 'date'] doc2 = ['banana', 'date', 'fig', 'grape'] common_words = np.intersect1d(doc1, doc2) print(common_words)
Attempts:
2 left
💡 Hint
np.intersect1d returns sorted unique common elements.
✗ Incorrect
The common words between doc1 and doc2 are 'banana' and 'date'. They are printed as a numpy array of strings.
🧠 Conceptual
expert3:00remaining
Behavior of np.intersect1d() with duplicates and sorting
Consider these arrays:
Which statement about the output is true?
arr1 = np.array([5, 3, 3, 1, 2, 2]) arr2 = np.array([2, 3, 3, 4, 5]) result = np.intersect1d(arr1, arr2) print(result)
Which statement about the output is true?
NumPy
arr1 = np.array([5, 3, 3, 1, 2, 2]) arr2 = np.array([2, 3, 3, 4, 5]) result = np.intersect1d(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
np.intersect1d returns sorted unique values common to both arrays.
✗ Incorrect
np.intersect1d removes duplicates and sorts the result ascending. The common unique elements are 2, 3, and 5.