Array Creator
Problem Statement
Create NumPy arrays using different methods.
Background
NumPy provides many ways to create arrays:
- np.array([1, 2, 3]) - from list
- np.zeros((m, n)) - array of zeros
- np.ones((m, n)) - array of ones
- np.arange(start, stop, step) - range
- np.linspace(start, stop, num) - evenly spaced
Your Task
Write a function create_arrays(n) that returns a dictionary with different arrays.
Output Format
Return a dictionary with:
- "zeros": n×n matrix of zeros (list of lists)
- "ones": n×n matrix of ones (list of lists)
- "range": Array from 0 to n-1 (list)
- "identity": n×n identity matrix (list of lists)
Example:
n = 3
{'zeros': [[0.0, 0.0, 0.0], ...], 'ones': [[1.0, ...], ...], ...}np.zeros((3,3)), np.ones((3,3)), np.arange(3), np.eye(3)
Constraints:
- Use np.zeros, np.ones, np.arange, np.eye
- Convert to lists using .tolist()
More from NumPy Foundations
Background Knowledge
NumPy is the foundational Python library for numerical computing, providing efficient multidimensional arrays (ndarrays) that support vectorized operations, broadcasting, and fast mathematical computations without explicit loops. Unlike Python lists, NumPy arrays are homogeneous (all elements same type), fixed-size, and stored contiguously in memory, enabling high-performance operations like element-wise arithmetic and linear algebra—key for scientific computing in fields like physics and data analysis. Core creation functions include np.zeros(shape) for zero-filled arrays, np.ones(shape) for ones, np.arange(start, stop, step) for integer sequences, and np.eye(n) or np.identity(n) for diagonal identity matrices with 1s on the main diagonal and 0s elsewhere.
This problem emphasizes converting NumPy arrays to nested Python lists (list of lists for 2D), as the output format requires pure Python structures rather than ndarray objects—common when interfacing NumPy with other libraries or for serialization. Understanding array shapes (e.g., (n, n) for square matrices) and the .tolist() method is crucial, as it recursively converts arrays to lists while preserving floats (e.g., 0.0 instead of 0). This bridges NumPy's efficiency with Python's flexibility.
Algorithm/Approach
Use NumPy's dedicated array creation functions to generate each required structure, then convert to the specified list format using .tolist(). Return a dictionary mapping string keys to these lists. This leverages NumPy's optimized internals for creation (e.g., pre-allocated memory) while meeting the output constraints—no manual loops needed, ensuring scalability for larger n.
Step-by-Step Strategy
- Import NumPy: Start with import numpy as np to access creation functions.
- Create zeros matrix: Use np.zeros((n, n)) for an n×n float array of zeros.
- Create ones matrix: Use np.ones((n, n)) for an n×n float array of ones.
- Create range array: Use np.arange(n) (or np.arange(0, n)) for 1D array [0, 1,..., n-1].
- Create identity matrix: Use np.eye(n) or np.identity(n) for n×n diagonal matrix.
- Convert to lists: Apply .tolist() to each array (e.g., zeros.tolist()) to get nested lists with float values.
- Build dictionary: Map keys "zeros", "ones", "range", "identity" to the lists and return it.
Test with n=3 to match the sample output structure.
Common Pitfalls
- Output type mismatch: Returning ndarrays instead of lists fails tests—always use .tolist(); verify with type(result["zeros"]) expecting float.
- Shape errors: Forgetting (n, n) tuple for 2D arrays creates 1D; np.arange(n) stops at n-1 (exclusive stop).
- Integer vs. float: NumPy defaults to float64 for zeros/ones/eye, matching sample 0.0—avoid dtype=int unless specified.
- Empty input: For n=0, np.eye(0) returns [] (empty list), but confirm problem constraints (likely n >= 1).
- Import omission: Forgetting import numpy as np causes NameError.
Time & Space Complexity
- Time: O(n2) dominant from creating n×n matrices (allocation + initialization); .tolist() is also O(n2). Overall O(n2).
- Space: O(n2) for storing two n×n matrices + O(n) for range/identity; temporary NumPy arrays add another O(n2) before conversion. Efficient due to contiguous memory.