Bird
0
0

You have a NumPy array arr = np.arange(20). How would you slice it to get every 4th element in reverse order starting from the last element?

hard📝 Application Q15 of 15
NumPy - Indexing and Slicing
You have a NumPy array arr = np.arange(20). How would you slice it to get every 4th element in reverse order starting from the last element?
A<code>arr[19::-4]</code>
B<code>arr[::4]</code>
C<code>arr[19:0:4]</code>
D<code>arr[0:19:-4]</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand array and goal

    arr = np.arange(20) creates array from 0 to 19. We want every 4th element backward starting from last element (index 19).
  2. Step 2: Check slicing options

    arr[19::-4] starts at index 19, goes backward to start (index 0), stepping by 4, which matches the goal.
  3. Step 3: Verify other options

    A: arr[::4] every 4th forward [0,4,8,...], wrong direction. C: arr[19:0:4] positive step with start>stop gives empty. D: arr[0:19:-4] start<stop with negative step gives empty.
  4. Final Answer:

    arr[19::-4] -> Option A
  5. Quick Check:

    Reverse every 4th from last = arr[19::-4] [OK]
Quick Trick: Use start index with negative step for reverse slicing [OK]
Common Mistakes:
  • Using positive step with decreasing indices
  • Wrong start or stop indices
  • Assuming arr[::-4] always starts at last element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes