Consider a text file called elections.txt that contains the following lines: The file contains presidential-election res
Posted: Sun Jul 10, 2022 11:27 am
Consider a text file called elections.txt thatcontains the following lines:
The file contains presidential-election results from 1980 to thepresent. Each line contains five values separated by commas:
Given the file above, answer the following two questions:
(1 point) What is the output of the following code fragment?
Initially, you should determine the output by tracing throughthe code by hand. Once you have done so, you can check your answerby downloading the following files and running the program:
Make sure to put these files inyour ps4 folder. If your browser doesn’tallow you to specify where the file should be saved,try right-clicking on the link above andchoosing Save as... or Save linkas..., which should produce a dialog box that allows you tochoose the correct folder for the file.
(4 points) Modify the code fragment provided above so that itfinds all years in which the winner won a majority of the popularvote (i.e., more than 50 percent). For each such year, the revisedcode fragment should print the year, the winner’s last name, andhis percentage of the vote in parentheses, as shown below:
(Note: In the remaining years — 1992, 1996, 2000, and 2016 — athird-party candidate won enough of the vote to prevent anyone fromwinning a majority.) Your revised code should work for any filethat includes results that are formatted in the same way as theresults in elections.txt.
The file contains presidential-election results from 1980 to thepresent. Each line contains five values separated by commas:
Given the file above, answer the following two questions:
(1 point) What is the output of the following code fragment?
Initially, you should determine the output by tracing throughthe code by hand. Once you have done so, you can check your answerby downloading the following files and running the program:
Make sure to put these files inyour ps4 folder. If your browser doesn’tallow you to specify where the file should be saved,try right-clicking on the link above andchoosing Save as... or Save linkas..., which should produce a dialog box that allows you tochoose the correct folder for the file.
(4 points) Modify the code fragment provided above so that itfinds all years in which the winner won a majority of the popularvote (i.e., more than 50 percent). For each such year, the revisedcode fragment should print the year, the winner’s last name, andhis percentage of the vote in parentheses, as shown below:
(Note: In the remaining years — 1992, 1996, 2000, and 2016 — athird-party candidate won enough of the vote to prevent anyone fromwinning a majority.) Your revised code should work for any filethat includes results that are formatted in the same way as theresults in elections.txt.