Difference between revisions of "CSC111 Homework 5"

From dftwiki3
Jump to: navigation, search
(Your assignment)
Line 252: Line 252:
  
 
       submit hw5 hw5a.py
 
       submit hw5 hw5a.py
 +
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Revision as of 22:08, 24 February 2010

This homework assignment is due on 3/4/10 at midnight. You are encouraged to work work on this assignment in pairs.

Problem #1

  • Create a new program in your account with the code below:



.

# hw5a.py
# your name 
#

statistics = [['Afghanistan', 28710000, -1, -1, 1], ['Albania', 3580000,
12000, -1, 10], ['Algeria', 32810000, 180000, -1, 2],
['Andorra', 69150, 24500, -1, 1], ['Angola', 10760000,
60000, -1, 1], ['Anguilla', 12738, 919, -1, 16],
['Antigua_and_Barbuda', 67897, 5000, -1, 16], ['Argentina',
38740000, 4650000, -1, 33], ['Armenia', 3320000, 30000, -1,
9], ['Aruba', 70844, 24000, -1, -1], ['Australia', 19730000,
13010000, 9020000, 571], ['Austria', 8180000, 4650000,
1300000, 37], ['Azerbaijan', 7830000, 25000, -1, 2],
['Bahrain', 667238, 140200, -1, 1], ['Bangladesh',
138440000, 150000, -1, 10], ['Barbados', 277264, 6000, -1,
19], ['Belarus', 10330000, 422000, -1, 23], ['Belgium',
10280000, 4870000, 1600000, 61], ['Belize', 266440, 18000,
-1, 2], ['Benin', 7040000, 25000, -1, 4], ['Bhutan',
2130000, 2500, -1, -1], ['Bolivia', 8580000, 78000, -1, 9],
['Bosnia_and_Herzegovian', 3980000, 45000, -1, 3],
['Botswana', 1570000, 33000, -1, 11], ['Brazil', 182030000,
22320000, 10860000, 50], ['Brunei', 358098, 35000, -1, 2],
['Bulgaria', 7530000, 1610000, -1, 200], ['Burkina_Faso',
13220000, 25000, -1, 1], ['Burma', 42510000, 10000, -1, 1],
['Burundi', 6090000, 6000, -1, 1], ['Cambodia', 13120000,
10000, -1, 2], ['Cameroon', 15740000, 45000, -1, 1],
['Canada', 32200000, 20450000, 8800000, 760], ['Cape_Verde',
412137, 12000, -1, 1], ['Cayman_Islands', 41934, -1, -1,
16], ['Central_African_Republic', 3680000, 2000, -1, 1],
['Chad', 9250000, 4000, -1, 1], ['Chile', 15660000, 5040000,
-1, 7], ['China', 1280000000, 99800000, -1, 3], ['Colombia',
41660000, 1870000, -1, 18], ['Comoros', 632948, 2500, -1,
1], ['Republic_of_the_Congo_Democratic', 56620000, 6000, -1,
1], ['Congo_Republic', 2950000, 500, -1, 1],
['Cook_Islands', 21008, -1, -1, 3], ['Costa_Rica', 3890000,
384000, -1, 3], ['Cote_d_Ivoire', 16960000, 70000, -1, 5],
['Croatia', 4420000, 480000, -1, 9], ['Cuba', 11260000,
120000, -1, 5], ['Cyprus', 771657, 150000, -1, 6],
['Czech_Republic', 10240000, 3530000, -1, 300], ['Denmark',
5380000, 3720000, -1, 13], ['Djibouti', 457130, 3300, -1,
1], ['Dominica', 69655, 2000, -1, 16],
['Dominican_Republic', 8710000, 186000, -1, 24],
['East_Timor', 977853, -1, -1, -1], ['Ecuador', 13710000,
328000, -1, 31], ['Egypt', 74710000, 2420000, -1, 50],
['El_Salvador', 6470000, 40000, -1, 4],
['Equatorial_Guinea', 510473, 900, -1, 1], ['Eritrea',
4360000, 10000, -1, 5], ['Estonia', 1400000, 620000, -1,
38], ['Ethiopia', 66550000, 20000, -1, 1], ['Faroe_Islands',
46345, 3000, -1, 2], ['Fiji', 856300, 15000, -1, 2],
['Finland', 5190000, 3270000, -1, 3], ['France', 60180000,
25470000, 15530000, 62], ['French_Guiana', 182917, 2000, -1,
2], ['French_Polynesia', 262125, 16000, -1, 2], ['Gabon',
1320000, 18000, -1, 1], ['Gambia', 1500000, 5000, -1, 2],
['Georgia', 4930000, 25000, -1, 6], ['Germany', 82390000,
41880000, 29520000, 200], ['Ghana', 20460000, 200000, -1,
12], ['Gilbraltar', 27776, -1, -1, 2], ['Greece', 10660000,
2710000, -1, 27], ['Greenland', 56385, 20000, -1, 1],
['Grenada', 89258, 5200, -1, 14], ['Guadeloupe', 440189,
4000, -1, 3], ['Guam', 163941, 5000, -1, 20], ['Guatemala',
13900000, 200000, -1, 5], ['Guernsey', 64818, -1, -1, -1],
['Guinea', 9030000, 15000, -1, 4], ['Guinea-Bissau',
1360000, 4000, -1, 2], ['Guyana', 702100, 95000, -1, 3],
['Haiti', 7520000, 30000, -1, 3], ['Honduras', 6660000,
40000, -1, 8], ['Hong_Kong', 7390000, 4580000, 2630000, 17],
['Hungary', 10040000, 2940000, -1, 16], ['Iceland', 200000,
198000, -1, 20], ['India', 1040000000, 36970000, -1, 43],
['Indonesia', 234890000, 12860000, -1, 24], ['Iran',
68270000, 420000, -1, 8], ['Iraq', 24680000, 12500, -1, 1],
['Ireland', 3920000, 1810000, -1, 22],
['Islas_Malvinas_(Falkland_Islands)', 2967, -1, -1, 2],
['Isle_of_Man', 74261, -1, -1, -1], ['Israel', 6110000,
3130000, 976000, 21], ['Italy', 57990000, 25530000,
15700000, 93], ['Jamaica', 2690000, 100000, -1, 21],
['Japan', 127210000, 78050000, 36580000, 73], ['Jersey',
90156, -1, -1, -1], ['Jordan', 5460000, 212000, -1, 5],
['Kazakhstan', 16760000, 100000, -1, 10], ['Kenya',
31630000, 500000, -1, 65], ['Kiribati', 98549, 1000, -1, 1],
['Kuwait', 2180000, 200000, -1, 3], ['Kyrgyzstan', 4890000,
51600, -1, -1], ['Laos', 5920000, 10000, -1, 1], ['Latvia',
2340000, 455000, -1, 41], ['Lebanon', 3720000, 300000, -1,
22], ['Lesotho', 1860000, 5000, -1, 1], ['Liberia', 3310000,
500, -1, 2], ['Libya', 5490000, 20000, -1, 1],
['Liechtenstein', 3314, -1, -1, -1], ['Lithuania', 3590000,
480000, -1, 32], ['Luxembourg', 454157, 100000, -1, 8],
['Macau', 469903, 101000, -1, 1], ['Macedonia', 2060000,
100000, -1, 6], ['Madagascar', 16970000, 35000, -1, 2],
['Malawi', 11650000, 35000, -1, 7], ['Malaysia', 23090000,
10040000, -1, 7], ['Maldives', 329684, 6000, -1, 1],
['Mali', 11620000, 30000, -1, 13], ['Malta', 400420, 59000,
-1, 6], ['Marshall_Islands', 56429, 900, -1, 1],
['Martinique', 425966, 5000, -1, 2], ['Mauritania', 2910000,
7500, -1, 5], ['Mauritius', 1210000, 158000, -1, 2],
['Mexico', 104900000, 13880000, -1, 51], ['Micronesia',
108143, 2000, -1, 1], ['Moldova', 4430000, 15000, -1, 2],
['Monaco', 32130, -1, -1, 2], ['Mongolia', 2710000, 40000,
-1, 5], ['Montserrat', 8995, -1, -1, 17], ['Morocco',
31680000, 400000, -1, 8], ['Mozambique', 17470000, 22500,
-1, 11], ['Namibia', 1920000, 45000, -1, 2], ['Nauru',
12570, -1, -1, 1], ['Nepal', 26460000, 60000, -1, 6],
['Netherlands_Antilles', 216226, 2000, -1, 6],
['New_Caledonia', 210798, 24000, -1, 1], ['New_Zealand',
3950000, 2340000, -1, 36], ['Nicaragua', 5120000, 20000, -1,
3], ['Niger', 10050000, 12000, -1, 1], ['Nigeria',
133880000, 100000, -1, 11], ['Niue', 2145, -1, -1, 1],
['Norfolk_Island', 1853, -1, -1, 2], ['North_Korea',
22460000, -1, -1, 1], ['Northern_Mariana_Islar', 80006, -1,
-1, 1], ['Norway', 4540000, 3030000, -1, 13], ['Oman',
2800000, 120000, -1, 1], ['Pakistan', 150690000, 1200000,
-1, 30], ['Palau', 19717, -1, -1, 1], ['Panama', 2960000,
45000, -1, 6], ['Papua_New_Guinea', 5290000, 135000, -1, 3],
['Paraguay', 6030000, 20000, -1, 4], ['Peru', 28400000,
4570000, -1, 10], ['Philippines', 84610000, 5960000, -1,
33], ['Pitcairn_Islands', 47, -1, -1, -1], ['Poland',
38620000, 10400000, -1, 19], ['Portugal', 10100000, 6090000,
-1, 16], ['Puerto_Rico', 3880000, 600000, -1, 76], ['Qatar',
817052, 75000, -1, 1], ['Reunion', 755171, 10000, -1, 1],
['Romania', 22270000, 4940000, -1, 38], ['Russia',
144520000, 21230000, -1, 35], ['Rwanda', 7810000, 20000, -1,
2], ['Samoa', 178173, 3000, -1, 2], ['San_Marino', 28119,
-1, -1, 2], ['Sao_Tome_and_Principe', 175883, 9000, -1, 2],
['Saudi_Arabia', 24290000, 2540000, -1, 42], ['Senegal',
10580000, 100000, -1, 1], ['Seychelles', 80469, 9000, -1,
1], ['Sierra_Leone', 5730000, 20000, -1, 1], ['Singapore',
4600000, 2750000, 956000, 9], ['Slovakia', 5430000, 1610000,
-1, 6], ['Slovenia', 1930000, 930000, -1, 11],
['Solomon_Islands', 509190, 8400, -1, 1], ['Somalia',
8020000, 200, -1, 3], ['South_Africa', 43600000, 4780000,
-1, 150], ['South_Korea', 42760000, 31670000, -1, 11],
['Spain', 40210000, 13440000, 8210000, 56], ['Sri_Lanka',
19740000, 121500, -1, 5], ['St._Kitts_and_Nevis', 38763,
2000, -1, 16], ['St._Lucia', 162157, 3000, -1, 15],
['St._Vincent_and_the_Grenadines', 116812, 3500, -1, 15],
['Sudan', 38110000, 56000, -1, 2], ['Suriname', 435449,
14500, -1, 2], ['Svalbard', 2811, -1, -1, -1], ['Swaziland',
1160000, 14000, -1, 6], ['Sweden', 8870000, 6120000,
4640000, 29], ['Switzerland', 7310000, 4600000, 3310000,
44], ['Syria', 17580000, 60000, -1, 1], ['Taiwan', 22600000,
9520000, 5000000, 8], ['Tajilistan', 6860000, 5000, -1, 4],
['Tanzania', 35920000, 300000, -1, 6], ['Thailand',
64260000, 7570000, -1, 15], ['The_Bahamas', 297477, 16900,
-1, 19], ['The_Netherlands', 16149999, 9790000, 7780000,
52], ['Togo', 5420000, 50000, -1, 3], ['Tokelau', 1418, -1,
-1, 1], ['Tonga', 108141, 1000, -1, 2],
['Trinidad_and_Tobago', 1100000, 120000, -1, 17],
['Tunisia', 9920000, 400000, -1, 1], ['Turkey', 68100000,
7270000, -1, 50], ['Turkmenistan', 4770000, 2000, -1, -1],
['Turks_and_Caicos', 19350, -1, -1, 14], ['Tuvalu', 11305,
-1, -1, 1], ['Uganda', 25630000, 60000, -1, 2], ['Ukraine',
48050000, 5278100, -1, 260], ['United_Arab_Emirates',
2480000, 900000, -1, 1], ['United_Kingdom', 60090000,
33110000, 22550000, 400], ['United_States', 290340000,
185550000, 139520000, 7000], ['Uruguay', 3410000, 600000,
-1, 14], ['Uzbekistan', 25980000, 100000, -1, 42],
['Vanuatu', 199414, 3000, -1, 1], ['Venezuela', 24650000,
2310000, -1, 16], ['Vietnam', 81620000, 400000, -1, 5],
['Virgin_Islands', 124778, 12000, -1, 50],
['Wallis_and_Futuna', 15734, -1, -1, 1], ['Western_Sahara',
261794, -1, -1, 1], ['Yemen', 19340000, 17000, -1, 1],
['Zambia', 10300000, 25000, -1, 5], ['Zimbabwe', 12570000,
100000, -1, 6]]

