为了账号安全,请及时绑定邮箱和手机立即绑定

试图用 Python 为医生和医院创建一个匹配算法,卡在条件上

试图用 Python 为医生和医院创建一个匹配算法,卡在条件上

青春有我 2021-10-19 16:07:22
根据单元测试文档doCleanups()是负责调用所有清理方法的方法。一种方法是在 doCleanups 方法中检查测试用例的结果,如果测试用例失败则弹出所有清理方法并跳过其余测试用例下面是代码:import unittestclass SimpleTestCases(unittest.TestCase):    FAILURE = False    def setUp(self):        print "\nmessage from function: setUp"        if SimpleTestCases.FAILURE:            self.skipTest("Test is skipped due to first failure")            return super(SimpleTestCases, self).setUp()        self.createResource()        self.addCleanup(self.cleanResource)    def createResource(self):        msg = "\nmessage from function: createResource"    def cleanResource(self):        print "\nmessage from function: cleanResource"    def test_func1(self):        print "message from function: test_func1::start"        print "message from function: test_func1::end"    def test_func2(self):        print "message from function: test_func2::start"        self.assertTrue(False)        print "message from function: test_func2::end"    def test_func3(self):        print "message from function: test_func3::start"        print "message from function: test_func3:end"    def doCleanups(self):        print "message from function: doCleanups"        if SimpleTestCases.FAILURE:            return super(SimpleTestCases, self).doCleanups()        result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups)        ok_result = True        exc_list = getattr(result, 'failures')        if exc_list and exc_list[-1][0] is self:            ok_result = ok_result and not exc_list[-1][1]        if not ok_result:            SimpleTestCases.FAILURE = True            while self._cleanups:                (func, args, kwargs) = self._cleanups.pop()        return super(SimpleTestCases, self).doCleanups()if __name__ == "__main__":    result = unittest.main()
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

我将 First_round 更改为字典以使其更直接。我还会建议一些其他的结构更改,但之后会更多。一个解决方案是简单地输入一个 if 检查,看看是否指定了特定的医院,然后如果他有更好的分数就更换医生:


def hospital_ranking_of_doctor(hospital, doctor):

    return next(v for k, v in ranking_by_hospital[hospital].items() if v == doctor)


Matches = {}

Matches['Doctors'] = (doctors)

First_round = {}


# We loop through all the doctors one by one

for Doctor_ in ranking_by_doctors:


    # Then we find which hospital the doctor prefers

    favored_hospital = ranking_by_doctors[Doctor_].get(1.0)


    # We test if that hospital has already had a doctor assigned

    if favored_hospital not in First_round:

        # If it has not, then we just assign the current doctor

        First_round[favored_hospital] = Doctor_

    else:

        # If the hosptial was already assigned a doctor we compare 

        # that doctor with the new one and set the new one only if 

        # the hospital prefers him.

        previously_assigned_doctor = First_round[favored_hospital]

        if hospital_ranking_of_doctor(favored_hospital, previously_assigned_doctor) > hospital_ranking_of_doctor(favored_hospital, Doctor_):

            First_round[favored_hospital] = Doctor_


print(First_round)

现在对于结构更改,我认为您还应该将排名结构从字典范围内的数字到医院/医生更改为只是偏好的有序列表或翻转它所以关键是医院/医生和优先级/score 是值。这将使检查医生的分数更自然,例如:


ranking_by_hospital['Hospital_2']['Doctor_2'] > ranking_by_hospital['Hospital_2']['Doctor_3']

因此,您可以将排名函数更新为:


def hospital_ranking_of_doctor(hospital, doctor):

    return ranking_by_hospital[hospital][doctor]

或者简单地内联这些代码位。


查看完整回答
反对 回复 2021-10-19
  • 1 回答
  • 0 关注
  • 280 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信