0

if you have a list of values:

values=['130','90','150','123','133','120','160',
        '45','67',
        '55','34','130','120','180','130','10']

and wanted to scan through with a window size of 6 and if 4 out of the 6 were >= 100 then keep scanning until there were 3 in a row that were < 100 and then not include those in the list

so for example with an empty list called results:

results=[]

i would like to append those values that satisfied the criteria into the empty list to get

results=[('130','90','150','123','133','120','160'),
         ('55','34','120','180','130','10')]

i know i have convert all the strings into integers with int() but that's not the part that i'm having trouble with. i'm having trouble finding the 4 out of the window size 6 that are >= 100, adding that to a list, AND THEN going from where i left off in the window

In the Chou Fasman Algorithm each window size is 6 and if 4 are greater than 100 then all 6 are included and its extended until 4 consecutive values (i'm going to do 3 instead) are less than 100 (those 4 (or 3) are not included) and then the window starts again from that spot making a new list.

so first window would be:

['130','90','150','123','133,'120'] #and more than 4 are greater than
# 100 so that starting point is stored and the next window is checked
['90','150','123','133','120','160'] #again there are 4 greater than 
# 100 so the next window is checked
['150','123','133','120','160','45'] #again
['123','133','120','160','45','67'] #again
['133','120','160','45','67','55'] #stop and assign values '130' to '160'
# into a list and then start the new window from where it left off

Results=[('130','90','150','123','133','120','160')]

['120','160','45','67','55','34'] # skips
['160','45','67','55','34','130'] # skips
['45','67','55','34','130','120'] # skips
['67','55','34','130','120','180'] # skips
['55','34','130','120','180','130'] # new list in Results starts with '55'
['34','130','120','180','130','10'] # sequence ends and this window still 
# fits criteria so include these into the list so the results would now be

Results=[('130','90','150','123','133','120','160'),      
         ('55','34','130','120','180','130','10')]

I'd really like to use For loops and stay clear of yields and generators if possible but if that's the only way then so be it

eyquem
  • 26,771
  • 7
  • 38
  • 46
O.rka
  • 29,847
  • 68
  • 194
  • 309
  • "I'd really like to use For loops and stay clear of yields and generators if possible"? That's a strange restriction! Why? – johnsyweb Dec 11 '11 at 07:28
  • when ever i use yields in my functions they always freak out and don't work . this is is a smaller part in a much larger function . i have the other parts finished but this is the main part that i can't finish – O.rka Dec 11 '11 at 07:31
  • It sounds like you are making your functions too big and they're almost certainly not obeying the [SRP](http://en.wikipedia.org/wiki/Single_responsibility_principle). I'm not aware of any problems with `yield` causing functions to "freak out". – johnsyweb Dec 11 '11 at 07:33
  • See also [this answer](http://stackoverflow.com/a/6998381/78845) to [this question](http://stackoverflow.com/q/6998245/78845). **WARNING:** Contains `yield`. – johnsyweb Dec 11 '11 at 07:36
  • @draconisthe0ry Could you explain what should happen and what should be the result with values=['130','190','150','1','133','120','160','45','67','258','589','78','55','34','130','120'‌​,'180','130','10'] , that is to say there are 3 numbers<100 in ['1','133','120','160','45','67'] – eyquem Dec 11 '11 at 09:18
  • WOW, _all_ of that was in one sentence. – Jeff Mercado Dec 11 '11 at 09:29
  • @Jeff Mercado What do you mean ? – eyquem Dec 11 '11 at 09:32
  • @eyquem: Tell me you read the whole question and could completely understand what he's saying. – Jeff Mercado Dec 11 '11 at 09:43
  • @Jeff Mercado Do you want to mean _"whole title"_ ? But this title isn't strictly equivalent to the body of the question. In the title, we must eliminate the last 3 numbers, while in the question we must eliminate the 3 nuùbers<100 that cause the criterion being not fitted – eyquem Dec 11 '11 at 09:50
  • @eyquem: No, title _and_ body. Seriously, try to actually read it. – Jeff Mercado Dec 11 '11 at 10:27
  • @Jeff Sorry, I don't understand what you want to point out. – eyquem Dec 11 '11 at 11:18

1 Answers1

1

Would you like to try with this to see if it suits your requirement?

for i in xrange(0,len(values)):
    results[-1].append(values[i])
    if len(filter(lambda x:int(x)<100,results[-1][-3:])) == 3:
        results.append(results[-1][-2:])
        results[-2]=results[-2][:-3]
        if len(results[-2]) == 0:
            del results[-2]


>>> results
[['130', '90', '150', '123', '133', '120', '160'], ['55', '34', '130', '120', '180', '130', '10']]
>>> 
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • @draconisthe0ry This code gives **[ ['130', '190', '150', '1', '133', '120', '160', '45', '67', '258', '589'] , ['34', '130', '120', '180', '130', '10'] ]** with ``values=['130','190','150','1','133','120','160','45','67','258','589','78','55','34','130','120','180','130','10']`` : there are 3 numbers < 100 in the first element. Is it the whished result ? – eyquem Dec 11 '11 at 09:23
  • @eyquem Well there was a problem in my previous code which was giving a wrong result. I have corrected it now :-). With the above input the result show now be `[['130', '190', '150', '1', '133', '120', '160', '45', '67', '258', '589'], ['55', '34', '130', '120', '180', '130', '10']]` – Abhijit Dec 11 '11 at 09:31
  • @draconisthe0ry Abhijit, now there are 1,45,67 in the first element, and 55,34,10 in the second element in **results**, while it seems that draconisthe0ry doesn't want 3 numbers < 100 in the results' elements – eyquem Dec 11 '11 at 09:46
  • @eyquem As per the problem statement `until there were 3 in a row that were < 100` – Abhijit Dec 11 '11 at 10:14
  • @eyquem Your problem would be much easier to solve. Maintain a counter of number's less then 100 and split whenever the counter is 3. – Abhijit Dec 11 '11 at 10:33
  • @Abhijit If you cut so the citation, the impression is that these 3 numbers<100 must be kept present in one results ' element. But there is a succession of words that must be taken as a whole: _"until there were 3 in a row that were < 100 and then not include those in the list"_ . As soon as there are 3 numbers<100 in a window, the scanning must stop **AND** some of them must be thrown away. From that, I understand that each element of **results** must have less than 3 numbers<100 – eyquem Dec 11 '11 at 11:30
  • @Abhijit One of the problems is that I wonder if these 3 numbers<100 that trigger the stop of the scanning must be successive (at the end) or not. And another linked problem is: the numbers not to kept are only the last ones or former ones too (as 1 in my example) ? - I think that only the OP could clear the requirements, but I fear that he has gone away fully happy of one answer in which he doesn't perceive the potential issues. – eyquem Dec 11 '11 at 11:33
  • @eyquem `And another problem is: the numbers not to kept are only the last ones or former ones too (as 1 in my example) `. If this to be true then the second example of the OP should be incorrect – Abhijit Dec 11 '11 at 11:35
  • @Abhijit What can be true ? The citation is badly written but it is a question: _"And another problem is: are the numbers not to kept only the last ones, or former ones too (as 1 in my example) ?"_ – eyquem Dec 11 '11 at 11:47
  • @Abhijit In fact, the more I think about the problem, the more I'm convinced that OP doesn't want to obtain potions of the initial list **values** having less than 3 numbers<100; but he wants to obtain portions of the initial list **values** in each of which it can't be possible to find a succession of 6 elements containing more than 2 numbers < 100. Additionally, as he is not interested by these numbers<100, then he doesn't keep any of such numbers at the end of each portion. I mean that there can be more than 2 numbers<100 in a **returns** ' element, and then your code seems correct, Abhiji. – eyquem Dec 11 '11 at 12:10
  • the reason why it should include '10' at the end is because the previous window [55,34,130,120,180,130] satisfied the requirement b/c more than 4 are > 100 . next window [34,130,120,180,130,10] which still satisfies the requirement of 4 elements > 100 but the list ends at that point. since both windows fit the criteria they would both be accepted into the last acceptance list . i put a new set of values into the function values=['70', '151', '101', '57', '113', '108', '100', '142', '142', '70', '70', '70', '70', '151'] and the first window worked but the second window was [70, 70, 151] ? – O.rka Dec 11 '11 at 21:15