Tuesday, 20 September 2016

List Alternating Values

Define a Python function "alternating(l)" that returns True if the values in the input list alternately go up and down (in a strict manner).


def alternating(l):
if(len(l)>1):
if(l[0]>l[1]):
for i in range(len(l)-1):
if(l[i]==l[i-1]):
return False
if(i%2==0):
if(l[i]<l[i+1]):
return False
if(l[0]<l[1]):
for i in range(len(l)-1):
if(i%2==0):
if(l[i]>l[i+1]):
return False

return True

No comments:

Post a Comment