All problems
Viterbi vs Posterior Difference
Problem
Compare a Viterbi path with the per-position posterior distributions. posteriors is a list of state-to-probability dictionaries, one per position.
Return the sorted list of positions where the posterior argmax (ties broken alphabetically) differs from the Viterbi path. Positions beyond the path length are ignored.
Hint
Iterate positions and compare the alphabetical argmax with the path entry when it exists.
Test cases
case 1
in: [["A","B","C"],[{"A":0.6,"B":0.4},{"A":0.7,"B":0.3},{"A":0.2,"B":0.8}]]
expected: [1,2]
case 2
in: [["A"],[{"A":0.5,"B":0.5}]]
expected: []
case 3
in: [["A"],[]]
expected: []
case 4
in: [[],[{"A":1}]]
expected: []
Starter codePython
def viterbi_posterior_diff(path, posteriors):
# Your code here
pass4 test cases · run your code against them in the editor