python - Boolean function to check if a list of ints is in increasing order- Must use a while loop -
how approach this? i've tried different ways , i've far . ex: [1, 2, 5, 7]
returns true
, [1, 1, 6, 9]
returns false
. second example doesn't work. returns true though first 2 elements equal. doing wrong?
def increasing(l): n = len(l) = 0 while <= n-1: if l[i] > l[i+1]: return false else: i+=1 return true
problem lies here
while <= n: if l[i] > l[i+1]:
in example, n=4. since array index starts @ 0, list index 0 1 2 3
. checking till n=4 incorrect.
then, doing [i+1] checks 5th element.
replace these lines
while < n-1: if l[i] > l[i+1]:
this take care of index out of range
error
do have use loop here?
no. check python - how check list monotonicity
Comments
Post a Comment