PIXELBANKv9.1.0
Menu

Copy vs View

Problem Statement

Understand the difference between array copies and views in NumPy.

Background

  • arr.copy(): Creates a deep copy (independent array)
  • arr.view() or slicing: Creates a view (shares data with original)
  • Modifying a view affects the original array!

Your Task

Write a function test_copy_view(arr) that:

  1. Creates a copy and a view of the array
  2. Modifies the first element of each to 999
  3. Returns a dictionary with:
    • "original": Original array as list (after modifications)
    • "copy_modified": Whether modifying copy affected original (boolean)
    • "view_modified": Whether modifying view affected original (boolean)

Output Format

Return a dictionary with exactly these three keys.

Example:

Input:
[1, 2, 3, 4, 5]
Output:
{'original': [999, 2, 3, 4, 5], 'copy_modified': False, 'view_modified': True}
Reasoning:

View shares data, so original is modified. Copy is independent.

Constraints:

  • Use .copy() for deep copy
  • Use .view() for shallow view
  • Array will have at least 1 element
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Copy vs View - Medium | PixelBank