Check if decimal representation of Binary String is divisible by 9 or not | Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary string ; Traverse the binary string ; Stores 3 consecutive characters of the binary string ; Append octal representation of temp ; Function to check if binary string is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the string oct ; Update oddSum ; Traverse the string oct ; Update evenSum ; Stores cotal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code
def ConvertequivalentBase8 ( S ) : NEW_LINE INDENT mp = { } NEW_LINE mp [ "000" ] = '0' NEW_LINE mp [ "001" ] = '1' NEW_LINE mp [ "010" ] = '2' NEW_LINE mp [ "011" ] = '3' NEW_LINE mp [ "100" ] = '4' NEW_LINE mp [ "101" ] = '5' NEW_LINE mp [ "110" ] = '6' NEW_LINE mp [ "111" ] = '7' NEW_LINE N = len ( S ) NEW_LINE if ( N % 3 == 2 ) : NEW_LINE INDENT S = "0" + S NEW_LINE DEDENT elif ( N % 3 == 1 ) : NEW_LINE INDENT S = "00" + S NEW_LINE DEDENT N = len ( S ) NEW_LINE octal = " " NEW_LINE for i in range ( 0 , N , 3 ) : NEW_LINE INDENT temp = S [ i : i + 3 ] NEW_LINE if temp in mp : NEW_LINE octal += ( mp [ temp ] ) NEW_LINE DEDENT return octal NEW_LINE DEDENT def binString_div_9 ( S , N ) : NEW_LINE INDENT octal = ConvertequivalentBase8 ( S ) NEW_LINE oddSum = 0 NEW_LINE evenSum = 0 NEW_LINE M = len ( octal ) NEW_LINE for i in range ( 0 , M , 2 ) : NEW_LINE INDENT oddSum += ord ( octal [ i ] ) - ord ( '0' ) NEW_LINE DEDENT for i in range ( 1 , M , 2 ) : NEW_LINE INDENT evenSum += ord ( octal [ i ] ) - ord ( '0' ) NEW_LINE DEDENT Oct_9 = 11 NEW_LINE if ( abs ( oddSum - evenSum ) % Oct_9 == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1010001" NEW_LINE N = len ( S ) NEW_LINE print ( binString_div_9 ( S , N ) ) NEW_LINE DEDENT