Bird
Raised Fist0
NumPydata~10 mins

np.searchsorted() for insertion points in NumPy - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the insertion index for value 5 in a sorted array.

NumPy
import numpy as np
arr = np.array([1, 3, 7, 9])
index = np.searchsorted(arr, [1])
print(index)
Drag options to blanks, or click blank then click option'
A5
B4
C5.0
D'5'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong value like 4 or 5.0
Passing the value as a string '5'
2fill in blank
medium

Complete the code to find the insertion index for value 2 in a sorted array using side='right'.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3])
index = np.searchsorted(arr, [1], side='right')
print(index)
Drag options to blanks, or click blank then click option'
A3
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using side='left' instead of 'right'
Choosing index 2 which is before the second 2
3fill in blank
hard

Fix the error in the code to correctly find insertion indices for multiple values.

NumPy
import numpy as np
arr = np.array([1, 3, 5, 7])
values = np.array([2, 4, 6])
indices = np.searchsorted(arr, [1])
print(indices)
Drag options to blanks, or click blank then click option'
Avalues
B[2, 4, 6]
Carr
Dnp.array([1, 2, 3])
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the original array instead of the values array
Passing a list instead of a numpy array
4fill in blank
hard

Fill both blanks to create a dictionary of values and their insertion indices for values > 3.

NumPy
import numpy as np
arr = np.array([1, 3, 5, 7, 9])
values = np.array([2, 4, 6, 8])
result = {v: np.searchsorted(arr, v[1]) for v in values if v [2] 3}
print(result)
Drag options to blanks, or click blank then click option'
A, side='right'
B>
C<
D, side='left'
Attempts:
3 left
💡 Hint
Common Mistakes
Using side='left' instead of side='right'
Using < instead of > in the condition
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and insertion indices for values less than 7.

NumPy
import numpy as np
arr = np.array([1, 3, 5, 7, 9])
values = np.array([2, 4, 6, 8])
result = { [1]: np.searchsorted(arr, v[2]) for v in values if v [3] 7}
print(result)
Drag options to blanks, or click blank then click option'
Astr(v).upper()
B, side='left'
C<
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using the value itself as key instead of uppercase
Using side='right' instead of side='left'
Using > instead of < in the condition

Practice

(1/5)
1. What does the np.searchsorted() function do in NumPy?
easy
A. Returns the maximum value in the array
B. Sorts the array in ascending order
C. Removes duplicate values from the array
D. Finds the index where a value should be inserted to keep the array sorted

Solution

  1. Step 1: Understand the purpose of np.searchsorted()

    This function finds the position where a new element can be inserted in a sorted array without breaking the order.
  2. Step 2: Compare with other options

    Options B, C, and D describe different functions: sorting, removing duplicates, and finding max, which are not what searchsorted does.
  3. Final Answer:

    Finds the index where a value should be inserted to keep the array sorted -> Option D
  4. Quick Check:

    Insertion index finder = A [OK]
Hint: Remember: searchsorted finds insert position, not sorting [OK]
Common Mistakes:
  • Confusing searchsorted with sorting functions
  • Thinking it removes duplicates
  • Assuming it returns values instead of indices
2. Which of the following is the correct syntax to find the insertion index of value 5 in a sorted array arr using np.searchsorted()?
easy
A. np.searchsorted(arr, value=5)
B. np.searchsorted(arr, 5)
C. arr.searchsorted(5)
D. np.searchsorted(5, arr)

Solution

  1. Step 1: Recall the function signature

    The correct syntax is np.searchsorted(array, value), so the array comes first, then the value.
  2. Step 2: Check each option

    np.searchsorted(arr, 5) matches the correct order. np.searchsorted(5, arr) reverses arguments. arr.searchsorted(5) is invalid because searchsorted is not a method of ndarray. np.searchsorted(arr, value=5) uses a wrong keyword argument.
  3. Final Answer:

    np.searchsorted(arr, 5) -> Option B
  4. Quick Check:

    Array first, value second = D [OK]
Hint: Remember: np.searchsorted(array, value) order [OK]
Common Mistakes:
  • Swapping the order of arguments
  • Using searchsorted as a method of array
  • Using incorrect keyword arguments
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 3, 5, 7])
index = np.searchsorted(arr, 4)
print(index)
medium
A. 2
B. 1
C. 3
D. 4

Solution

  1. Step 1: Understand the array and value

    The array is [1, 3, 5, 7], and we want to insert 4 while keeping it sorted.
  2. Step 2: Find the insertion index

    4 fits between 3 (index 1) and 5 (index 2), so the insertion index is 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Insert 4 between 3 and 5 = 2 [OK]
Hint: Find where value fits between sorted elements [OK]
Common Mistakes:
  • Choosing index of smaller element
  • Choosing index of larger element
  • Confusing zero-based indexing
4. The code below throws an error. What is the mistake?
import numpy as np
arr = np.array([2, 4, 6, 8])
index = np.searchsorted(arr, side='left', 5)
print(index)
medium
A. The 'side' argument should come after the value argument
B. The array must be sorted in descending order
C. The function does not accept keyword arguments
D. The value to insert must be an array, not a scalar

Solution

  1. Step 1: Check function argument order

    np.searchsorted expects the array first, then the value, then optional keywords like side.
  2. Step 2: Identify the error in argument placement

    The code passes side='left' before the value 5, which is incorrect syntax.
  3. Final Answer:

    The 'side' argument should come after the value argument -> Option A
  4. Quick Check:

    Keyword args after positional args = A [OK]
Hint: Put keyword arguments after positional ones [OK]
Common Mistakes:
  • Placing keyword arguments before positional arguments
  • Assuming array must be descending
  • Thinking scalar values are invalid
5. Given a sorted array arr = np.array([1, 2, 2, 3, 4]), which code snippet will insert the value 2 after all existing 2s using np.searchsorted()?
hard
A. index = np.searchsorted(arr, 2)
B. index = np.searchsorted(arr, 2, side='left')
C. index = np.searchsorted(arr, 2, side='right')
D. index = np.searchsorted(arr, 2, side='both')

Solution

  1. Step 1: Understand the side parameter

    side='right' returns the insertion index after existing equal values; side='left' inserts before.
  2. Step 2: Apply to the array

    For value 2 in [1, 2, 2, 3, 4], side='right' gives index 3, after the two 2s.
  3. Final Answer:

    index = np.searchsorted(arr, 2, side='right') -> Option C
  4. Quick Check:

    Insert after equals = side='right' = C [OK]
Hint: Use side='right' to insert after equal values [OK]
Common Mistakes:
  • Using side='left' inserts before equal values
  • Assuming default side inserts after equals
  • Using invalid side='both'