EHMJ
EHMJ

Reputation: 251

How to shorten this code without using bunch of if statements

CONSTANTS:

NDP_INDEX = 0
GREEN_INDEX = 1
LIBERAL_INDEX = 2
CPC_INDEX = 3

A dist of the indices where each party's data appears in a 4-element list.

PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]

A dict where each key is a party name and each value is that party's index.

NAME_TO_INDEX = {'NDP': NDP_INDEX,
'GREEN': GREEN_INDEX,'LIBERAL': LIBERAL_INDEX,'CPC': CPC_INDEX}

A dict where each key is a party's index and each value is that party's name.

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

I am not sure how to shorten this even with using helper function because we are not allow to create new constants (universal variables) but just local variables

Upvotes: 0

Views: 233

Answers (2)

engineerC
engineerC

Reputation: 2868

You don't need all those dicts and range variables and define/enum looking things. This is a little less C and a little more python:

votes=[[5, 4, 1, 4], [3, 2, 2, 5], [3, 3, 1, 4,]]
parties=['NDP','GREEN','LIBERAL','CPC']

def voting_range(v):
  totals = [sum(x) for x in zip(*v)] # transpose row/col of list of lists
  pname = parties[totals.index(max(totals))]
  return (pname,totals)

>>> voting_range(votes)
('CPC',[11, 9, 4, 13])

There are a few magic python things going on here. *v unpacks the list of lists, and feeds them as separate arguments to zip, which returns an iterable that gives the first element of each argument in a list, then the second element of each argument in a list, and so on. This has the effect of transposing the matrix (swapping rows and columns). In this form, simply summing each list will be the vote totals. The for x in syntax wrapped in brackets is a list comprehension, another awesome python feature which efficiently creates lists from iterables. Finally, index is a list method that will return the index of the first element that has the value specified, which in this case is the max. Since the elements of v should have the same length as the number of parties, this will also serve as the index into the parties list.

Upvotes: 1

try this:

for sub_list in range_votes:
   for i in range(4):
       PARTY_INDICES[i] += sub_list[i]
return ( {PARTY_INDICES[0] : 'NDP', PARTY_INDICES[1] : 'GREEN', ...}[max(PARTY_INDICES)], 
         PARTY_INDICES )

That's as short as I'm willing to make it ;-)

Upvotes: 2

Related Questions