0

I know the question can be unclear. I will attempt to explain.

I have a scenario where I need to verify for a sequence of values 5,10,15,20...., only that the system that produces sequence is not very accurate that sometimes it can miss the values or repeat couple of values like 5, 10,10,20,25,30,40.

So as you can see there is a tolerance of 5-10.

This is okay. I have to verify that the sequence does not go backwards like 5,10,15,10.. or 5,10,9,....

I need to write a PASS FAIL condition using a logic that Passes if it sees monotonically increasing sequence with occasional misses or repeats, fails if it sees values go backwards or differ by more than 10 (like 5, 20, 15...)

Can someone please help me identify right logic for this

E.S
  • 536
  • 1
  • 9
  • 20
user2927392
  • 249
  • 3
  • 11

3 Answers3

1

One-liner:

def validate(li):
    return all(x==y or 5<=(y-x)<=10 for x,y in zip(li,li[1:]))

If you really want to assert that your data originates from a monotonically-increasing-by-5 source, you also might want to tack on some logic that the max value of your sequence (li[-1]) is approximately 5*len(li) + li[0].

roippi
  • 25,533
  • 4
  • 48
  • 73
0

How about a simple:

previousVal = ###
actualVal = ###

difference = actualVal - previousVal

if 0 <= difference <= 10  ## Change acc. to your requirement. Unclear if its 5-10 or 0-10
    #PASS
else:
    #FAIL!

If its a list that you are getting as input, then you can zip the original list with a shifted-by-1 list, and iterate over the tuple.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

If the sequence is a list, l, you can use this:

'FAIL' if any(l[i]<l[i-1] or l[i]-l[i-1]>10 for i in xrange(1,len(l))) else 'PASS'
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52