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.