v9.1.1
Menu

Create Arrays with Special Values

easyPROI/ONumPy Array CreationNumpy

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

Input
rows = 2, cols = 3
Output
{'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]]}
Reasoning
  • Inputs: rows =2= 2 and cols =3= 3 fix the shape of every matrix: 2 rows, each holding 3 entries, so each matrix has 2×3=62 \times 3 = 6 entries.
  • Math: three constant matrices of that shape. The zero matrix has aij=0a_{ij} = 0 for every row ii and column jj; the all-ones matrix has aij=1a_{ij} = 1; the third has aij=5a_{ij} = 5. No entry depends on its position, only on which matrix it belongs to.
  • Output shape: each matrix is written row by row as a list of 2 rows, each row a list of 3 numbers, e.g. [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]. The three matrices sit under the keys zeros, ones and fives.
  • Notice the number types: the zeros and ones appear as real numbers (0.0, 1.0), while the fives appear as whole numbers (5). The printed output must match this exactly.
  • Swapping the arguments to 3×23 \times 2 gives 3 rows of 2 entries each, not the same lists.

Constraints

  • Use np.zeros(), np.ones(), np.full()
  • rows and cols will be positive integers

Learn this in

More from Numpy

PRO

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

Sign in to run your code against the test cases.