Holding until next value change
Arshpreet Singh
arsh840 at gmail.com
Sat Aug 20 03:38:25 EDT 2016
On Saturday, 20 August 2016 11:38:03 UTC+5:30, Steve D'Aprano wrote:
> On Sat, 20 Aug 2016 02:53 pm, Arshpreet Singh wrote:
>
> > I am writing a function as main_call() which is continuously producing
> > values. (+ve or -ve) I want to print on screen only for first +ve value
> > and hold until -ve value comes around. here is my code:
> >
> >
> > def main_call():
> > while True:
> > yield strategy()
> >
> > for value in main_call():
> > if(value>0):
> > print '+ve'
> > elif(value>0):
> > print '-ve'
> > else:
> > pass
> >
> > Do I need to use threads or processes?
>
> Only if you want a major headache.
>
>
> Your code doesn't do what you want it to do. It prints "+ve" every time it
> sees a positive value, not just the first time.
>
> One solution to this is to think of a state machine: your machine starts off
> in the state:
>
> (1) Ignore negative values, print the first positive value you see,
> then change to the next state.
>
> The second state is:
>
> (2) Ignore positive values, print the first negative value you see,
> then change to the next state.
>
> The third state is unspecified. You don't know say what it is, so I'm going
> to guess that you change to a third state:
>
> (3) Don't ignore anything, print every value you see.
>
>
> So here are our three machines:
>
> def ignore_negative(x):
> if x < 0:
> return False
> else:
> print("+ve")
> return True
>
> def ignore_positive(x):
> if x > 0:
> return False
> else:
> print("-ve")
> return True
>
> def ignore_nothing(x):
> if x > 0:
> print("+ve")
> else:
> print("-ve")
> return False
>
>
> Here is a table that specifies the changes in state:
>
> TABLE = { # current state: next state
> ignore_negative: ignore_positive,
> ignore_positive: ignore_nothing,
> }
>
>
> And some code to drive it:
>
>
> state = ignore_negative # DON'T call the function yet
> for value in main_call():
> print(value) # for testing
> if state(value):
> print("changing state")
> state = TABLE[state]
Hi Steve, my third state is if x==0 , I tired the same code but here is output I am getting, so we are changing the state even it is printing out the +ve as well as -ve values
8.7664352813e-05
+ve
changing state
8.7664352813e-05
8.7664352813e-05
8.7664352813e-05
8.7664352813e-05
8.7664352813e-05
8.76628243948e-05
8.30490294982e-05
8.30490294982e-05
6.45892873188e-05
6.45892873188e-05
6.45892873188e-05
6.45892873188e-05
-4.61232547941e-06
-ve
changing state
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
-4.61232547941e-06
-ve
More information about the Python-list
mailing list