Algorithm · Google · Easy
Implement the decoder for a simple random-number generator. A fair source produces bits, each independently 0 or 1 with equal probability. To approximate a uniform real number in the interval $$[0, 1)$$, the first n bits are placed immediately after the binary point: bits[0] is the halves place, bits[1] is the quarters place, and so on. Given the ordered list bits, compute and return the resulting value $$X = \sum_{i=0}^{n-1} \frac{bits[i]}{2^{i+1}}.$$ If bits is empty,…
Checking your access…