CONSTANTS:
NDP_INDEX = 0
GREEN_INDEX = 1
LIBERAL_INDEX = 2
CPC_INDEX = 3
各当事者のデータが 4 要素のリストに表示されるインデックスの分布。
PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]
各キーがパーティー名で、各値がそのパーティーのインデックスである辞書。
NAME_TO_INDEX = {'NDP': NDP_INDEX,
'GREEN': GREEN_INDEX,'LIBERAL': LIBERAL_INDEX,'CPC': CPC_INDEX}
各キーが当事者のインデックスであり、各値がその当事者の名前である辞書。
INDEX_TO_NAME = {NDP_INDEX: 'NDP',GREEN_INDEX: 'GREEN', LIBERAL_INDEX:
'LIBERAL',CPC_INDEX: 'CPC'}
def voting_range(range_votes):
'''
(list of list of int) -> tuple of (str, list of int)
#range_votes is a list of integers of range ballots for a single
#riding; the order of the inner list elements corresponds to the order
#of the parties in PARTY_INDICES.Based on range_votes, return a tuple
#where the first element is the name of the party winning the seat and
#the second is a list with the total range_votes for each party in the
#order specified in PARTY_INDICES.
>>> voting_range([[5, 4, 1, 4], [3, 2, 2, 5], [3, 3, 1, 4,]])
('CPC', [11, 9, 4, 13])
'''
NDP_count = 0
GREEN_count = 0
LIBERAL_count = 0
for sub_list in range_votes:
NDP_count += sub_list[0]
GREEN_count += sub_list[1]
LIBERAL_count += sub_list[2]
CPC_count += sub_list[3]
PARTY_INDICES[0] = NDP_count
PARTY_INDICES[1] = GREEN_count
PARTY_INDICES[2] = LIBERAL_count
PARTY_INDICES[3] = CPC_count
winner = max(NDP_count, GREEN_count, LIBERAL_count, CPC_count)
if winner == NDP_count:
return 'NDP', PARTY_INDICES
elif winner == GREEN_count:
return 'GREEN', PARTY_INDICES
elif winner == LIBERAL_count:
return 'LIBERAL', PARTY_INDICES
elif winner == CPC_count:
return 'CPC', PARTY_INDICES
新しい定数(ユニバーサル変数)を作成することはできず、ローカル変数のみを作成できるため、ヘルパー関数を使用してもこれを短縮する方法がわかりません