2

I have three lists (the container*) and they need to pop one(the first) element at a time. Which list will pop the element depends on a value (tF*), the smallest value sends first, but if the values are equal then it goes in the alphabetical order by the names of list(container1 sends first), the list that go into the sending are the ones that have elements in it. Also, the tF* are calculatet per element, so if there are two elements "1" than first pops one, and in the next round(when the "2's" arrive) it pop the other one.
It's in fact a simulation on fair queuing that routers use when they have more flows incoming... I just can't figure out that popping(sending) part, any help is appreciated.
here is the code I got so far (python)

flow1 = [1,2,4,6]
flow2 = [2,6]
flow3 = [1,2,3,5,6]

container1 = []
container2 = []
container3 = []

t = 1
tA=1
tF1 = 0
tF2 = 0
tF3 = 0
counter_tA = 0

while flow1[0] or flow2[0] or flow3[0] or container1[0] or container2[0] or container3[0]:
#from flow to the container                                                                                                                                                            
    if flow1[0] == t:
        container1.append(flow1.pop(0))
        tF1 = max(tF1, tA)+1
        counter_tA += 1
    if flow2[0] == t:
        container2.append(flow2.pop(0))
        tF2 = max(tF2, tA)+1
        counter_tA += 1
    if flow3[0] == t:
        container3.append(flow3.pop(0))
        tF3 = max(tF3, tA)+1
        counter_tA += 1

#sending                                                                                                                                                                  
    send = min(tF1, min(tF2, tF3))

    if tF1 == tF2 == tF3:
        container1.pop()
    if tF1 == (tF2 or tF3):


    tA = tA + 1/counter_tA
    counter_tA =0
   t+=1
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
arbast
  • 145
  • 7
  • Wherever you have `x1`, `x2`, `x3`, you are making more work for yourself by not using an actual data structure. – Gareth Latty May 18 '13 at 10:06
  • Also, [the `sorted()` built-in](http://docs.python.org/3.3/library/functions.html#sorted) takes a `key` keyword argument, which is a function that gives the ordering for the item. You can use that to sort your items correctly. – Gareth Latty May 18 '13 at 10:07
  • you mean i add for ex container1[][1] and then sort the three lists by that and then look which ha the lowest tF? – arbast May 18 '13 at 10:16
  • 1
    `.pop(0)` is `O(N)`, whenever you see yourself doing `.pop(0)` or any `pop` near the start of the list where each item in the list must be shifted to the left, you should consider using a `collections.deque` – jamylak May 18 '13 at 10:59
  • thank you for that, but in this example i don't really care :D I will have that in mind in my further programming :) – arbast May 18 '13 at 12:09

0 Answers0