Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-132

Energy Compaction

Problem

Return the fraction of total variance captured by the k largest coefficients: the sum of the k largest values divided by the sum of all values. Assume k is at most the list length and the total is positive.

Hint

Transform coding works because a few coefficients hold most of the energy.

Test cases

case 1
in: [[4,3,2,1],2]
expected: 0.7
case 2
in: [[1,1,1,1],2]
expected: 0.5
case 3
in: [[9,1,0],1]
expected: 0.9
case 4
in: [[2,2],2]
expected: 1
Starter codePython
def energy_compaction(variances, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor