# 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 ; 
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))

# Expected output:
777