PIXELBANKv9.1.0
Menu

Problem Statement

A transition function P(s' | s, a) must be a valid probability distribution over next states. Given a list of non-negative unnormalized counts, return the normalized distribution. If the counts sum to 0, return a uniform distribution.

Implement normalize_transition(counts).

Example:

Input:
normalize_transition([1, 3])
Output:
[0.25, 0.75]
Reasoning:
  • Compute the sum of the input counts to determine the normalization factor: total=1+3=4total = 1 + 3 = 4.
  • Check if the total is zero to decide between a uniform distribution and standard normalization; since 4≠04 \neq 0, proceed with dividing each count by the total.
  • Normalize the first count by dividing it by the total: 1/4=0.251 / 4 = 0.25.
  • Normalize the second count by dividing it by the total: 3/4=0.753 / 4 = 0.75.
  • The final output is [0.25, 0.75]

Constraints:

  • 1 <= len(counts) <= 1000, all counts >= 0.
  • Output sums to 1.
  • All-zero input returns uniform 1/n each.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Normalize a Transition Row - Easy | PixelBank