2017-02-23 23 views
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])) 

當我運行這個我得到的結果#[2, 2, 2, 1, 0],這些都不是我預期的結果。如何使列表中元素的第一個結果等於0,然後使用此結果計算下一個結果?

''' 
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 

''' 

如何糾正這個錯誤?基本上,我試圖在i == 1等於0的情況下爲第一個元素創建第一個結果,然後使用此結果作爲start來計算下一個結果,然後使用下一個結果作爲start來計算其後的結果。

我試圖儘可能清楚地解釋問題,請確認問題是否不清楚。

+1

看看這個問題。不知道這是否是問題。 http://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument –

回答

0

您有i==1,公式計算。你的邏輯都不知道列表中的任何第一個元素。

你可以嘗試枚舉,它給出了位置。

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

    if x == 1: 
     # compute formula 

順便說一句,不存儲result作爲默認參數的功能,使它成爲一個局部變量

而且start從不更新。它總是零......你打算讓這個函數遞歸嗎?

+0

是的,這是非常真實的,我知道該程序不能告訴列表中的任何第一個elemnet。是的,所以我基本上想保持列表中每個元素的更新開始,我打算讓函數是遞歸的。 –

+0

如果你想要一個遞歸函數,那麼你需要再次用更新後的參數調用'formula'並且擺脫for循環。 –

相關問題