def prettyPrint( stats ):
    print "%-40s %10s %10s %10s %10s" % ( " ",       " ",          " ",     "Active", " " )
    print "%-40s %10s %10s %10s %10s" % ( "Country", "Population", "Users", "Users",  "ISPs" )
    for item in stats:
        ( country, population, internetUsers, activeUsers, ISPs ) = item
        print "%-40s %10d %10d %10d %10d" % ( country, population, internetUsers, activeUsers, ISPs )


def main():
    prettyPrint( statistics )


main()

.



  • Run the program, and observe its output:
                                                                   Active           
Country                                  Population      Users      Users       ISPs
Afghanistan                                28710000         -1         -1          1
Albania                                     3580000      12000         -1         10
Algeria                                    32810000     180000         -1          2
Andorra                                       69150      24500         -1          1
Angola                                     10760000      60000         -1          1
...
This data is taken from http://www.clickz.com/ and represents statistics about the world population of Internet users, and the number of Internet Service Providers dating from a few years back.
The original data used "NA" to indicate when data was Not Available, but I have replaced the NAs with -1 to simplify sorting.
  • Study the program and make sure you understand how the data is coded, and how the program works. There are no new Python constructs in this code. We have seen all the Python constructs used in this program in class or in a lab before!

Your assignment

Your assignment is to add new functions to this program so that we get a finer quantitative view of the world in terms of its usage of the Internet.

