v9.1.1
Menu

Create Range Arrays

easyFREEI/ONumPy Array CreationNumpy

Problem Statement

Create NumPy arrays with sequential or evenly spaced values.

Background

  • np.arange(start, stop, step): Like Python range, but returns array
  • np.linspace(start, stop, num): num evenly spaced values

Your Task

Write a function create_ranges(start, stop, num_points) that returns a dictionary with:

  • "arange": Array from start to stop (exclusive) with step 1
  • "linspace": num_points evenly spaced values from start to stop (inclusive)

Round linspace values to 2 decimal places.

Output Format

Return a dictionary with exactly these two keys. Both values must be plain Python lists (not NumPy arrays), as shown in the sample.

Example

Input
start = 0, stop = 5, num_points = 5
Output
{'arange': [0, 1, 2, 3, 4], 'linspace': [0.0, 1.25, 2.5, 3.75, 5.0]}
Reasoning
  • Inputs: start =0= 0 and stop =5= 5 bound the interval; num_points =5= 5 only affects the second list.
  • arange: integers 0,1,2,…0, 1, 2, \dots stepping by 11 from 00 up to but not including 55, i.e. {k:0≤k<5}\{k : 0 \le k < 5\}. That gives 55 values, [0, 1, 2, 3, 4], and since every value is an integer they print without a decimal point.
  • linspace: both endpoints are included, so 55 points create 5−1=45 - 1 = 4 equal gaps. The spacing is Δ=5−04=1.25\Delta = \frac{5 - 0}{4} = 1.25, and the ii-th value is 0+iΔ0 + i\Delta for i=0,…,4i = 0, \dots, 4: 0,1.25,2.5,3.75,50, 1.25, 2.5, 3.75, 5.
  • Rounding to 2 decimal places changes nothing here, but the values are floats, so the list is [0.0, 1.25, 2.5, 3.75, 5.0].
  • Notice: the first list's length is stop−start\text{stop} - \text{start} and it never reaches stop; the second always has exactly num_points values and always ends at stop. With start =0=0, stop =10=10, 33 points, the spacing becomes 102=5\frac{10}{2} = 5.

Constraints

  • Round linspace to 2 decimal places
  • start < stop

More from Numpy

solution.py

Test results

Sign in to run your code against the test cases.