0
def formula(listA, count_zero=0, start=0, result=[], interarrival=1):
    for i in listA:
        if i == 1:
            service_time=3
            result.append(max(start + service_time - (count_zero + 1) * interarrival, 0))
            count_zero = 0
        elif i == 0:
            count_zero += 1
    return result


print(formula([1, 1, 1, 0, 1, 0, 0, 1]))

When I run this I get results #[2, 2, 2, 1, 0], these are not the results I expected.

'''
For the elements in listA = [1, 1, 1, 0, 1, 0, 0, 1]

For the 1st element:
i == 1, because this is the first element, the program is supposed to compute 0 which is why I set the start = 0

For the 2nd element: 
i == 1, the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(0 #start from 1st element + 3 #service_time - (count_zero + 1) #no zeros right before second element * interarrival, 0)
max(0 + 3 - (0 + 1) * 1, 0)
max(3 - (1)*1, 0) 
max(3 - 1, 0)
max(2, 0) #the max of these two numbers is 2 is the programs is supposed to print 2


For the 3rd element: 
i == 1, so the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(2 #start from 2nd element + 3 #service_time - (count_zero + 1) #no zeros right before third element * interarrival, 0)
max(2 + 3 - (0 + 1)*1, 0)
max(5 - 1*1, 0) 
max(5 - 1, 0)
max(4, 0) #the max of these two numbers is 4, the program is supposed to print 4


For the 4th element:
i == 0, the program simply counts this zero and doesn't do anything else, nothing is printed as expected.


For the 5th element: 
i == 1, so the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(4 #start from 2nd element + 3 #service_time - (count_zero + 1) #one zero right before fifth element * interarrival, 0)
max(4 + 3 - (1 + 1)*1, 0)
max(7 - 2*1, 0) 
max(7 - 2, 0)
max(5, 0) #the max of these two numbers is 4, the program is supposed to print 4


For the 6th element:
i == 0, the program simply counts this zero and doesn't do anything else, nothing is printed as expected. 


For the 7th element:
i == 0, the program also simply counts this zero and doesn't do anything else, nothing is printed as expected.


For the 8th element: 
i == 1, so the formula computes:
max(start + service_time - (count_zero + 1) * interarrival, 0)
max(5 #start from 5th element + 3 #service_time - (count_zero + 1) #two zeros right before third element * interarrival, 0)
max(5 + 3 - (2 + 1)*1, 0)
max(8 - 3*1, 0) 
max(5, 0)
max(5, 0) #the max of these two numbers is 5, the program is supposed to print 5

'''

How do I correct this mistake? Basically I am trying to make the very first result for the 1st element when i == 1 equal to 0 and then use this result as the start to compute the next results and then use the next result as start to compute the results after that.

I have tried to explain the problem as clearly as possible, please do ask if it is not clear.

  • 1
    Have a look at this question. Not sure if that's the problem. http://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument – OneCricketeer Feb 23 '17 at 14:08

1 Answers1

0

You have i==1, the formula computes. None of your logic knows about any first element of the list.

You can try to enumerate, which gives the position.

for i, x in enumerate(listA):
    if i == start:
        # at start of list 

    if x == 1:
        # compute formula 

By the way, don't store result as a default parameter to the function, make it a local variable

And start is never updated. It's always zero... Did you intend for this function to be recursive?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Yes, that's very true, I know that the program can't tell about any first elemnet in the list. Yes, so I basically want to keep updating start for each element in the list, I intend for the function to be recursive. – Tupilwe Sinyangwe Feb 23 '17 at 14:32
  • If you want a recursive function, then you need to call `formula` again with the updated parameters and get rid of the for loop. – OneCricketeer Feb 23 '17 at 15:54