Question 1
Add a new function that will receive the statistics list as parameter and that will print the top 10 countries in the world by order of their population. This function should be called from the main() function and should use the same format as the one shown above, i.e. with the country name listed first, then the population, then the Active users, then the users, then the ISPs, but it should output a ranking on the left side, with 1 listed in the left margin of the first country, 2 listed in the left margin of the second country, until the 10th country. Note that the number 10 has 2 digits, while 1 to 9 have only one digits. However, the countries names and the various quantities should be listed one above the other without a jump to the right on the 10th line.
Make sure your program does not output the complete list of the 225 countries! This would use up too much paper when your program is graded. All that we are interested in this assignment is a collection of short lists of "the most" countries, for various quantities.
Make sure your program prints a title before the list of the top 10 populated countries. Something like "Top 10 Countries ranked by population" is fine.
Make sure you are efficient and that if you do not replicate identical code in different functions. The purpose of a function is to encapsulate code that can be used many times in different areas.
Question 2
Add a new function that will receive the statistics list as parameter and that will print the top 10 countries in terms of users and active users (i.e. add the number of users and the number of active users, and list the 10 countries with the largest such sums).
As with Question 1, make sure you print at title before the liste.
Question 3
Add a new function that will receive the statistics list as a parameter and that will print the top 10 countries in terms of the ratio of active Internet Users to population. The first country listed should be the one with the highest percentage of active users to the total population.
Use the same print format as explained in Question 2, including a title before the list.
Question 4
Add a function to your program that will receive statistics as a parameter and that will print out some global statistics.
  • The total world population as recorded in the list statistics
  • The total number of Internet users (don't worry about the -1 values. They contribute a very tiny error in the whole computation)
  • The total number of active Internet users.
  • The total number of ISPs.
  • The percentage of active users relative to Internet users, for the whole world.
  • The percentage of Internet users relative to the wold population.
The percentages should be printed with 2 decimal digits, and with the % sign. For example:
       Active Internet users world-wide:  12.30%
(Note, the % sign is a string operator. When it appears in a string, Python thinks it is going to be used as a substitution operator. To print a regular %-sign, just write two of them next to each other and Python will understand!)

Submission

  • Store your program in a file called hw5a.py and submit it as follows:
     submit hw5 hw5a.py