def greedyAdvisor(subjects, maxWork, comparator):
'''subjects is a dictionary with keys classes and values of tuples of class value and class work. maxWork is the maximum work a student wants to put in. comparator is a function that takes two tuples of the aforementioned class value/class work variety and returns which one has a higher value. The function returns a dictionary with the most valuable classes to take within the given parameter of the willingness to work. I am supposed to use a greedy algorithem'''
greedy_dict = {}
highest_subjects = []
total_work = 0
while total_work < maxWork:
highest_value = 0, 0
highest_class = 0.0
for i in subjects:
if comparator (subjects[i], highest_value) and i not in highest_subjects and total_work + int(subjects[i][WORK]) <= maxWork:
highest_value = subjects[i]
highest_class = i
print highest_class, highest_value
highest_subjects.append(highest_class)
total_work += int(highest_value[WORK])
greedy_dict[highest_class] = highest_value
print greedy_dict
return greedy_dict
データであるのpythonだと思うん方法については、クラスの値の値が1-10でタプルに6.00、7.01、などのようなコースをマッピングした辞書とワークロード1-20であります何時間も問題が起こるはずです。それはテキストファイルで始まり、ちょうどダンディーな辞書に変えました。問題は、ocwのイントロイントロからプログラミング問題8へのテキストであり、テキストはsubjects.txtというファイルにあります。これがあなたのデータに対する懸念に応えたことを祈っています。は被験者、10未満9つの
問題は辞書のクラス値が最大10ですが、greedy_dictionaryは最大値が9であると考え続けます。パラメータのコンパレータは、最初のtuple[VALUE]
がより大きい場合はTrue
を返す関数です第2のtuple[VALUE]
。
必要に応じて人々が実行できるようにデータを提供してください。 –