States ma tx ca fl selfcandidates o m n selfstatevotes

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: f): self.states = [’MA’, ’TX’, ’CA’, ’FL’] self.candidates = [’O’, ’M’, ’N’] self.stateVotes = {} def addVotes(self, cand, votes, state): if not state in self.stateVotes: self.stateVotes[state] = {cand : votes} else: if not cand in self.stateVotes[state]: self.stateVotes[state][cand] = votes else: self.stateVotes[state][cand] += votes 7.1 Write a method for the Votes class, called popularVotesTotal, that computes the total number of votes for a candidate, across all states. • Use a list comprehension, not a for loop. • Use the Python sum procedure, which takes a list of numbers as input and returns the sum. def popularVotesTotal(self, cand): return sum([self.stateVotes[state][cand] for state in self.states]) Midterm 2 Solutions — Spring 10 14 7.2 Write a method for the Votes class, called stateWinner, that computes the winner for a state, that is, the candidate with the most votes in that state. Use the procedure argmaxDict(d), which is called with a dictionary d and returns the key in d whose associated value is highest. def stateWinner(self, state): return argmaxDict(self.stateVotes[state]) 7.3 Write a method for the Votes class, called statesWon, that takes a candidate as an argument and returns a list of the states that candidate won. Use the stateWinner method you just imple­ mented. def statesWon(self, cand): return [state for state in self.states if self.stateWinner(state) == cand] Midterm 2 Solutions — Spring 10 15 7.4 Write a method for the Votes class, called winnerOfMostStates, that returns the candidate that won the most states. Use the statesWon method you just implemented. For full credit, use util.argmax. If l is a list of items and f is a procedure that maps an item into a numeric score, then util.argmax(l, f) returns the element of l that has the highest score. def winnerOfMostStates(self): return util.argmax(self.candidates, lambda cand: len(self.statesWon(cand))) Midterm 2 Solutions — Spring 10 16 8 Balanced Machine (10 points) Write a state machine that counts the depth of nesting of parens in a string. It takes a character from the string as input and outputs an integer, indicating how many unmatched open parens there are (on or) before this character or None if the parens are unbalanced. Note that once the parens become unbalanced, all future outputs will be None. Here are some examples: >>> >>> [1, >>> [1, >>> [1, bp = BalancedParens() bp.transduce(’(ab (c (d (e)) f))’) 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 3, 2, 2, 2, 1, 0] bp.transduce(’(()) ((())) ()()()’) 2, 1, 0, 0, 0, 1, 2, 3, 2, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0] bp.transduce(’(())) ()()()’) 2, 1, 0, None, None, None, None, None, None, None, None, None] Your state machine should be a subclass of sm.SM. class BalancedParens(sm.SM): startState = 0 def getNextValues(self, state, inp): if state == None: return (None, None) elif inp == ’(’: return (state+1, state+1) elif inp == ’)’: if state == 0: return (None, None) else: return (state-1, state-1) else: return (state, state)...
View Full Document

This document was uploaded on 03/17/2014 for the course ELECTRICAL 6.01SC at MIT.

Ask a homework question - tutors are online