QUIZ 4
COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_4.pyEnter an Indicator Name: Belly explosion by excessive Coca Cola consumption
Sorry, either the indicator of interest does not exist or it has no data.
$ python3 quiz_4.pyEnter an Indicator Name: Literacy rate, youth total (% of people ages 15-24)
The maximum value is: 100
It was reached in these years, for these countries or categories:
2007: ['Azerbaijan']
2013: ['Moldova']
$ python3 quiz_4.pyEnter an Indicator Name: Age population, age 12, female, interpolated
The maximum value is: 13193254
It was reached in these years, for these countries or categories:
2000: ['China']
$ python3 quiz_4.pyEnter an Indicator Name: Newborns protected against tetanus (%)
The maximum value is: 99
It was reached in these years, for these countries or categories:
2006: ['Bahamas, The']
2007: ['Bahamas, The']
2008: ['Bahamas, The', 'Bahrain']
2009: ['Bahamas, The']
2010: ['Bahamas, The']
2011: ['Bahamas, The']
2012: ['Bahamas, The']
2013: ['Bahamas, The', 'Guyana']
2014: ['Bahamas, The', 'Guyana']
$ python3 quiz_4.pyEnter an Indicator Name: Female headed households (% of households with a female head)
The maximum value is: 49.4
It was reached in these years, for these countries or categories:
2007: ['Ukraine']
$ python3 quiz_4.pyEnter an Indicator Name: Number of neonatal deaths
The maximum value is: 5106312
It was reached in these years, for these countries or categories:
1990: ['World']
$ python3 quiz_4.pyEnter an Indicator Name: Age at first marriage, female
The maximum value is: 33.7
It was reached in these years, for these countries or categories:
1991: ['St. Lucia']
Date: Trimester 1, 2019.
Quiz 4
Uses Heath Nutrition and Population statistics, stored in the file HNP_Data.csv.gz, assumed to be located in the working directory.Prompts the user for an Indicator Name. If it exists and is associated with a numerical value for some countries or categories, for some the years 1960-2015, then finds out the maximum value, and outputs:
that value;
the years when that value was reached, from oldest to more recents years;
for each such year, the countries or categories for which that value was reached, listed in lexicographic order.
1 mark for the maximum value;
1.5 marks for the years and associated countries
import sys
import os
import csv
import gzip
#import pandas as pd
filename = 'HNP_Data.csv.gz'
if not os.path.exists(filename):
print(f'There is no file named {filename} in the working directory, giving up...')
sys.exit()
indicator_of_interest = input('Enter an Indicator Name: ')
first_year = 1960
number_of_years = 56
max_value = None
countries_for_max_value_per_year = {}
with gzip.open(filename) as csvfile:
file = csv.reader(line.decode('utf8').replace('\0', '') for line in csvfile)
my_columns = [0]
my_table=[]
x=0
for i in range(4,60):
my_columns.append(i)
for line in file:
my_table.append([])
if indicator_of_interest in line:
for index_value in my_columns:
my_table[x].append(line[index_value])
x=x+1
if len(my_table)!=[]:
max_v=0.0
LL=[]
c_L=[]
x,y=0,0
for i in range(len(my_table)):
for j in range(1,len(my_table[i])):
if my_table[i][j]!='' and float(my_table[i][j])>max_v:
max_v=float(my_table[i][j])
x,y=i,j
LL=[]
c=my_table[i][0]
LL.append((j+1959,c))
elif my_table[i][j]!='' and float(my_table[i][j])==max_v:
c=my_table[i][0]
LL.append((j+1959,c))
index=0
index_tem=1
if max_v>0:
max_value=my_table[x][y]
LL.sort()
countries_for_max_value_per_year.update({LL[0][0]:[LL[0][1]]})
for i in range(1,len(LL)):
if LL[i-1][0]==LL[i][0]:
countries_for_max_value_per_year[LL[index][0]].append(LL[i][1])
index_tem=index_tem+1
else:
index=index+index_tem
index_tem=1
countries_for_max_value_per_year.update({LL[index][0]:[LL[i][1]]})
if max_value is None:
print('Sorry, either the indicator of interest does not exist or it has no data.')
else:
print('The maximum value is:', max_value)
print('It was reached in these years, for these countries or categories:')
print('\n'.join(f' {year}: {countries_for_max_value_per_year[year]}'
for year in sorted(countries_for_max_value_per_year)
)
)