In: Computer Science
Exercise 2:
You are provided with a text file named covid19-3.txt. It reports a
few confirmed cases of covid19. It consists of three columns. The
1st column indicates the names of the provinces, the 2nd column
indicates the names of the countries and the 3rd column indicates
the numbers of confirmed cases.
To do:
1. Define a function that reads, from covid19-3.txt, provinces,
countries and confirmed cases in three separate lists.
2. Define a function that iterates through a list (or array) from
the second element to the last and finds the smallest elements
between them.
3. Define a function that sorts confirmed cases from largest to
smallest. The sorted numbers must be returned by the function in a
list or an array. The function should also return the names of the
provinces in the order of confirmed cases.
Note:
1. You have to design your functions yourself, including input and
output parameters.
2. You need to manually check the preprogram result whether the
sorting (confirmed cases and also provinces) is done correctly or
not.
data::
Province,Country,Confirmed
Anhui,Mainland China,1
Beijing,Mainland China,14
Chongqing,Mainland China,6
Fujian,Mainland China,1
Gansu,Mainland China,0
Guangdong,Mainland China,26
Guangxi,Mainland China,2
Guizhou,Mainland China,1
Hainan,Mainland China,4
Hebei,Mainland China,1
Heilongjiang,Mainland China,0
Henan,Mainland China,5
Hong Kong,Hong Kong,0
Hubei,Mainland China,444
Hunan,Mainland China,4
Inner Mongolia,Mainland China,0
Jiangsu,Mainland China,1
Jiangxi,Mainland China,2
Jilin,Mainland China,0
Liaoning,Mainland China,2
Macau,Macau,1
Ningxia,Mainland China,1
Qinghai,Mainland China,0
Shaanxi,Mainland China,0
Shandong,Mainland China,2
Shanghai,Mainland China,9
Shanxi,Mainland China,1
Sichuan,Mainland China,5
Taiwan,Taiwan,1
Tianjin,Mainland China,4
Tibet,Mainland China,0
Washington,US,1
Xinjiang,Mainland China,0
Yunnan,Mainland China,1
Zhejiang,Mainland China,10
Xima,Japan,2
Mano,Thailand,2
Suool,South Korea,1
Anhui,Mainland China,9
Beijing,Mainland China,22
Chongqing,Mainland China,9
Fujian,Mainland China,5
Gansu,Mainland China,2
Guangdong,Mainland China,32
Guangxi,Mainland China,5
Guizhou,Mainland China,3
Hainan,Mainland China,5
Hebei,Mainland China,1
Heilongjiang,Mainland China,2
Henan,Mainland China,5
Hong Kong,Hong Kong,2
Hubei,Mainland China,444
Hunan,Mainland China,9
Inner Mongolia,Mainland China,0
Jiangsu,Mainland China,5
Jiangxi,Mainland China,7
Jilin,Mainland China,1
Liaoning,Mainland China,3
Macau,Macau,2
Ningxia,Mainland China,1
Qinghai,Mainland China,0
Shaanxi,Mainland China,3
Shandong,Mainland China,6
Shanghai,Mainland China,16
Shanxi,Mainland China,1
Sichuan,Mainland China,8
Taiwan,Taiwan,1
Tianjin,Mainland China,4
Tibet,Mainland China,0
Washington,US,1
Xinjiang,Mainland China,2
Yunnan,Mainland China,2
Zhejiang,Mainland China,27
Python code for above problem
# (1). method that reads data from file and returns lists
def read_data():
fp=open("covid19-3.txt","r")
provinces=[]
country=[]
confirmed=[]
lines=fp.readlines()
lines.pop(0)
for line in lines:
values=line.split(",")
provinces.append(values[0])
country.append(values[1])
confirmed.append(int(values[2]))
fp.close()
return provinces,country,confirmed
# (2). method that prints information about smallest number of
confirmed cases
def smallest(provinces,country,confirmed):
min_value=min(confirmed)
print("Information about whose confirmed cases are
smallest are as follows:")
for i in range(len(provinces)):
if confirmed[i]==min_value:
print(provinces[i],country[i],confirmed[i])
print("")
# helper method to print data
def print_data(provinces,country,confirmed):
print("Detailed information is as follows:")
for i in range(len(provinces)):
print(provinces[i],country[i],confirmed[i])
print("")
# helper method to swap 2 indices in given list
def swap(lst,a,b):
temp=lst[a]
lst[a]=lst[b]
lst[b]=temp
# (3): method that sorts the lists in descending order of
confirmed
def sort_data(provinces,country,confirmed):
for i in range(len(provinces)):
for j in
range(i+1,len(provinces),1):
if
confirmed[i]<confirmed[j]:
swap(provinces,i,j)
swap(country,i,j)
swap(confirmed,i,j)
# testing main method
def main():
provinces,country,confirmed=read_data()
smallest(provinces,country,confirmed)
sort_data(provinces,country,confirmed)
print_data(provinces,country,confirmed)
main()
Sample output
if "covid19-3.txt" has following data
Province,Country,Confirmed
Anhui,Mainland China,1
Beijing,Mainland China,14
Chongqing,Mainland China,6
Fujian,Mainland China,1
Gansu,Mainland China,0
Guangdong,Mainland China,26
Guangxi,Mainland China,2
Guizhou,Mainland China,1
Hainan,Mainland China,4
Hebei,Mainland China,1
Heilongjiang,Mainland China,0
Henan,Mainland China,5
Hong Kong,Hong Kong,0
Hubei,Mainland China,444
Hunan,Mainland China,4
Inner Mongolia,Mainland China,0
Jiangsu,Mainland China,1
Jiangxi,Mainland China,2
Jilin,Mainland China,0
Liaoning,Mainland China,2
Macau,Macau,1
Ningxia,Mainland China,1
Qinghai,Mainland China,0
Shaanxi,Mainland China,0
Shandong,Mainland China,2
Shanghai,Mainland China,9
Shanxi,Mainland China,1
Sichuan,Mainland China,5
Taiwan,Taiwan,1
Tianjin,Mainland China,4
Tibet,Mainland China,0
Washington,US,1
Xinjiang,Mainland China,0
Yunnan,Mainland China,1
Zhejiang,Mainland China,10
Xima,Japan,2
Mano,Thailand,2
Suool,South Korea,1
Anhui,Mainland China,9
Beijing,Mainland China,22
Chongqing,Mainland China,9
Fujian,Mainland China,5
Gansu,Mainland China,2
Guangdong,Mainland China,32
Guangxi,Mainland China,5
Guizhou,Mainland China,3
Hainan,Mainland China,5
Hebei,Mainland China,1
Heilongjiang,Mainland China,2
Henan,Mainland China,5
Hong Kong,Hong Kong,2
Hubei,Mainland China,444
Hunan,Mainland China,9
Inner Mongolia,Mainland China,0
Jiangsu,Mainland China,5
Jiangxi,Mainland China,7
Jilin,Mainland China,1
Liaoning,Mainland China,3
Macau,Macau,2
Ningxia,Mainland China,1
Qinghai,Mainland China,0
Shaanxi,Mainland China,3
Shandong,Mainland China,6
Shanghai,Mainland China,16
Shanxi,Mainland China,1
Sichuan,Mainland China,8
Taiwan,Taiwan,1
Tianjin,Mainland China,4
Tibet,Mainland China,0
Washington,US,1
Xinjiang,Mainland China,2
Yunnan,Mainland China,2
Zhejiang,Mainland China,27
then after running the above code, output looks as follows:
Mention in comments if any mistakes or errors are found. Thank you.