python 冒泡排序

 for i in range(len(self.teamList), 0, -1):
            for j in range(0, i - 1):
                if self.teamList[j].getScore() < self.teamList[j + 1].getScore():
                    temp = self.teamList[j];'''移動最小到最後一位'''
                    self.teamList[j] = self.teamList[j + 1];
                    self.teamList[j + 1] = temp;

 

 

異常捕獲

class teamInfoNotFindError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

 

try:
    raise teamInfoNotFindError("TeamInfo Not Find")
except teamInfoNotFindError as e:
    print(e.value)

 

 

類定義

class teamInfo:
    teamId = ""
    winNum = 0
    failNum = 0
    midNum = 0
    __sorce = -1
    ranking = -1
    __log=""
    def __init__(self, teamId):
        self.teamId = teamId
    def addWin(self,teamid):
        self.winNum = self.winNum + 1
        self.__log=self.__log+(teamid+":3--")
    def addFail(self,teamid):
        self.failNum = self.failNum + 1
        self.__log=self.__log+(teamid+":0--")
    def addMid(self,teamid):
        self.midNum = self.midNum + 1
        self.__log=self.__log+(teamid+":1--")
    def addScore(self, score,teamid):
        if self.isInput(teamid)==-1:
            if score == 3:
                self.addWin(teamid)
            elif score == 1:
                self.addMid(teamid)
            elif score == 0:
                self.addFail(teamid)
            else:
                print("輸入錯誤!!!")
    def getScore(self):
        __sorce = self.winNum * 3 + self.midNum * 1
        return __sorce
    def addSetRandking(self, ranking):
        self.ranking = ranking
    def isInput(self,teamid):
        currentlog_a=self.__log.split("--")
        for i in range(0,len(currentlog_a)):
            '''存在返回0'''
            if teamid==currentlog_a[i].split(":")[0]:
                print(self.teamId+"與"+currentlog_a[i].split(":")[0]+"比賽結果已錄入") 
                return 0
        return -1

 

class allTeamInfo:
    teamList = []
    def addTeam(self, teamInfo):
        if len(self.teamList)<=1000:
            self.teamList.append(teamInfo)
        else:
            print("比賽隊伍總數<=1000")
    '''排序'''
    def reSetRandking(self):
        for i in range(len(self.teamList), 0, -1):
            for j in range(0, i - 1):
                if self.teamList[j].getScore() < self.teamList[j + 1].getScore():
                    temp = self.teamList[j];'''移動最小到最後一位'''
                    self.teamList[j] = self.teamList[j + 1];
                    self.teamList[j + 1] = temp;
            '''此時設置ranking'''
            self.teamList[i - 1].ranking = i
    def selectTeamInfo(self, teamid):
        for i in range(0, len(self.teamList)):
            if teamid == self.teamList[i].teamId:
                return i
        return - 1

 

#類調用,

def AddGameResult(Team1Id, Team1Score, Team2Id, Team2Score):
    '''
        輸入參數:       Team1Id         隊伍1ID
                  Team1Score      隊伍1得分
                  Team2Id         隊伍2ID
                  Team2Score      隊伍2得分
        輸出參數: 無
        返回值:  無
        方法說明:
                輸入隊伍1對隊伍2的比賽結果
    '''
    '''ID相同'''
   
    iT1=int(Team1Id)
    iT2=int(Team2Id)
    if iT1>=0 and iT1<=65535 and iT2>=0 and iT2<=65535:
        if Team1Id!=Team2Id:
            t1 = g_allteaminfo.selectTeamInfo(Team1Id)
            t2 = g_allteaminfo.selectTeamInfo(Team2Id)
            if t1 == -1:
                tem = teamInfo(Team1Id)
                tem.addScore(Team1Score,Team2Id)
                g_allteaminfo.addTeam(tem)
            else:
                g_allteaminfo.teamList[t1].addScore(Team1Score,Team2Id)
           
            if t2 == -1:
                tem = teamInfo(Team2Id)
                tem.addScore(Team2Score,Team1Id)
                g_allteaminfo.addTeam(tem)
            else:
                g_allteaminfo.teamList[t2].addScore(Team2Score,Team1Id)
        else:
            print("不能和自己比賽")
    else:
        print("team1或team2的ID超過限制")

 

def GetTeamResult(TeamId, TeamResultList):
    '''
        輸入參數: TeamId            隊伍ID
        輸出參數: TeamResultList    返回隊伍的最新賽況,返回一個list,內容如下:
                  [隊伍ID, 勝的場數, 平的場數, 負的場數, 積分, 名次]
                  提醒:調用者不負責清空這個list,必須在此函數內先處理清空
                 
        返回值:  成功返回0,失敗返回-1。若沒有輸入過該隊伍的比賽,返回失敗
        方法說明:
                獲取隊伍的最新賽況
    '''
    g_allteaminfo.reSetRandking()
    teamindex = g_allteaminfo.selectTeamInfo(TeamId)
    if teamindex == -1:
        raise teamInfoNotFindError("TeamInfo Not Find")
    else:
        try:
            teaminfo = g_allteaminfo.teamList[teamindex]
            reslut = [teaminfo.teamId,teaminfo.winNum,teaminfo.midNum,teaminfo.failNum,teaminfo.getScore(),teaminfo.ranking]
            TeamResultList[0]=reslut
            return 0
        except Exception:
            return -1
def Clear():
    '''
        輸入參數: 無
        輸出參數: 無
        返回值:  無
        方法說明:
                清空所有隊伍以及其賽況信息
    '''
    pass
g_allteaminfo = allTeamInfo()
TeamResultList = ["","","","","",""]
AddGameResult("0001", 1, "0002", 1)
AddGameResult("0001", 0, "0003", 3)
AddGameResult("0001", 3, "0004", 0)
AddGameResult("0001", 1, "0005", 1)
AddGameResult("0001", 0, "0006", 3)
AddGameResult("0002", 0, "0003", 3)
AddGameResult("0002", 3, "0004", 0)
AddGameResult("0002", 1, "0005", 1)
AddGameResult("0002", 0, "0006", 3)
AddGameResult("0003", 3, "0004", 0)
AddGameResult("0003", 1, "0005", 1)
AddGameResult("0003", 0, "0006", 3)
AddGameResult("0004", 1, "0005", 1)
AddGameResult("0004", 0, "0006", 3)
AddGameResult("0005", 0, "0006", 3)
AddGameResult("0006", 0, "0005", 3)

try:
    GetTeamResult("0001",TeamResultList)
    print(TeamResultList[0])
    GetTeamResult("0002",TeamResultList)
    print(TeamResultList[0])
    GetTeamResult("0003",TeamResultList)
    print(TeamResultList[0])
    GetTeamResult("0004",TeamResultList)
    print(TeamResultList[0])
    GetTeamResult("0005",TeamResultList)
    print(TeamResultList[0])
    GetTeamResult("0006",TeamResultList)
    print(TeamResultList[0])
except teamInfoNotFindError as e:
    print(e.value)
   

 

發佈了38 篇原創文章 · 獲贊 16 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章