python 字典

本文介绍了一个用于管理体育竞赛成绩的系统,包括如何录入竞赛成绩、查询国家奖牌排名及进行奖牌数据分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

“`

-- coding:UTF-8 --

import re
from operator import itemgetter
import constants

class OlympicsMedalSystem:
def init(self):
”’
系统初始化
:return: 返回操作成功或失败的代码
”’

    self.countByCountry = {"China": [0]*3, "America": [0]*3, "Japan":[0]*3,\
                           "Korea":[0]*3, "Russia":[0]*3, "England":[0]*3}        
    self.player_country = {}

    self.currentSubjectMedal = [0] * 11

    self.subject_player = {}
    for i in range(11):
        self.subject_player[i] = set()

    self.currentTime = 0

    return constants.S000


def input_medal_record(self, time, country, player, subject, medal):
    '''
    录入竞赛成绩
    :param time: 系统时间(int)
    :param country: 国家(string)
    :param player: 运动员名字(string)
    :param subject: 运动项目(int)      1-11 
    :param medal: 奖牌级别(int)        1,2,3
    :return: 返回操作成功或失败的代码
    '''
    # ERROR PARA
    if time > 30 or time < 0 or country not in self.countByCountry.keys():
        return constants.E001
    if re.search(r"[^a-zA-Z]",player) or len(player) > 10:
        return constants.E001
    if subject > 11 or subject < 1:
        return constants.E001
    if medal > 3 or medal < 1:
        return constants.E001
    if time < self.currentTime:
        return constants.E002
    if player in self.player_country.keys() and country != self.player_country[player]:
        return constants.E003
    if player in self.subject_player[subject-1]:
        return constants.E004
    if self.currentSubjectMedal[subject-1] != medal - 1:
        return constants.E005

    self.currentTime = time
    self.player_country[player] = country
    self.currentSubjectMedal[subject-1] = medal
    self.subject_player[subject-1].add(player)

    self.countByCountry[country][medal - 1] += 1

    return constants.S001


def query_country_medal_rankings(self):
    '''
    查询国家奖牌排名
    :param time: 系统时间(int)
    :return: ret: 返回操作成功或失败的代码
    :return: info: 返回排名信息,格式为列表,列表的元素为字典,结构如下所示:
    [
    {
        'country': 'China',     #国家
        'gold': 58              #金牌
        'silver': 58              #银牌
        'bronze': 12,            #铜牌
        'ranking': 1,           #排名
    }
    ]

    '''
    outListOfDict = []
    for key, value in self.countByCountry.items():
        tempDict = dict()
        tempDict["country"] = key
        tempDict["gold"] = value[0]
        tempDict["silver"] = value[1]
        tempDict["bronze"] = value[2]
        tempDict["ranking"] = 0
        outListOfDict.append(tempDict)
    outListOfDict = sorted(outListOfDict, key = itemgetter("gold","silver","bronze","country"), reverse=True)
    for i in range(len(outListOfDict)):
        outListOfDict[i]["ranking"] = i+1


    ret = constants.S003

    return ret, outListOfDict
    ```

未AC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值