Negate Values in a Range

mediumnumpy_indexing

Implement mask_negate(x, lo, hi).

Given a 1D array x and scalars lo < hi, return a new array equal to x but with every element strictly between lo and hi negated (multiplied by -1). Leave x unchanged. Use a boolean mask — no for/while loops.

Constraints
  • no scipy, sklearn
  • no for/while loops

Your solution

Edit mask_negate and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

Build a boolean mask (x > lo) & (x < hi) and flip the sign of those entries in a copy.