Question
A nonempty slice of integers L [p: q] is unimodal if there is some number m ∈ N such that p ≤ m < q, L [p: m+1] is increasing, and L [m: q] is decreasing. Such a number m, if it exists, is called the mode of L [p: q].
Prove correctness for following -
Precondition: L is a list of integers, p,q ∈ N, and 0 ≤ p < q ≤ len(L).
Postcondition: Return the mode of L[p : q] if L[p : q] is unimodal; otherwise return -1.
Mode(L, p, q)
1) u = p ; v = q-1 ; w= 1
2) while w ?= 0 and u < v :
3) if L[u] < L [u+1]: u= v + 1
4) else if L[ v-1] >l[v]: v=v-1
5) else: w=0
6) if w ?= 0 : return u
7) else: return -1