human
I am working on the file "Generating_permutations_with_Python.ipynb".
The first task is:
Then we need a function to scan the array a, from its beginning, to find the largest movable element.
This can cost upto a time of $O(n)$ (if $n = #a$), but it could hardly be improved.
Can you write Python program for it?

=====
What is a Python program to find the largest movable element?
assistant
#Here is Python program to find the largest movable element.

def scan_largest_movable(a):
    """Find the largest movable element."""
    def aux(acc, i):
        if i >= len(a):
            return acc
        else:
            if not is_movable(a, i):
                return aux(acc, i + 1)
            else:
                x, _ = a[i]
                if acc is None:
                    return aux(i, i + 1)
                else:
                    j = acc if x < a[acc][0] else i
                    return aux(j, i + 1)
    return aux(None, 0)