Create Arrays with Special Values
Problem Statement
Create NumPy arrays filled with special values (zeros, ones, specific values).
Background
NumPy provides functions to create pre-filled arrays:
- np.zeros(shape): Array of zeros
- np.ones(shape): Array of ones
- np.full(shape, value): Array filled with specific value
- np.empty(shape): Uninitialized array
Your Task
Write a function create_special_arrays(rows, cols) that returns a dictionary with:
- "zeros": Array of zeros as nested list
- "ones": Array of ones as nested list
- "fives": Array filled with 5s as nested list
Output Format
Return a dictionary with exactly these three keys.
Example:
rows = 2, cols = 3
{'zeros': [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], 'ones': [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], 'fives': [[5, 5, 5], [5, 5, 5]]}Creates 2x3 arrays with different fill values
Constraints:
- Use np.zeros(), np.ones(), np.full()
- rows and cols will be positive integers
1. Background Knowledge
NumPy is Python's foundational library for numerical computing, providing efficient n-dimensional arrays (ndarray) that enable vectorized operations—performing computations on entire arrays without explicit loops. Key functions for this problem:
- np.zeros(shape): Creates array filled with 0.0 (float64 by default).
- np.ones(shape): Creates array filled with 1.0 (float64 by default).
- np.full(shape, fill_value): Creates array filled with any specified value (dtype adapts to value type).
Array shape is a tuple (rows, cols) defining dimensions. Nested lists are Python's native representation: [[0.0, 0.0], [0.0, 0.0]] for 2×2 zeros array. Conversion uses array.tolist().
Prerequisites: Basic NumPy import (import numpy as np), understanding shape tuples, dictionary creation.
2. Algorithm Approach
This is a direct mapping problem: three independent array creation operations stored in a dictionary. No search, sorting, or iteration needed—pure array initialization.
Algorithm:
1. Create zeros array: np.zeros((rows, cols))
2. Create ones array: np.ones((rows, cols))
3. Create fives array: np.full((rows, cols), 5)
4. Convert each to nested list:.tolist()
5. Package in dict: {"zeros": z_list, "ones": o_list, "fives": f_list}
Vectorization principle: NumPy fills entire arrays in contiguous memory using optimized C loops, O(1) initialization regardless of size.
3. Step-by-Step Strategy
import numpy as np
def create_special_arrays(rows, cols):
# Step 1: Create shape tuple
shape = (rows, cols)
# Step 2: Generate arrays using required functions
zeros_arr = np.zeros(shape) # Shape: (rows, cols), all 0.0
ones_arr = np.ones(shape) # Shape: (rows, cols), all 1.0
fives_arr = np.full(shape, 5) # Shape: (rows, cols), all 5
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.