Problem p04045 (python)

Fixed submission:
def run_code(N, K, ds):

    dislike_digits_set = set(i for i in ds)
    avail_digits = [i for i in range(10) if i not in dislike_digits_set]
    avail_min = avail_digits[0]

    def get_next_digit(d):
        last = 10
        for ad in (avail_digits + [last]):
            if ad >= d:
                if ad == last:
                    return (1, avail_min)
                else:
                    return (0, ad)

    def fill_last_kth_digits(n_digits, d, k):
        n = len(n_digits)
        for i in range(k):
            n_digits[n - i - 1] = d
        return n_digits

    def to_num(n_digits):
        num = 0
        n = len(n_digits)
        for i in range(n):
            num += n_digits[i] * 10**(n-i-1)
        return num

    def to_digits(num):
        return [int(i) for i in str(num)]

    def search_nearest_larger(num):
        n_digits = to_digits(num)
        n = len(n_digits)

        for i in range(n):
            d_in = n_digits[i]
            inc, d_out = get_next_digit(d_in)
            if inc == 0 and d_in == d_out:
                continue
            if inc > 0:
                num += 10**(n-i)
                n_digits = fill_last_kth_digits(to_digits(num), avail_min, n-i)
                return search_nearest_larger(to_num(n_digits))
            else:
                n_digits[i] = d_out
                return to_num(fill_last_kth_digits(n_digits, avail_min, n-i-1))

        return to_num(n_digits)

    return search_nearest_larger(N)


N, K = [int(i) for i in input().split()]
ds = [int(i) for i in input().split()]
res = run_code(N, K, ds)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
ds = [int(i) for i in input().split()]

#N, K = (99999, 8)
#ds = [0,1,2,3,4,5,6,7,9]

def run_code(N, K, ds):

    dislike_digits_set = set(i for i in ds)
    avail_digits = [i for i in range(10) if i not in dislike_digits_set]
    avail_min = avail_digits[0]

    def get_next_digit(d):
        last = 10
        for ad in (avail_digits + [last]):
            if ad >= d:
                if ad == last:
                    return (1, avail_min)
                else:
                    return (0, ad)

    def to_num(n_digits):
        num = 0
        n = len(n_digits)
        for i in range(n):
            num += n_digits[i] * 10**(n-i-1)
        return num

    def to_digits(num):
        return [int(i) for i in str(num)]

    def search_nearest_larger(num):
        n_digits = to_digits(num)
        n = len(n_digits)

        for i in range(n):
            d_in = n_digits[i]
            inc, d_out = get_next_digit(d_in)
            if inc == 0 and d_in == d_out:
                continue
            if inc > 0:
                num += 10**(n-i)
                return search_nearest_larger(num)
            else:
                n_digits[i] = d_out
                return to_num(n_digits[:i+1] + [avail_min]*(n-i-1))

        return to_num(n_digits)

    return search_nearest_larger(N)



res = run_code(N, K, ds)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = [int(i) for i in input().split()]
ds = [int(i) for i in input().split()]

#N, K = (99999, 1)
#ds = [0]

def run_code(N, K, ds):

    dislike_digits_set = set(i for i in ds)
    avail_digits = [i for i in range(10) if i not in dislike_digits_set]
    avail_min = avail_digits[0]

    def get_next_digit(d):
        last = [10]
        for ad in (avail_digits + last):
            if ad >= d:
                if ad == last:
                    return (1, avail_min)
                else:
                    return (0, ad)

    def to_num(n_digits):
        num = 0
        n = len(n_digits)
        for i in range(n):
            num += n_digits[i] * 10**(n-i-1)
        return num

    def to_digits(num):
        return [int(i) for i in str(num)]

    def search_nearest_larger(num):
        n_digits = to_digits(num)
        n = len(n_digits)

        for i in range(n):
            d_in = n_digits[i]
            inc, d_out = get_next_digit(d_in)
            if inc == 0 and d_in == d_out:
                continue

            if inc > 0: