Question

In: Computer Science

Exercise 2: You are provided with a text file named covid19-3.txt. It reports a few confirmed...

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

Solutions

Expert Solution

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.


Related Solutions

Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you...
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you can submit a zipped file containing the text file. Word, PDF, or Image format are not accepted. You do not need to show screen shot. Make sure you have tested your SQL statements in Oracle 11g. Problem 1. Please create the following tables for a tool rental database with appropriate primary keys & foreign keys. [30 points] Assumptions: Each tool belongs to a category....
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you...
Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you can submit a zipped file containing the text file. Word, PDF, or Image format are not accepted. You do not need to show screen shot. Make sure you have tested your SQL statements in Oracle 11g. The list of tables is: Tables: Cust Table: cid, -- customer id cname, --- customer name cphone, --- customer phone cemail, --- customer email Category table: ctid, ---...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
Create a cronjob which does the following tasks 1.) create a file named <yourname>.txt 2.) in...
Create a cronjob which does the following tasks 1.) create a file named <yourname>.txt 2.) in the 35th minute of the hour change the permission to 755 3.) Create a shell script named first.sh in which you print your name and redirect the output by 45th minute of the hour to the text file
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
A text file called coit20245.txt consists of N lines each containing a name and a family...
A text file called coit20245.txt consists of N lines each containing a name and a family name as follows. N is largest digit of your student id number. Example of coit20245.txt file: Daniel Atkinson Rob Jackson Brian Lara Write a main() method that reads the rows of coit20245.txt and counts the lines which starts with a first letter of your name. You are to provide class comments that describe what the application does. You are also to provide comments that...
demonstrate how to write a file named "hcsi 112.txt" while handling exceptions, the sentence given below...
demonstrate how to write a file named "hcsi 112.txt" while handling exceptions, the sentence given below into separate lines(i.e. each sentence begins from where the comma starts) "how are you today?, my name is Python, am easy to use, simple to write, and object oriented". Also show how to read the lines and store into a list.
Write all your answers for this problem in a text file named aa.txt – for each...
Write all your answers for this problem in a text file named aa.txt – for each problem write the problem number and your answer. 1.1 What type of object can be contained in a list (write the letter for the best answer)? a. String b. Integer c. List d. String and Integer only e. String, Integer, and List can all be contained in a list 1.2 Which statement about the debugger is not correct? a. It is a powerful tool...
IS 633 Assignment 1 Due 9/27 Please submit SQL statements as a plain text file (.txt)....
IS 633 Assignment 1 Due 9/27 Please submit SQL statements as a plain text file (.txt). If blackboard rejects txt file you can submit a zipped file containing the text file. Word, PDF, or Image format are not accepted. You do not need to show screen shot. Make sure you have tested your SQL statements in Oracle 11g. Problem 1. Please create the following tables for a tool rental database with appropriate primary keys & foreign keys. [30 points] Assumptions:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT