159.171 Assignment 2 Page 1 of 5
159.171 - Assignment 2 – 2019
Worth: 12% of final mark
Due date: 6pm Friday, May 3rd
For all three programs:
use meaningful names for variables and functions.
add any comments you think will help explain what your program's doing.
Question 1: Monthly Tax Deduction Table (4 marks)
Write a program that calculates tax deductions from monthly earnings ranging from $1000 to
$2000. Display the results.
Tax deductions include:
PAYE (rate 18%): Pay as you earn
PAYE = Earnings * 18%
Student Loan Deduction (rate 12%): Student loan repayment
Repay 12% of every dollar earned over the repayment Threshold that is $1536 monthly.
Student Loan Deduction = (Earnings – Threshold) * 12%
For example, if you earn $1900 a month before tax, your repayment will be $43.68
(($1900 - $1536) * 12% = $43.68).
If you earn under the repayment threshold, no student loan deductions will be made from
your earnings.
KiwiSaver (rate 3%): Retirement saving scheme
KiwiSaver = Earnings * 3%
A person’s monthly net earnings will be the amount after deducting the above three deductions
from the raw earnings.
Net Earnings = Earnings – PAYE – Student Loan Deduction - KiwiSaver
159.171 Assignment 2 Page 2 of 5
An example of the layout formatting is shown below:
Earnings $ PAYE $ ST Loan Ded $ KiwiSaver $ Net Earnings $
1000 180 0 30 790
1100 198 0 33 869
1200 216 0 36 948
1300 234 0 39 1027
1400 252 0 42 1106
1500 270 0 45 1185
1600 288 7.68 48 1256.32
1700 306 19.68 51 1323.32
1800 324 31.68 54 1390.32
1900 342 43.68 57 1457.32
2000 360 55.68 60 1524.32
So, if a person earns $1400 per month, he/she must pay $252 for PAYE and $42 for KiwiSaver,
then the net amount he/she can get is $1106.
If a person earns $1800 a month, the deductions are $324 for PAYE, $31.68 for Student Loan
Repayment, and $54 for KiwiSaver, then the net amount he/she can get is $1390.32.
IMPORTANT
You MUST use nested for-loops, one inside the other.
If you just print out the lines using fixed numbers, like this:
print("1500 270 0 45 1185")
print("1600 288 7.68 48 1256.32")
you'll get zero marks.
Hints:
You can set the increment of the sequence numbers of earnings to 50 or 100.
Values should be formatted to 2 decimal places.
159.171 Assignment 2 Page 3 of 5
Question 2: Handling Text Files (4 marks)
Download the files-info.txt from Stream. This text file contains the results of the “ls -l”
command, which outputs file permissions, number of links, owner name, owner group, file size,
time of last modification, and file/directory name. files-info.txt is formatted with one file per
line. (An example is shown below.)
Your task is to read this text file, clean it up by removing the permissions, number of hard links,
owner, and the date/time of last modification in each line. Namely, each line should only contain:
owner group, file size, and file name. Take the above for an example, after removing, it should
look like: root 4096 ataka.
Then find and display following statistics:
Count the number of files
Count the number of file formats, and display these formats. The file format can be
determined easily through the extension name of the file that appears after the name of
the file, followed by a dot. For example, the name of a text file format ends with .txt, a zip
file format ends with .zip
Count the number of zip files, for example, files with the .zip extension are zip files
Count the number of files that are owned by the staff group. Check whether the owner
group is staff.
Count the number of files, which are in text format and are larger than 200,000. Then sort
the list of text files greater than 200,000, and display the top 5 largest text files, at least
the file name and size should be printed out.
The average size of files
159.171 Assignment 2 Page 4 of 5
Question 3: A File Information Explorer (4 marks)
Still use files-info.txt on Stream. For this question, you are to build a way of displaying a
random file information, finding files, replacing and saving a list of files that meet certain
requirements.
The program first displays a menu (something like that shown below) and carries out the
appropriate action depending on which letter the user types and then redisplays the menu:
***File Information Explorer ***
l – load
r – random file
s – search
fr – find and replace
fs – find then save
q – quit
l – load
Load a file. Prompt for the name of a file. If no name is input (e.g. if using input() and the
user presses the enter key), load from “files-info.txt”, otherwise load it from the specified
filename. files-info.txt is formatted with one file per line.
r – random file
Randomly pick a file and display it with permissions, owner, owner group, size,
date/time, and name.
s – search
Prompt for a string, and then search for and display all files whose name contains this
string. Search case-independently, and separate the files with a blank line. If the search
string is empty, exit the search and redisplay the menu.
fr – find and replace
Prompt for one string (the search pattern) and then a second (the replacement). Replace
all occurrences of the first string with the second.
fs – find then save
Find all files in a specified format, then save the results to a file.
First, asking the user to input the file format, e.g. File format: txt
Second, prompt for a filename, e.g. File name: textfiles.txt
Then, search and find all files in this specified format.
Finally save the results to the file inputted by the user (e.g. textfiles.txt), inserting
“\n” where appropriate. The results should include permissions, owner, owner group,
size, date/time, and name for each file.
q – quit
Quit the program
159.171 Assignment 2 Page 5 of 5
Some suggestions on getting started:
Get the menu going and then create a function for each of load, search, ... Initially, simply
put a print inside each function, e.g. for the search function, put print("You called
SEARCH"). You can test each command – it will simply print out a message.
Then get load going so you can load a text file into a list. First get the name of the file,
next open the file, then load the file into the list and close the file. Test that you have
valid entries by printing out the first two files.
Implement the random command next. If you import the random module, you'll find the
random.choice() method useful here. It returns a randomly chosen element from a list,
e.g. print(random.choice([1, 2, 3, 5, 9])) .
Finally implement the rest of commands separately.
Hints:
Don’t forget to add appropriate checks, e.g. to make sure files-info.txt has been loaded.
Each command could be in their own function. You can of course, create any additional
functions that will help to simplify or clarify your code.
What and how to submit
How: ALL submissions must be via Stream (not email) using the Assignment 2 submission link.
What: Submit your Python programs, each named with .py extensions. You should have
three .py files to submit, one for each question.
Do not submit Word documents (.doc or .docx) or .zip or .rar files.
Check:
that your files have a .py extension
that all programs display your name and your Massey Student ID number when starting.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。