CSC111 Homework 5 Solution
--D. Thiebaut 16:03, 6 March 2010 (UTC)
# hw5a.py
# Kristina Fedorenko
# 111c-aq
#
# This program takes information about internet users population
# of different countries in a form of a list and calculates
# and outputs the following statistics:
#
# - three lists of top 10 countries ranked based on total population,
# number of users and the ratio of active Internet Users to population
#
# - global statistics (total world population, total number of
# Internet users, total number of active Internet users, total number of ISPs,
# percentage of active users relative to Internet users, percentage of
# Internet users relative to the world population.
#
# Data is taken from www.clickz.com
#
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]]
# adds population in the beginning of each country's data set
def AddPopul(stats):
populationStats = []
for country in stats:
name, population, users, actUsers, ISPs = country
populationStats.append([population, name, population, users, \
actUsers, ISPs])
return populationStats
# caclulates the sum (number of users plus number of active users)
# and adds it in the beginning of each country's data set
def AddUsers(stats):
userstats = []
for country in stats:
name, population, users, actUsers, ISPs = country
userstats.append([users + actUsers, name, population, users,\
actUsers,ISPs])
return userstats
# inserts ratio of active users to population in the beginning
# of each country's data set
def AddRatio(stats):
ratiostats = []
for country in stats:
name, population, users, actUsers, ISPs = country
ratiostats.append([float(actUsers)/population, name, \
population, users, actUsers,ISPs])
return ratiostats
# removes the first item in a data set of each country
def take_out(stats):
Stats = []
for country in stats:
odd, name, population, users, actUsers, ISPs = country
Stats.append([name, population, users, actUsers, ISPs])
return Stats
# sorts stats in a set (must have the number first) from largest to lowest
def sort_biggest(stats):
stats.sort()
stats.reverse()
return stats
# creates a list from the first 10 items from a given list
def selectfirst10(stats):
first10 = stats[0:10]
return first10
# prints a line of '=-'
def PrettyLine():
print '=-'*40
# prints formatted data (in columns); set of data must have original order
def prettyPrint( stats ):
print "%2s %-30s %10s %10s %10s %10s" % \
(" ", " ", " ", " ", "Active", " " )
print "%2s %-30s %10s %10s %10s %10s" % \
(" ", "Country", "Population", "Users", "Users", "ISPs" )
print
number = 1
for item in stats:
( country, population, internetUsers, activeUsers, ISPs ) = item
print "%2d %-30s %10d %10d %10d %10d" % \
(number, country, population, internetUsers, activeUsers, ISPs )
number = number + 1
# prints internet users population data
# of top 10 countries by order of their population
def topPopul(data):
print
PrettyLine()
print "Top 10 Countries ranked by population"
PrettyLine()
top10 = selectfirst10(sort_biggest(AddPopul(data)))
prettyPrint(take_out(top10))
PrettyLine()
print
# prints internet users population data
# of 10 countries that have the bigest sum
# of users and active users
def topUsers(data):
print
PrettyLine()
print "Top 10 Countries ranked by the sum of users and active users"
PrettyLine()
top10_users = selectfirst10(sort_biggest(AddUsers(data)))
prettyPrint(take_out(top10_users))
PrettyLine()
print
# prints internet users population data of 10 countries
# that have the biggest active users to population ratio
def topRatio(data):
print
PrettyLine()
print "Top 10 Countries ranked by the ratio of active users to population"
PrettyLine()
top10_ratio = selectfirst10(sort_biggest(AddRatio(data)))
prettyPrint(take_out(top10_ratio))
PrettyLine()
print
# calculates sums for a given quality across all countries data sets
def total(data,n):
q = 0
for country in data:
q = q + country[n]
return q
# outputs some global statistics
def stats(data):
print
PrettyLine()
print "Global Statistics"
PrettyLine()
totalPopul = total(data,1)
print "Total world population (calculated from the data):", totalPopul
totalUsers = total(data,2)
print "Total number of Internet users:", totalUsers
totalActUsers = total(data,3)
print "Total number of active Internet users:", totalActUsers
totalISPs = total(data,4)
print "Total number of ISPs:", totalISPs
print "Percentage of active users relative to Internet users:",\
str(round(float(totalActUsers)/totalUsers*100,3))+ "%"
print "Percentage of Internet users relative to the wold population:",\
str(round(float(totalUsers)/totalPopul*100,3))+ "%"
PrettyLine()
print
def main():
topPopul(statistics)
topUsers(statistics)
topRatio(statistics)
stats(statistics)
main()