#223 · Bit manipulation · Beginner · 30 XP · about 6 min
A sensor reports its readings as text made of the characters 0 and 1, sometimes with spaces that split the digits into groups to make them easier to read, like '1010 0110'. Write binary_value(bits) that returns the whole number those binary digits stand for. Spaces are ignored.
Read the digits from left to right, the way you would read a decimal number: start with a total of 0, and for each digit, double the total and add the digit. Do it yourself, digit by digit, without int(bits, 2).
binary_value("101") # returns 5 binary_value("1111 1111") # returns 255 binary_value("0001") # returns 1 binary_value("1 0000 0000") # returns 256
For "101" the total goes 0 * 2 + 1 = 1, then 1 * 2 + 0 = 2, then 2 * 2 + 1 = 5.
bits holds at least one digit, and at most 200 characters.First solves only, ranked by stars and then time.