Determine if values in a list are ascending / descending

Here is my problem. I need to determine where the values in the list are ascending and descending. Also as these values will be in degrees they could reverse through 0 deg.

Any clues

deg = [10,12,15,30,45,60,55,51,30,20,10,9,15,22,29,40,52,60,60,60,1,30,29,20,9,0,355,344]




if deg == sorted(deg, reverse=True):
    print('true')
if deg == sorted(deg, reverse=False):
    print('False')

How about iterating through the pairs of consecutive elements? If the next pair was in the same ascending= / descending order as the previous pair, append the second element of it to a sub list. Otherwise if it’s in a different order, yield the sub list, and start a new one with both items in the pair (e.g. make a list from the 2uple).

Decide how you want to treat equality.

This means they are always in ascending and descending order - if you think you have a pair in the other direction, it’s actually just a step with close to 360 degree rotation. You will need to select a method to correct for this. It’s probably most sensible to select the shorter absolute distance.

Otherwise you want to follow what @JamesParrott suggested: step through the list pairwise, use a helper function to compute the shorter delta between two functions (in the range [-180, 180]) and then check if the sign is the same as in the previous iteration.

1 Like