Given a large number , check if a subsequence of digits is divisible by 8 | Python3 program to check if given string has a subsequence divisible by 8 ; map key will be tens place digit of number that is divisible by 8 and value will be units place digit ; For filling the map let start with initial value 8 ; key is digit at tens place and value is digit at units place mp . insert ( { key , value } ) ; Create a hash to check if we visited a number ; Iterate from last index to 0 th index ; If 8 is present in string then 8 divided 8 hence print yes ; considering present character as the second digit of two digits no we check if the value of this key is marked in hash or not If marked then we a have a number divisible by 8 ; If no subsequence divisible by 8
Str = "129365" NEW_LINE mp = { } NEW_LINE no = 8 NEW_LINE while ( no < 100 ) : NEW_LINE INDENT no = no + 8 NEW_LINE mp [ ( no // 10 ) % 10 ] = no % 10 NEW_LINE DEDENT visited = [ False ] * 10 NEW_LINE for i in range ( len ( Str ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( Str [ i ] == '8' ) : NEW_LINE INDENT print ( " Yes " , end = " " ) NEW_LINE break NEW_LINE DEDENT if visited [ mp [ ord ( Str [ i ] ) - ord ( '0' ) ] ] : NEW_LINE INDENT print ( " Yes " , end = " " ) NEW_LINE break NEW_LINE DEDENT visited [ ord ( Str [ i ] ) - ord ( '0' ) ] = True NEW_LINE DEDENT if ( i == - 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT