easy +8 pts

Convert Binary Number List

Given a list of 0s and 1s, compute the integer value it represents in binary.

Write a function `binary_list_to_int(bits)` that takes a list `bits` containing only integers 0 and 1. The list represents a binary number in most-significant-first order (i.e., `[1, 0, 1]` means 5). The function should return the integer value of that binary number. The input list can be empty; in that case, return 0. The length of the list is at most 30, so the result fits in a normal Python integer.

Constraints

- `bits` is a list of integers, each is either 0 or 1. - `0 <= len(bits) <= 30`. - Return an integer.

Example

>>> binary_list_to_int([1, 0, 1])
5
>>> binary_list_to_int([])
0
>>> binary_list_to_int([1] * 4)
15
>>> binary_list_to_int([0] * 5)
0
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate through the list and use the formula: result = result * 2 + bit.
Remember that an empty list represents zero.
Alternatively, use `int(''.join(map(str, bits)), 2)` but implement it manually.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.