Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Opeyemi Adesina, PhD Assistant Professor Computer Information Systems University of the Fraser Valley Office: C2435, Abbotsford Campus Tel: XXXXXXXXXXext: 4931) XXXXXXXXXX Assignment 3 — Modules and...

1 answer below »
Opeyemi Adesina, PhD
Assistant Professo
Computer Information Systems
University of the Fraser Valley
Office: C2435, A
otsford Campus
Tel: XXXXXXXXXXext: 4931)
XXXXXXXXXX
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., you
instructor) are taxed at different levels - Federal and Provincial. Tax amounts are computed ac-
cording to salary
ackets.
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+
ackets 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+
ackets 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/cm
m/n/9 {\OT1/cm
m/n/9 }\OT1/cm
m/n/9 \size@update \enc@update XXXXXXXXXX}
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 occu
ence.
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
oth 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 co
ect 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 co
ect 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 te
itory 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: #e
or 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: #e
or 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: #e
or 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: #e
or 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 te
itory 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: #e
or 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 Extracto
UFV is located on the unceded, traditional te
itory of the Stó:lō people. Page 4 of 4
    Brief Description
    Problem Definitions
    Grading Scheme
    Appendix
Answered Same Day Dec 04, 2021

Solution

Ximi answered on Dec 05 2021
133 Votes
import pandas as pd
df = pd.read_excel('salaries.xls')
print (df.head())
# converting into json structure
json_data = df.apply(lambda record: {key: value for key, value in zip(df.columns, record) }, axis=1)
# writing to a json file
import json
with open('salaries_json.json', 'w') as f:
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here