Largest number with prime digits | check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
def isPrime ( c ) : NEW_LINE INDENT return ( c == '2' or c == '3' or c == '5' or c == '7' ) NEW_LINE DEDENT def decrease ( s , i ) : NEW_LINE INDENT if ( s [ i ] <= '2' ) : NEW_LINE INDENT s . pop ( i ) NEW_LINE s [ i ] = '7' NEW_LINE DEDENT elif ( s [ i ] == '3' ) : NEW_LINE INDENT s [ i ] = '2' NEW_LINE DEDENT elif ( s [ i ] <= '5' ) : NEW_LINE INDENT s [ i ] = '3' NEW_LINE DEDENT elif ( s [ i ] <= '7' ) : NEW_LINE INDENT s [ i ] = '5' NEW_LINE DEDENT else : NEW_LINE INDENT s [ i ] = '7' NEW_LINE DEDENT DEDENT def primeDigits ( s ) : NEW_LINE INDENT s = [ i for i in s ] NEW_LINE i = 0 NEW_LINE while i < len ( s ) : NEW_LINE INDENT if ( isPrime ( s [ i ] ) == False ) : NEW_LINE INDENT while ( s [ i ] <= '2' and i >= 0 ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if ( i < 0 ) : NEW_LINE INDENT i = 0 NEW_LINE decrease ( s , i ) NEW_LINE DEDENT else : NEW_LINE INDENT decrease ( s , i ) NEW_LINE DEDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT s [ j ] = '7' NEW_LINE DEDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return " " . join ( s ) NEW_LINE DEDENT s = "45" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "1000" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "7721" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "7221" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "74545678912345689748593275897894708927680" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE