我正在尝试实现一种在(美国)体育比赛中可能会看到的三方平局打破程序。我想首先按胜利次数排序,如果并列则使用头对头的打破者。
我希望这个运行时尽可能快,对内存要求并不在意。如果有更好的方法来表示我的数据,使其易于使用,那也是一个有用的答案。
我要排序的数据最多只有15个值,因此从这个角度来说运行时间并不糟糕,我只想做100k次。
伪代码大致如下:
Iterator = 0
maxVal = max value of wins
maxes = teams with wins == maxVal
If len(maxes) == 1
rank[values] = iterator
iterator += 1
sort(restOfData)
Else
# H2Hwins computes the amount of wins for teams currently tied incase of 2 or more teams tied
counts = sorted([(h2hwins(t, maxes), pointDifferential) for team in maxes])
for c in counts
rank[value] = iterator
iterator += 1
sort(restOfData)
return rank
如果我有以下输入,它们将产生以下输出:
# Columns are Team, Wins, H2H Tiebreaks, Point Differential
# Lakers win tie based on H2H with Clippers
testData = [['Lakers', 48, ['Clippers'], 6], ['Clippers', 48, ['Warriors'], 8], ['Warriors', 47, ['Lakers'], 10]]
magicSort(testData)
>>> ['Lakers', 'Clippers', 'Warriors']
# Warriors have 2 H2H tiebreakers so they are 1st. Lakers have 1 H2H tiebreaker so they are 2nd.
testData2 = [['Lakers', 48, ['Clippers'], 6], ['Clippers', 48, [''], 8], ['Warriors', 48, ['Lakers', 'Clippers'], 10]]
magicSort(testData2)
>>> ['Warriors', 'Lakers', 'Clippers']
# All 3 are tied so we default to point differential
testData3 = [['Lakers', 47, ['Clippers'], 6], ['Clippers', 47, ['Warriors'], 8], ['Warriors', 47, ['Lakers'], 10]]
magicSort(testData3)
>>> ['Warriors', 'Clippers', 'Lakers']
如果需要,我可以提供更多的测试案例,但我相信这已经涵盖了边缘情况。