2011-12-15 71 views
0

如果未滿足條件,則試圖結束while循環。如果條件未滿足,則結束while循環

這段代碼的目的是爲了獲得最大的多學科的但不超過最高工時的學生願意把英寸

我創建了一個名爲「科目」字典它映射(有價值,有價值,有工作價值)這個主題是多麼寶貴,並且需要多少工作才能投入到這個主題中。我將這些科目的價值加起來,而不會超過學生願意投入的最長時間。然後將最有意義的科目放入不同的字典中。

下面是代碼:如果maxWork滿足

def greedyAdvisor(subjects, maxWork, comparator): 
""" 
Returns a dictionary mapping subject name to (value, work) which includes 
subjects selected by the algorithm, such that the total work of subjects in 
the dictionary is not greater than maxWork. The subjects are chosen using 
a greedy algorithm. The subjects dictionary should not be mutated. 

subjects: dictionary mapping subject name to (value, work) 
maxWork: int >= 0 
comparator: function taking two tuples and returning a bool 
returns: dictionary mapping subject name to (value, work) 
""" 

bestVal = {} 
tempVal = 0 
high = 0 
count = 0 
tempDict = {} 
tempWork = 0 
currentBest = None 
done = False 

while done == False: 

    for k in range(len(subjects)+1): 
     for i in subjects: 
      for j in subjects: 
       if i not in bestVal: 
        sub1 = subjects[i][0] 
        sub2 = subjects[j][0] 
        work1 = subjects[i][1] 
        work2 = subjects[j][1] 
        if tempWork >= maxWork: 
          print('tempWork is', tempWork)       
          print('bestVal is', bestVal) 
          print('high is', high) 
          print('tempVal is', tempVal) 
          print() 
          return 
        print('sub1 is', sub1) 
        print('sub2 is', sub2) 
        print('work1 is', work1) 
        print('work2 is', work2) 
        maxVal = comparator(sub1, sub2) 
        print('count is', count) 
        count += 1 
        if maxVal == True: 
         print('sub1+tempVal is', sub1+tempVal) 
         print('tempVal is', tempVal) 
         print() 
         if work1 + tempWork > tempWork and tempWork + work1 <= maxWork: 
           high += tempVal+sub1 
           tempWork += work1 
           tempVal = sub1 +tempVal 
           print('sub1', sub1) 
           print('work1 is', work1) 
           print('tempWork is', tempWork) 
           print('tempVal is', tempVal) 
           print('tempWork is', tempWork) 
           bestVal[i] = subjects[i] 
           print('bestVal is', bestVal) 
           print() 
         else: 
          break 

循環結束,這是我在代碼了。問題是,如果maxWork在經過所有主題後沒有被滿足,它將會一直循環。在詞典中的所有項目都循環並且條件未滿足之後,我需要結束循環。我猜我在這裏需要一個「if」聲明,但我不知道如何編寫它。 「如果所有主題都經過測試並且maxWork> tempWork:done = True」

任何幫助都非常感謝。

感謝

回答

0

你可能只是設置done = True所有的循環之後。

done = False 

while not done: 
    # your bunch of loops code here 
    done = True 

更多。你真的需要while循環嗎?我無法完全理解您的代碼,因爲它太多了,但我看到while中只有for循環,看起來像其他事情永遠不會改變。

+0

哇,這正是我所期待的。謝謝。 – CastorTroy 2011-12-16 04:15:27

+0

我知道這是一團糟,我只是停留在這個while循環的事情上,需要知道如何去做,即使我不需要它。我要回去清理它。我很感激幫助。 – CastorTroy 2011-12-16 04:34:54

+0

抱歉,但這是一個不好的補丁。 while循環絕對是不必要的 – joaquin 2011-12-17 10:29:11

3

添加done = True後最後for循環應該是足夠的,但你的代碼有其他問題。

while循環是完全不必要的。如果你刪除它,代碼應該按需要工作。

你也有三個基於主題數量的循環,所以你的總迭代次數(while)將是立方體的數量。這意味着如果你有100個科目,最裏面的部分必須執行100萬次,如果沒有找到匹配。我真的沒有看到「k」循環的目的。你似乎沒有做任何有價值的事情,所以它只是無用地重複內部循環。

1

你寫的代碼太多了!

您可能要拆你的代碼是如何工作的:先在善良的順序科目排序(也許是使用sorted函數,它接受一個cmp參數),然後通過排序列表將它們添加到一個結果變量停止時,你會超出maxWork。您不會遇到目前遇到的終止問題,因爲一旦完成了排序列表,您將自然停止。

通常,將您正在進行的操作打破邏輯上分離的位(這裏,先排序然後再彙總結果)讓您更容易理解代碼。