All problems
Elias Gamma Code Length
Problem
Return the length in bits of the Elias gamma code for n >= 1. The code writes floor(log2(n)) leading zeros, then the binary representation of n, so the length is 2 * floor(log2(n)) + 1. Return 0 for n <= 0.
Hint
n.bit_length() - 1 is floor(log2(n)) for n >= 1.
Test cases
case 1
in: [1]
expected: 1
case 2
in: [2]
expected: 3
case 3
in: [7]
expected: 5
case 4
in: [8]
expected: 7
case 5
in: [100]
expected: 13
Starter codePython
def elias_gamma_length(n):
# Your code here
pass5 test cases · run your code against them in the editor