如果条件未满足,则结束while循环

问题描述:

如果未满足条件,则试图结束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”

任何帮助都非常感谢。

感谢

你可能只是设置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

添加done = True后最后for循环应该是足够的,但你的代码有其他问题。

while循环是完全不必要的。如果你删除它,代码应该按需要工作。

你也有三个基于主题数量的循环,所以你的总迭代次数(while)将是立方体的数量。这意味着如果你有100个科目,最里面的部分必须执行100万次,如果没有找到匹配。我真的没有看到“k”循环的目的。你似乎没有做任何有价值的事情,所以它只是无用地重复内部循环。

你写的代码太多了!

您可能要拆你的代码是如何工作的:先在善良的顺序科目排序(也许是使用sorted函数,它接受一个cmp参数),然后通过排序列表将它们添加到一个结果变量停止时,你会超出maxWork。您不会遇到目前遇到的终止问题,因为一旦完成了排序列表,您将自然停止。

通常,将您正在进行的操作打破逻辑上分离的位(这里,先排序然后再汇总结果)让您更容易理解代码。