python认为10小于9

时间:2012-02-08 02:38:56

标签: python

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

数据,主题是一个字典,它将类似6.00,7.01等的课程映射到类的值为1-10的元组,而工作负载1-20表示问题应该花费多少小时。好吧,它开始于一个文本文件,我把它变成了一个花花公子的字典。问题是从麻烦的介绍到编程问题8,文本在一个名为subjects.txt的文件中。我希望这能解决您对数据的担忧。

我遇到的问题是主题词典的类值最多为10,但是greedy_dictionary一直认为最大值为9.参数中的比较器是一个函数,如果第一个True则返回tuple[VALUE] {1}}大于第二个tuple[VALUE]

1 个答案:

答案 0 :(得分:10)

如果您要比较字符串,那就是......

>>> '10' < '9'
True

首先尝试将它们转换为数字。