Problem A
Elections
Languages
en
is
Note in this problem the output must be exactly correct, even regarding the amount and positions of whitespace characters. The files will be located under the /src/ directory.
Write a program that shows information about elections in Iceland. The input to the program are text files with information about constituencies and political parties, as well as election results in each constituency. The output is nicely formatted tables (printed using format strings) based on the information in the text files.
The user is repeatedly shown a menu from which she can select the following actions in each iteration:
-
1. Show constituencies
-
2. Show parties
-
3. Show results.
-
9. Quit.
If any other option is selected by the user, the program does not take any action and presents the menu again.
Show constituencies
When this option is selected for the first time, the user is asked for a file name which contains, in each line, the name of a constituent and the number of eligible voters, separated with a semicolon. If the file can be opened, the information from the file is read into a dictionary and printed in a table. Thereafter, if this option is selected again, the table is presented without re-reading the data.
Below is an example of a constituent file, constit.txt:
Reykjavik sudur;45725 Reykjavik nordur;45638 Sudvestur;73729 Sudur;38424 Nordaustur;29887 Nordvestur;21548
Given the above input file, the program should show the following table:
Constituency Electorals ------------------------------ Reykjavik sudur 45725 Reykjavik nordur 45638 Sudvestur 73729 Sudur 38424 Nordaustur 29887 Nordvestur 21548 ------------------------------ Total: 254951
The width of the two columns are 20 and 10, for ‘Constituency’ and ‘Electorals’, respectively.
Show parties
When this option is selected for the first time, the user is asked for a file name which contains, in each line, the letter and the name of a political party, separated with a semicolon. If the file can be opened, the information from the file is read into a dictionary and printed in a table. Thereafter, if this option is selected again, the table is presented without re-reading the data.
Below is an example file, parties.txt:
D;Sjálfstaedisflokkurinn B;Framsoknarflokkurinn V;Vinstri graen S;Samfylkingin F;Flokkur folksins P;Piratar C;Vidreisn M;Midflokkurinn J;Sosialistaflokkur Islands
Given the above input file, the program should show the following table:
List Party -------------------------------- D Sjálfstaedisflokkurinn B Framsoknarflokkurinn V Vinstri graen S Samfylkingin F Flokkur folksins P Piratar C Vidreisn M Midflokkurinn J Sosialistaflokkur Islands
The width of the two columns are 6 and 26, for ‘List’ and ‘Party’, respectively.
Show results
When this option is selected for the first time, the user is asked for a file name which contains the election results. This file contains the name of all the constituencies, and for each constituency the name is followed by the party letters and the votes that the corresponding party obtained in the given constituency. You can assume that the number of constituencies in the file is equal to the number of constituencies that appeared in the file given in action 1 (‘Show constituencies’) and that the number of parties in the file is equal to the number of parties that appeared in the file in action 2 (‘Show parties’). If either the data on constituencies or parties has not been loaded, then this command will not succesfully load the results data. If the data is succesfully loaded, then the user is requested for a constituency for which to show results.
Below is an example file, results.txt:
Reykjavik sudur D;8809 V;5212 S;4720 B;4077 P;3875 F;3169 C;3067 J;1691 M;1456 Reykjavik nordur D;7353 V;5597 P;4508 S;4427 B;4329 C;2706 F;2694 J;1976 M;1234 Sudvestur D;17727 B;8520 V;7087 C;6684 P;4853 S;4748 F;4436 M;2612 J;1738 Sudur D;7296 B;7111 F;3837 S;2270 M;2207 V;2200 C;1845 P;1660 J;1094 Nordaustur B;6016 D;4346 V;3040 S;2465 M;2092 F;2026 C;1263 P;1256 J;954 Nordvestur B;4448 D;3897 V;1978 F;1510 M;1278 S;1195 P;1081 C;1063 J;728
If this file of results can be opened, it should be read into a dictionary. The key is a constituency name and the value for each key is a list of tuples. Each tuple is a pair containing a party letter and number of votes.
Once the file has been read, the user is asked to input a constituency name for which to show election results. For example, if the user inputs ‘Reykjavik nordur’, the following table is shown:
Reykjavik nordur List Party Votes Ratio -------------------------------------------------------- D Sjálfstaedisflokkurinn 7353 21.1 V Vinstri graen 5597 16.1 P Piratar 4508 12.9 S Samfylkingin 4427 12.7 B Framsoknarflokkurinn 4329 12.4 C Vidreisn 2706 7.8 F Flokkur folksins 2694 7.7 J Sosialistaflokkur Islands 1976 5.7 M Midflokkurinn 1234 3.5 -------------------------------------------------------- Total: 34824 100.0 Turnout: 76.3
The width of the four columns are 10, 26, 10, and 10 for ‘List’, ‘Party’,‘Votes’, and ‘Ratio’, respectively. The value for the ‘Ratio’ column should be formatted as having one digit after the decimal point. The value for ‘Turnout’ is computed as the ratio of total number of votes to the number of eligible voters in the given constituency.
Once the file of results has been read, it is not re-read, when option, ‘3. Show results’, is selected again by the user.
Read | Sample Interaction 1 | Write |
---|
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
1
File name:
constit.txt
Constituency Electorals ------------------------------ Reykjavik sudur 45725 Reykjavik nordur 45638 Sudvestur 73729 Sudur 38424 Nordaustur 29887 Nordvestur 21548 ------------------------------ Total: 254951 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
1
Constituency Electorals ------------------------------ Reykjavik sudur 45725 Reykjavik nordur 45638 Sudvestur 73729 Sudur 38424 Nordaustur 29887 Nordvestur 21548 ------------------------------ Total: 254951 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
2
File name:
bla.txt
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
2
File name:
parties.txt
List Party -------------------------------- D Sjálfstaedisflokkurinn B Framsoknarflokkurinn V Vinstri graen S Samfylkingin F Flokkur folksins P Piratar C Vidreisn M Midflokkurinn J Sosialistaflokkur Islands 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
2
List Party -------------------------------- D Sjálfstaedisflokkurinn B Framsoknarflokkurinn V Vinstri graen S Samfylkingin F Flokkur folksins P Piratar C Vidreisn M Midflokkurinn J Sosialistaflokkur Islands 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
3
File name for results:
results.txt
Constituency:
Reykjavik nordur
Reykjavik nordur List Party Votes Ratio -------------------------------------------------------- D Sjálfstaedisflokkurinn 7353 21.1 V Vinstri graen 5597 16.1 P Piratar 4508 12.9 S Samfylkingin 4427 12.7 B Framsoknarflokkurinn 4329 12.4 C Vidreisn 2706 7.8 F Flokkur folksins 2694 7.7 J Sosialistaflokkur Islands 1976 5.7 M Midflokkurinn 1234 3.5 -------------------------------------------------------- Total: 34824 100.0 Turnout: 76.3 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
3
Constituency:
Nordaustur
Nordaustur List Party Votes Ratio -------------------------------------------------------- B Framsoknarflokkurinn 6016 25.6 D Sjálfstaedisflokkurinn 4346 18.5 V Vinstri graen 3040 13.0 S Samfylkingin 2465 10.5 M Midflokkurinn 2092 8.9 F Flokkur folksins 2026 8.6 C Vidreisn 1263 5.4 P Piratar 1256 5.4 J Sosialistaflokkur Islands 954 4.1 -------------------------------------------------------- Total: 23458 100.0 Turnout: 78.5 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
3
Constituency:
bla
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
9
Read | Sample Interaction 2 | Write |
---|
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
2
File name:
parties.txt
List Party -------------------------------- D Sjalfstaedisflokkurinn B Framsoknarflokkurinn V Vinstri graen S Samfylkingin F Flokkur folksins P Piratar C Vidreisn M Midflokkurinn J Sosialistaflokkur Islands 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
1
File name:
blah
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
3
File name for results:
results.txt
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
1
File name:
constit.txt
Constituency Electorals ------------------------------ Reykjavik sudur 45725 Reykjavik nordur 45638 Sudvestur 73729 Sudur 38424 Nordaustur 29887 Nordvestur 21548 ------------------------------ Total: 254951 1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
3
File name for results:
results.txt
Constituency:
Sudurland
1. Show constituencies 2. Show parties 3. Show results 9. Quit Select an action:
9