a3_instruction.pdf is the question
Opeyemi Adesina, PhD Assistant Professor Computer Information Systems University of the Fraser Valley Office: C2435, Abbotsford Campus Tel: (604) 504-7441 (ext: 4931)
[email protected] Assignment 3 — Modules and File Processing COMP 150 : Introduction to Programming (100 points) When Due : December 05, 2019 – 23:59:00 [Submission via Blackboard] Brief Description This assignment amounts to 7.5% of the entire course grade. In particular, whatever your obtains as a score will be scaled to this value for final grade computation. You are required to work (or do this assignment) ALONE. No late submission will be permitted (see deadline above). The goal of this assignment is to assess your knowledge and skills mainly on modules and file pro- cessing but partly on looping constructs, functions and lists. It is an update on the second assignment. You will find a grading scheme at the end of this document – to guide you on instructor’s expectations while preparing your submission. Problem Definitions 1. British Columbia’s (BC) Income Tax Calculator: Employees within BC province (e.g., your instructor) are taxed at different levels - Federal and Provincial. Tax amounts are computed ac- cording to salary brackets. British Columbia’s (i.e., provincial) government tax amount is given as follows: 5.1%, 7.7%, 10.5%, 12.3%, 14.7% and 16.8% for salaries in [$0 .. $41,000), [$41,000 .. $81,000), [$81,000 .. $93,000), [$93,000 .. $114,000), [$114,000 .. $154,000), and $154,000+ brackets respectively. In 2019, federal (this includes employees in BC) level tax amount is given as follows: 15.0%, 20.5%, 26.0%, 29.0% and 33.0% for salaries in [$0 .. $48,000), [$48,000 .. $95,000), [$95,000 .. $148,000), [$148,000 .. $210,000), and $210,000+ brackets respectively. (a) Write a program that: • take gross income of N employees from an excel file (*.xls or *.xslx format), convert the file to its JSON equivalent (for interoperability purposes); 1 mailto:{\protect \edef cmr{qag}\protect \xdef \OT1/cmr/m/n/9 {\OT1/cmr/m/n/9 }\OT1/cmr/m/n/9 \size@update \enc@update
[email protected]} COMP 150: Assignment 3 — Modules and File Processing • read gross income of N employees from the generated JSON file and do the following: – compute their net incomes (i.e., after deducting appropriate provincial and federal level taxes); – compute and display their population variance, median and all modes (use Dictionary data structure) for the entire net incomes; and – display a list of all gross incomes and a list of all net incomes. Things to note: • Median – Computations differ for even and odd lengths of any given list. – Even length: let lst = [1, 2, 8, 5, 6, 3], lst = sort(lst) => median(lst) = (3 + 5)/2 = 4 – Odd length: let lst = [1, 2, 8, 5, 6], lst = sort(lst) => median(lst) = 5 • Mode – By definition, it is the net income with the highest frequency. But since there may be more than one net income with same but highest frequency – you are to compute the list of all net incomes with highest number of occurrence. For example, let lst = [5, 4, 3, 2, 1, 3, 2, 1] => modes(lst) = [3, 2, 1] • Excel File – Two files of data (randomly generated) in excel formats. The files contain exactly the same data – you may use any of them in your program. If you decide to try both of them out – they should give the same output. • Reading Excel and Generating JSON – In the appendix page, you will find a working code to generate JSON and extract a list of of gross incomes from the input excel file. Your task is to engineer these functions in order to produce a list of gross incomes to compute median and mode. Grading Scheme The following scheme will be used to grade your submission. Therefore, you may also use it as a guide in preparing your deliverable. Grade Item Weight A syntactically and semantically correct program. 50 A program with a detailed high-level program design - see my solution to assignment 2. I am enforcing program design this time. 15 A very high-level flowchart (see solution to assignment 2) of your program must be included. This must represent your pro- gram. You can use pen and paper for drawings. At this point you should decompose the problem into separate components, draw them each and show how the components will be coupled to realize your code. 15 Program efficiency. That is, using efficient but correct control structures. 10 A program with detailed program documentation and uses sen- sible variable names. Your program’s file name and other files should be zipped and named in the following format - [first- Name lastName studentID] 10 Total 100 UFV is located on the unceded, traditional territory of the Stó:lō people. Page 2 of 4 COMP 150: Assignment 3 — Modules and File Processing Appendix 1 #Author - Opeyemi Adesina 2 #Institution - University of the Fraser Valley 3 #Academic Session - Fall, 2019 4 import xlrd 5 import json 6 7 #takes in filename, its extension and sheet id of interest 8 def fileParser( fileName, extension, sheetNum ): 9 store = [] #List of objects to be parsed into json file 10 framenames = [] 11 file = str( fileName ) + str(’.’) + str( extension ) #composing a fully-qualified file name 12 13 try: #error handling 14 book = xlrd.open_workbook( fileName ) #opening the excel sheet 15 except: 16 print ("File Not Found!!!") #user-readable output 17 quit() #program terminates gracefully 18 19 try: #error handling 20 sheetIndex = book.sheet_by_index( sheetNum ) #refering to the first sheet within 21 except: 22 print("Sheet Not Found!!!") #user-readable output 23 quit() #program terminates gracefully 24 25 for row in range(1, sheetIndex.nrows): 26 27 if sheetIndex.row(row)[0].value not in framenames: 28 try: #error handling 29 framenames.append(sheetIndex.row(row)[0].value) 30 frame = { 31 "S/N": int(sheetIndex.row(row)[0].value), 32 "Full Name": sheetIndex.row(row)[1].value, 33 "Gross Income CAD($)": sheetIndex.row(row)[2].value, 34 "Sex": sheetIndex.row(row)[3].value, 35 "Date of Birth": sheetIndex.row(row)[4].value, 36 "Marital Status" : sheetIndex.row(row)[5].value 37 } 38 except: 39 print("Row Not Found!!!") #user-readable output 40 quit() #program terminates gracefully 41 42 #print(store) 43 store.append(frame) 44 45 return store 46 47 48 #JSON Generator -- takes in an excel file (*.xls or *.xslx -- not *.csv) 49 def generateJSON ( fileName, extension ) : 50 51 store = fileParser( fileName, extension, 0 ) 52 fileName = str(fileName) + str(’.’) + str(’json’) #composing a fully-qualified file name 53 54 try: #error handling 55 f = open( fileName, ’w’) 56 except: 57 print("File Not Found!!!") #user-readable output 58 quit() #program terminates gracefully 59 out = json.dumps(store, indent = 2) 60 f.write(out) 61 62 63 UFV is located on the unceded, traditional territory of the Stó:lō people. Page 3 of 4 COMP 150: Assignment 3 — Modules and File Processing 64 #Processes the JSON file and generates a list of gross incomes 65 def extractGrossIncomes (fileName, extension ) : 66 67 grossIncomeList = [] 68 fileName = str(fileName) + str(’.’) + str(extension) #composing a fully-qualified file name 69 70 try: #error handling 71 with open(fileName, ’r’) as employeesFile: 72 employeesDictionary = json.load( employeesFile ) 73 except: 74 print("File Not Found!!!") #user-readable output 75 quit() #program terminates gracefully 76 77 #creating the list of gross incomes from employees’ dictionary produced from JSON 78 for employee in employeesDictionary: 79 grossIncomeList.append( float( format( employee[’Gross Income CAD($)’] , ’.2f’ ) ) ) 80 81 return grossIncomeList Listing 1: Excel File Parser – JSON Generator – Gross Income Extractor UFV is located on the unceded, traditional territory of the Stó:lō people. Page 4 of 4 Brief Description Problem Definitions Grading Scheme Appendix