human
Largest number less than N whose each digit is prime number | Number is given as string ..
We stop traversing digits , once it become smaller than current number . For that purpose we use small variable ..
Array indicating if index i ( represents a digit ) is prime or not ..
Store largest.
If there is only one character , return the largest prime less than the number.
If number starts with 1 , return number consisting of 7.
Traversing each digit from right to left Continue traversing till the number we are forming will become less ..
If digit is prime , copy it simply ..
If not prime , copy the largest prime less than current number.
If not prime , and there is no largest prime less than current prime.
Make current digit as 7 Go left of the digit and make it largest prime less than number . Continue do that until we found a digit which has some largest prime less than it.
If the given number is itself a prime ..
Make last digit as highest prime less than given digit ..
If there is no highest prime less than current digit ..
Once one digit become less than any digit of input replace 7 ( largest 1 digit prime ) till the end of digits of number.
If number include 0 in the beginning , ignore them . Case like 2200.
Driver Program"
How can the above be solved in Python?
assistant
#Here is code to largest number less than N whose each digit is prime number and the other steps required.
 def PrimeDigitNumber ( N , size ) :
	ans = [ " " ] * size
	ns = 0 ;
	small = 0 ;
	p = [ 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 ]
	prevprime = [ 0 , 0 , 0 , 2 , 3 , 3 , 5 , 5 , 7 , 7 ]
	if ( size == 1 ) :
		ans [ 0 ] = prevprime [ ord ( N [ 0 ] ) - ord ( '0' ) ] + ord ( '0' ) ;
		ans [ 1 ] = ' ' ;
		return ' ' . join ( ans ) ;
	if ( N [ 0 ] == '1' ) :
		for i in range ( size - 1 ) :
			ans [ i ] = '7'
		ans [ size - 1 ] = ' ' ;
		return ' ' . join ( ans )
	i = 0
	while ( i < size and small == 0 ) :
		if ( p [ ord ( N [ i ] ) - ord ( '0' ) ] == 1 ) :
			ans [ ns ] = N [ i ]
			ns += 1
		else :
			if ( p [ ord ( N [ i ] ) - ord ( '0' ) ] == 0 and prevprime [ ord ( N [ i ] ) - ord ( '0' ) ] != 0 ) :
				ans [ ns ] = prevprime [ ord ( N [ i ] ) - ord ( '0' ) ] + ord ( '0' ) ;
				small = 1
				ns += 1
			elif ( p [ ord ( N [ i ] ) - ord ( '0' ) ] == 0 and prevprime [ ord ( N [ i ] ) - ord ( '0' ) ] == 0 ) :
				j = i ;
				while ( j > 0 and p [ ord ( N [ j ] ) - ord ( '0' ) ] == 0 and prevprime [ ord ( N [ j ] ) - ord ( '0' ) ] == 0 ) :
					ans [ j ] = N [ j ] = '7' ;
					N [ j - 1 ] = prevprime [ ord ( N [ j - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) ;
					ans [ j - 1 ] = N [ j - 1 ] ;
					small = 1 ;
					j -= 1
				i = ns
		i += 1
	if ( small == 0 ) :
		if ( prevprime [ ord ( N [ size - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) != ord ( '0' ) ) :
			ans [ size - 1 ] = prevprime [ ord ( N [ size - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) ;
		else :
			j = size - 1 ;
			while ( j > 0 and prevprime [ ord ( N [ j ] ) - ord ( '0' ) ] == 0 ) :
				ans [ j ] = N [ j ] = '7' ;
				N [ j - 1 ] = prevprime [ ord ( N [ j - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) ;
				ans [ j - 1 ] = N [ j - 1 ] ;
				small = 1 ;
				j -= 1
	while ( ns < size ) :
		ans [ ns ] = '7'
		ns += 1
	ans [ ns ] = ' ' ;
	k = 0 ;
	while ( ans [ k ] == '0' ) :
		k += 1
	return ( ans + k )
if __name__ == " _ _ main _ _ " :
	N = "1000" ;
	size = len ( N ) ;
	print ( PrimeDigitNumber ( N , size ) )