Suppose we have a number num, we have to check whether the binary representation of num has the same number of consecutive blocks of 0s and 1s. We have to keep in mind that 0 and a number with all 1s are not considered to have number of blocks of 0s and 1s.
So, if the input is like num = 455, then the output will be True, as the binary representation of this number is 111000111.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(num): bin_form = bin(num).replace("0b", "") one_count = set() count = 1 for i in range(len(bin_form)-1): if bin_form[i] == bin_form[i + 1]: count += 1 else: one_count.add(count) count = 1 if len(one_count) == 1: return True return False num = 455 print(solve(num))