Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver code
def hasEqualBlockFrequency ( N ) : NEW_LINE INDENT S = bin ( N ) . replace ( "0b " , " " ) NEW_LINE p = set ( ) NEW_LINE c = 1 NEW_LINE for i in range ( len ( S ) - 1 ) : NEW_LINE INDENT if ( S [ i ] == S [ i + 1 ] ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT p . add ( c ) NEW_LINE c = 1 NEW_LINE DEDENT p . add ( c ) NEW_LINE DEDENT if ( len ( p ) == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE hasEqualBlockFrequency ( N ) NEW_LINE