“`
-- 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