PIXELBANKv8.2.1
Menu

Hard Tanh

Problem Statement

Implement the "Hard Tanh" function which clamps values to a range.

Background

Hard Tanh is a computationally cheaper approximation of the tanh function:

  • Returns 1-1 if x<1x < -1
  • Returns 11 if x>1x > 1
  • Returns xx if 1x1-1 \leq x \leq 1

It's essentially a piecewise linear function that clips values outside [-1, 1].

Your Task

Write a function hard_tanh(values) that applies Hard Tanh element-wise to a list.

Output Format

Return a list of floats with Hard Tanh applied. Round each value to 4 decimal places.

Example:

Input:
values=[-1.5, 0.5, 2.0]
Output:
[-1.0, 0.5, 1.0]
Reasoning:

-1.5 < -1 → clamped to -1.0; 0.5 is in [-1,1] → stays 0.5; 2.0 > 1 → clamped to 1.0.

Constraints:

  • -1000 <= x <= 1000 for each element
  • List length: 1 to 100 elements
Editor

Test Results

0/0
Run code to see test results.