Back to Practice
#0157
Compute Ridge Penalty Term
EasyML15 min10 XP
Problem
Given weights and lambda, return the ridge penalty lambda * sum(weight^2). Do not include the bias term if provided separately.
Why This Matters
Regularization is easier to understand when the penalty can be computed directly from model weights.
Function Signature
def ridge_penalty(weights, lam):
Examples
Example 1
Inputweights = [2, -3, 1], lam = 0.5
Output7.0
0.5 * (4 + 9 + 1) = 7.0.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Keep the code readable and deterministic.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputweights = [2, -3, 1], lam = 0.5
Expected7.0
0.5 * (4 + 9 + 1) = 7.0.
Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input