Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Perform an action if N is within a pre-determined range

I have a number generator/counter that will output increasingly higher values ranging from 1 to 1,000.

Once it reaches 1,000, the count is reset and restarts.

The total numbers generated between 1--1,000 is variable and random.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The delta between numbers is variable and random.

Example:

 1, 45, 120, 300, 590, 700, 750, 967
 3,  4, 212, 400, 660, 800, 850, 901, 967, 998
50, 90, 121, 603, 780, 833, 890

I need to trigger an action every time the counter passes a certain pre-determined threshold. For example, if the threshold were 100th, that would mean if a new hundred-handle is generated, that would trigger the action.

100 -> action
120
350 -> action
355
378
490 -> action
600 -> action
601
677
905 -> action

I’m blocked in how to resolve this. I though of using the modulus operator but that won’t suffice as the generated numbers are unpredictable and their divisibility cannot be pre-determined.

Any ideas how to accomplish this task? TIA

>Solution :

You can use floor division by your step, then compute the successive differences to identify the groups:

group = a//100
# array([0, 0, 1, 3, 5, 7, 7, 9, 0, 0, 2, 4, 6, 8, 8, 9, 9, 9, 0, 0, 1, 6,
#        7, 8, 8])

actions = np.r_[True, np.diff(group)!=0]
# array([ True, False,  True,  True,  True,  True, False,  True,  True,
#        False,  True,  True,  True,  True, False,  True, False, False,
#         True, False,  True,  True,  True,  True, False])

As a single array:

np.vstack([a, actions]).T

array([[  1,   1],
       [ 45,   0],
       [120,   1],
       [300,   1],
       [590,   1],
       [700,   1],
       [750,   0],
       [967,   1],
       [  3,   1],
       [  4,   0],
       [212,   1],
       [400,   1],
       [660,   1],
       [800,   1],
       [850,   0],
       [901,   1],
       [967,   0],
       [998,   0],
       [ 50,   1],
       [ 90,   0],
       [121,   1],
       [603,   1],
       [780,   1],
       [833,   1],
       [890,   0]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading