-1

if you have a list of values:

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

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 4 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','180']

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

i was trying:

count=0
for i in values:
    if int(i)=> 100:
        count=count+1

but it wasn't working


the window size is 6 so the first window would be ['130','90','150','123','133','120'] and 5 out of 6 of those values are over 100 so progress one place over and then window would be ['90','150','123','133','120','160'] again it would be 5 out of 6 being over 100 so it would progress over one more place . not until it reaches the window with ['160','180','45','67','55','34'] would it stop b/c there were 4 consecutive values less than 100 in a row. so it would put all the values from '130' to '180' in the list. Hope that explains it better

O.rka
  • 29,847
  • 68
  • 194
  • 309

3 Answers3

0

You can use this code to find the index of the start of your window:

ints = map(int, values)
for i in range(len(ints) - 6):
    if sum(x >= 100 for x in ints[i : i + 6]) >= 4:
        return i

It can be implemented more efficiently by using a sliding window where you have a buffer of 6 elements and at each step add an element and remove one, adjust the running count by +1, 0 or -1.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

I'd try something like this.

results = []
for i, v in enumerate(values):
    if sum(1 for x in values[max(0, i - 3): i + 2] if int(x) > 100) >= 4:
        results.append(v)

It's not clear from the question where your windows are, nor what happens at the ends but I've made reasonable assumptions I think.

  • the window size is 6 so the first window would be ['130','90','150','123','133','120'] and 5 out of 6 of those values are over 100 so progress one place over and then window would be ['90','150','123','133','120','160'] again it would be 5 out of 6 being over 100 so it would progress over one more place . not until it reaches the window with ['160','180','45','67','55','34'] would it stop b/c there were 4 consecutive values less than 100 in a row. so it would put all the values from '130' to '180' in the list. Hope that explains it better – O.rka Dec 11 '11 at 00:02
0
def windows(values):
    index = 0
    window_size = 6
    while True:
        yield values[index:index + window_size]
        index += 1
        if index > len(values) - window_size:
            return

def are_four_above_100(window):
    count = 0
    for element in window:
        if int(element) > 100:
            count += 1
     if count > 4:
         return True
     else:
         return False

for i, window in enumerate(windows(values)):
    if not are_four_above_100(window):
        print 'found window at index %s' % i
        break

found window at index 4

Once you find that index you can just do your much simpler check for 4 in a row below 100. If you still need help let us know. I'm sure someone else can condense this down :), but that wasn't my concern.

Derek Litz
  • 10,529
  • 7
  • 43
  • 53
  • k cool i'm going to try and figure it out how to put it into one function. i'm not to familiar with yield but i'm creating a pretty complex function and this is the most difficult part for me to figure out . thanks a lot! – O.rka Dec 11 '11 at 02:08
  • ok so when you get the generator object from the window function. you can see the first window when you do .next() but how can you put those values into another list or function? – O.rka Dec 11 '11 at 02:44
  • Also, my use of the windows generator may have been masked above with the enumerate (in the for loop). – Derek Litz Dec 11 '11 at 03:53
  • i can't really grasp my mind around how these generators work in complicated scenarios . i'm working something out right now with for loops – O.rka Dec 11 '11 at 05:11