m0_61699298 2024-05-03 14:19 采纳率: 28.6%
浏览 1
已结题

4个for循环优化,算法优化

找出哪两个评委对哪位选手的哪条申请的哪个项目认定不一致。
评委认定结果只有 同意和不同意
每位选手有多条申请,每条申请里对应多个项目,评委要对每个项目进行认定。
如:3位评委,4位选手,每个选手有2条申请,每条申请里面的项目可以不一致,
如A选手的第一条申请有3个项目,第二条申请有4个项目。
每位评委都要认定A选手的每个项目,就有3*7=21条认定结果,问这些结果中哪两个评委对哪位选手的哪条申请的哪个项目认定不一致。
其他选手也有自己的申请项目。

算法优化,经过分析,最笨的是要用4个for循环,请问还有其他办法可以解决这个问题吗?

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-05-03 14:19
    关注

    引自免费微信小程序:皆我百晓生

    为了优化这个查找不一致认定的结果,我们可以采用一种更高效的方法,利用集合(Set)数据结构来进行处理。首先,我们需要将所有评委对每个选手的申请项目的认定结果组织成一个数据结构,然后通过比较不同评委之间的结果差异找到不一致之处。

    以下是Python代码实现:

    def find_inconsistent_judges(judges, applicants, applications):
        # 假设 judges 是一个字典,键是评委名字,值是一个嵌套字典,键是申请人名字,值是一个嵌套列表
        # 列表中的元素是应用编号,对应的值是评委对该申请中每个项目的认定结果
        # 例如:
        # {
        #   "Judge1": {
        #       "ApplicantA": [{"Project1": "Agree", "Project2": "Disagree"}, {"Project3": "Agree", "Project4": "Agree"}],
        #       "ApplicantB": [{"Project5": "Agree", "Project6": "Agree"}, {"Project7": "Disagree", "Project8": "Agree"}]
        #   },
        #   "Judge2": {...},
        #   "Judge3": {...}
        # }
    
        inconsistent_results = []
    
        for applicant in applicants:
            for application in applications[applicant]:
                project_results = {}
                for judge, judgements in judges.items():
                    if applicant in judgements and application in judgements[applicant]:
                        for project, result in judgements[applicant][application].items():
                            project_results.setdefault(project, set()).add(result)
    
                # 检查是否有项目存在至少两位评委的认定结果不一致
                for project, results in project_results.items():
                    if len(results) > 1:
                        inconsistent_results.append((applicant, application, project, results))
    
        return inconsistent_results
    
    # 示例数据
    judges = {
        "Judge1": {
            "ApplicantA": [{"Project1": "Agree", "Project2": "Disagree"}, {"Project3": "Agree", "Project4": "Agree"}],
            "ApplicantB": [{"Project5": "Agree", "Project6": "Agree"}, {"Project7": "Disagree", "Project8": "Agree"}]
        },
        "Judge2": {
            "ApplicantA": [{"Project1": "Agree", "Project2": "Agree"}, {"Project3": "Agree", "Project4": "Disagree"}],
            "ApplicantB": [{"Project5": "Agree", "Project6": "Disagree"}, {"Project7": "Agree", "Project8": "Agree"}]
        },
        "Judge3": {
            "ApplicantA": [{"Project1": "Agree", "Project2": "Agree"}, {"Project3": "Agree", "Project4": "Agree"}],
            "ApplicantB": [{"Project5": "Disagree", "Project6": "Agree"}, {"Project7": "Agree", "Project8": "Agree"}]
        }
    }
    
    applicants = ["ApplicantA", "ApplicantB"]
    applications = {
        "ApplicantA": ["Application1", "Application2"],
        "ApplicantB": ["Application1", "Application2"]
    }
    
    inconsistencies = find_inconsistent_judges(judges, applicants, applications)
    print(inconsistencies)
    

    这段代码首先遍历所有的申请人和他们的申请,然后针对每个申请中的每一个项目,收集所有评委的认定结果并存储在project_results字典中。接着检查该项目是否存在至少两个不同的结果,如果有,则记录为不一致的结果并将其添加到inconsistent_results列表中。最后返回这个列表,其中包含所有不一致的认定情况。

    这种方法避免了不必要的嵌套循环,大大减少了计算复杂度。对于大规模的数据集,性能会有显著提升。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月26日
  • 创建了问题 5月3日