Scale and Shift an Array

~8 mincode completion

Implement scale_and_shift(arr, scale, shift) that multiplies every element by scale then adds shift.

Examples

Scale by 2, shift by 1

Input
scale_and_shift([1, 2, 3], 2, 1)
Output
[3, 5, 7]

Scale 0.5, negative shift

Input
scale_and_shift([0, 5, 10], 0.5, -1)
Output
[-1, 1.5, 4]

Negative values, scale 3

Input
scale_and_shift([-2, -1, 0, 1, 2], 3, 0)
Output
[-6, -3, 0, 3, 6]

Hints

Hint 1

Work directly with the arguments arr, scale, shift and return the result rather than printing it.

Hint 2

Vectorise this. A Python loop will not pass.

Requirements

  • arr: NumPy array (any shape)

  • scale: Multiplicative factor

  • shift: Additive offset

  • Return Array of same shape as arr.

  • Use a fully vectorised implementation without Python loops

Constraints

  • Vectorised implementation only, no Python loops

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

8 employers weight this skill

3 quant funds, 2 big tech firms, 1 frontier lab, 1 autonomy company, 1 enterprise vendor. Top match scores 81.

Python
import numpy as np

def scale_and_shift(arr: np.ndarray, scale: float, shift: float) -> np.ndarray:
    """
    Multiply each element by scale, then add shift.

    Args:
        arr:   NumPy array (any shape)
        scale: Multiplicative factor
        shift: Additive offset

    Returns:
        Array of same shape as arr.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.