Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON This function will be taking a text file name as a parameter. The text

ID: 3677226 • Letter: I

Question

IN PYTHON

This function will be taking a text file name as a parameter. The text file is formatted to have the first name, last name and political ID of that person per line. You will use that information to keep count of the total number of each party affiliation presented in the file. Party affiliations are defined as follows: “1” is Independent, “2” is Republican, “3” is Democrat and “4” is Not Known This function will return a string with the parties and the counts as shown in the Sample run below.


You are required to use a dictionary to keep track of the counts. Failure to do so will result in
0 points for this problem. You can assume the input file has at least one line in it and at least one of each affiliation.
Output MUST BE as presented:

SAMPLE RUN:
polDict("politics.txt") will return, in this EXACT format,
('Democrats ', 22, 'Independents ', 6, 'Republicans ', 16, 'Not Known', 6)

File looks like this:

Hillary Clinton 3 Bill Clinton 3 Jeb Bush 2 Ted Cruz 2 Donald Trump 4 Bernie Sanders 1 Barak Obama 3 Chris Christy 2 Hillary Clinton 3 Bill Clinton 3 Jeb Bush 2 Ted Cruz 2 Donald Trump 4 Bernie Sanders 1 Barak Obama 3 Chris Christy 2 Hillary Clinton 3 Bill Clinton 3 Jeb Bush 2 Ted Cruz 2 Donald Trump 4 Bernie Sanders 1 Barak Obama 3 Chris Christy 2

Explanation / Answer

items = "Whats the simpliest way to add the list items to a dictionary " stats = {} for i in items: if i in stats: stats[i] += 1 else: stats[i] = 1 # bonus for i in sorted(stats, key=stats.get): print("%d×'%s'" % (stats[i], i))