Binary string with given frequencies of sums of consecutive pairs of characters | A Function that generates and returns the binary string ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code
def build_binary_str ( p , q , r ) : NEW_LINE INDENT ans = " " NEW_LINE if ( q == 0 ) : NEW_LINE INDENT if ( p != 0 and r != 0 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT else : NEW_LINE INDENT if ( p ) : NEW_LINE INDENT temp = " " NEW_LINE for i in range ( p + 1 ) : NEW_LINE INDENT temp += '0' NEW_LINE DEDENT ans = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp = " " NEW_LINE for i in range ( r + 1 ) : NEW_LINE INDENT temp += '1' NEW_LINE DEDENT ans = temp NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 1 , q + 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT else : NEW_LINE INDENT ans += '1' NEW_LINE DEDENT DEDENT temp = " " NEW_LINE for i in range ( p ) : NEW_LINE INDENT temp += '0' NEW_LINE DEDENT st = " " NEW_LINE st += ans [ 0 ] NEW_LINE st += temp NEW_LINE for i in range ( 1 , len ( ans ) ) : NEW_LINE INDENT st += ans [ i ] NEW_LINE DEDENT ans = st NEW_LINE temp = " " NEW_LINE for i in range ( r ) : NEW_LINE INDENT temp += '1' NEW_LINE DEDENT ans = temp + ans NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 1 NEW_LINE q = 2 NEW_LINE r = 2 NEW_LINE print ( build_binary_str ( p , q , r ) ) NEW_LINE DEDENT