Create Range Arrays
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.
Example:
start = 0, stop = 5, num_points = 5
{'arange': [0, 1, 2, 3, 4], 'linspace': [0.0, 1.25, 2.5, 3.75, 5.0]}arange gives [0,1,2,3,4], linspace gives 5 points from 0 to 5
Constraints:
- Round linspace to 2 decimal places
- start < stop
1. Background Knowledge
NumPy arrays are the foundation of numerical computing in Python, enabling efficient vectorized operations on multi-dimensional data without explicit loops. Key functions for this problem:
- np.arange(start, stop, step): Generates values from start to stop (exclusive) with fixed step. Like Python's range(), but returns a NumPy array. Default step=1.
- np.linspace(start, stop, num): Creates num evenly spaced values from start to stop (inclusive). Spacing calculated as:
Key differences:
| Function | Stop inclusive? | Fixed step? | Use case |
|---|---|---|---|
| arange | ❌ No | ✅ Yes | Integer sequences |
| linspace | ✅ Yes | ❌ Even spacing | Fixed number of points |
2. Algorithm Approach
This is a direct mapping problem: translate function specifications into NumPy calls.
- arange: np.arange(start, stop) → step defaults to 1
- linspace: np.linspace(start, stop, num_points) → round to 2 decimals
- Package in dictionary: {"arange": arr1, "linspace": arr2}
No search/optimization needed; pure function composition.
3. Step-by-Step Strategy
import numpy as np
def create_ranges(start, stop, num_points):
# Step 1: Create arange array (stop exclusive, step=1)
arange_arr = np.arange(start, stop)
# Step 2: Create linspace array (stop inclusive, num_points points)
linspace_arr = np.linspace(start, stop, num_points)
# Step 3: Round linspace to 2 decimal places
linspace_arr = np.round(linspace_arr, 2)
# Step 4: Return dictionary with exact keys
return {
"arange": arange_arr,
"linspace": linspace_arr
}
Verification:
result = create_ranges(0, 5, 5)
print(result)
# {'arange': array([0, 1, 2, 3, 4]), 'linspace': array([0., 1.25, 2.5, 3.75, 5. ])} ✓
4. Common Pitfalls
- Stop inclusivity: np.arange(0, 5) → [0,1,2,3,4] (excludes 5); np.linspace(0, 5, 5) → includes 5.
- Rounding precision: Use np.round(linspace_arr, 2) after linspace, not as parameter.
- Integer inputs: np.linspace(0, 5, 5) produces floats; rounding converts to float64 with 2 decimals.
- Dictionary keys: Must be exactly "arange" and "linspace" (string literals).
- Import: import numpy as np required.
- Constraints: Assume start < stop; no validation needed.
5. Time & Space Complexity
Time: O(n) where n=max(\text{stop}−\text{start},\text{num_points})
- arange: O(\text{stop}−\text{start}) array allocation/filling
- linspace: O(\text{num_points})
- Rounding: O(\text{num_points})
Space: O(n)
- Two arrays stored in dictionary: O(\text{stop}−\text{start}+\text{num_points})
NumPy optimization: Vectorized creation is O(1) amortized per element due to contiguous memory allocation and cache efficiency.