New folder/Assignment _Sem2_2013.docx Assignment 2 Semester 2, 2013 Introduction This assignment requires you to work with some lists and to explore some common algorithms.
New folder/Assignment _Sem2_2013.docx
|
ITECH1000/5000 Programming 1 School of Science, Information Technology and Engineering |
|
Assignment 2
Semester 2, 2013
Introduction
This assignment requires you to work with some lists and to explore some common algorithms. You will be working with a list of bank records which contain details of individual bank customers and their accounts.
You will be required to provide a hardcopy of a written report (a word document) with some of your answers. You will also be required to hand the whole assignment (both code and reports) in electronic form.
Task A
Download the files bank.txt and assignment2.py. The file bank.txt is the input file for the program. It consists of a number of lines of text, each with five fields. These fields are:
· The customer’s first name
· The customer’s last name
· The customer’s account number
· The account type (savings, credit or cheque)
· The account balance
Assignment2.py already has some functionality added – ignore that for the moment.
For each of the following functions provide the expected inputs, outputs and pseudocode in your report (note: you will probably find the assignment easier if you do this BEFORE you do your coding)
a) Write a new function (called readInputFileIntoList) which will read the file bank.txt and place individual customer records into a list (effectively a ‘list of lists’). The function should NOT take the bank list as a parameter. It should return the bank list via its return statement. You may not assume that you know how many records are in the input file – it must work for any number of lines. For each line in the input file you should create an individual list with five elements (listed above). Use the ‘split’ function to help you with this. Once you have created this list, add it to the bank list. The result of executing readInputFileInfoList should be a list of bank records – one for each line in the file.
b) Write a function called ‘printIndividualRecord’. It should take a list (corresponding to a single bank record) as a parameter. If the record is Bruce Willis 123313 savings 405.00
the function should print out the details of the record in the following format
Bruce Willis has a savings account with account no: 123313
There is $405.00 in this account.
c) Write a function called ‘printAllRecords’, which calls printIndividualRecord for each record in the bank list. It should take the bank list as a parameter.
Task B
Examine the functions already authored in assignment2.py.
· swap
· maxListByItem
In the code, write a high-level description of what each of these functions is intended to do as a comment above the function code. This description should be no longer than five lines. Marks will be awarded according to how well your response reflects your understanding.
Task C
Examine the function ‘sortListUsingSelectionSort’ that has been written in assignment2.py. This function is not complete. Use the functions from task B to complete the implementation of this function. Note – it should not be necessary to move (change the order of) or delete the code that is already there. When complete the function should have sorted the list in descending order (biggest item first).
In the code (not the report) add a brief comment (1 or 2 lines maximum) to each line to explain how the line contributes to the implementation of the selection sort.
Also add a comment above the function to describe to someone else who might want to use it at a later time. This comment should be no longer than 5 lines. Marks for the comments will be awarded according to how well they reflect your understanding of the code.
Task D
Write functions to perform the following tasks. Each function should be properly commented, use well-chosen identifier names and have appropriate parameters and any other good programming practices.
1) Write a function, called ‘sortAccountTypeLastName’ that sorts the list by the account type. If account types are the same, sort the list by last name. You can use either insertion sort or bubble sort for this function. The original list should be passed in as a parameter. This list should be copied into a second list which should be sorted and returned by the function.
2) Write a function, called ‘printListToFile’, that will write the details of a list to a file called ‘bank.out’. The format for the output file should be the same as the input file. In other words, it should be possible to use ‘bank.out’ as input to the program.
3) Write a function, called ‘addInterest’ that charges 10% interest to each account with a negative account balance. Other account balances should remain unchanged.
4) Write a function, called ‘binarySearch’, implements a binary search. This function should take three parameters.
a. the target (the value that you are looking for)
b. the field being searched(eg account type, account balance etc.).
c. the list itself
If the target is found, return the index value where it was found. Otherwise the function should return -1.
5) Write a function, called ‘sortAnyTwoFields’ which performs a similar task to ‘sortAccountTypeLastName’ but which is more general. This function should have three parameters
a. field1 – the first field to be sorted on
b. field2 – the field to be sorted on if the values of field1 are equal
c. bList – the bank list
The original list should be passed in as a parameter. This list should be copied into a second list which should be sorted and returned by the function. For example
sortAnyTwoFields (CUSTOMER_LAST_NAME, CUSTOMER_ACCOUNT_BALANCE, bankList)
will sort by the customer’s last name. If the last names are the same, then the list is sorted by account balance.
Assignment 2
Mark Sheet - ITECH1000-5000
Student Name: ____________________ Id: _____________ Marker: _______
|
Task |
Comments |
Mark |
Max |
|
Section A |
|
|
6 |
|
|
readInputFileIntoList |
|
2 |
|
|
printIndividualRecord |
|
1 |
|
|
printAllRecords |
|
1 |
|
|
documentation |
|
2 |
|
|
|
|
|
|
Section B |
|
|
2 |
|
|
documentation |
|
2 |
|
|
|
|
|
|
|
|
|
|
|
Section C 3 |
|||
|
|
2 for functionality, 1 for comments |
|
3 |
|
|
|
|
|
|
Section D |
|
|
9 |
|
1) |
sortAccountTypeLastName |
|
2 |
|
2) |
printListToFile |
|
2 |
|
3) |
addInterest |
|
2 |
|
4) |
binarySearch |
|
2 |
|
5) |
sortTwoFields |
|
1 |
|
|
|
|
|
|
Deductions |
comments missing, inappropriate names, failure to use defined methods to access objects |
|
max 6 |
|
Total(20) |
|
|
20 |
|
CRICOS Provider No. 00103D |
|
Page 6 of 6 |
image1.jpeg
image2.jpeg
New folder/assignment2.py
import string CUSTOMER_FIRST_NAME = 0 CUSTOMER_LAST_NAME = 1 CUSTOMER_ACCOUNT_NO = 2 CUSTOMER_ACCOUNT_TYPE = 3 CUSTOMER_ACCOUNT_BALANCE = 4 def readInputFileIntoList (): print "readInputFileIntoList is yet to be implemented" def printIndividualRecord (record): print "printIndividualRecord yet to be implemented" def printAllRecords (bList): print "printAllRecords yet to be implemented" def swap (pos1, pos2, bList): temp = bList[pos1] bList[pos1] = bList[pos2] bList[pos2] = temp def maxListByItem (item, bList): if len(bList) == 0: print "no items in list" return 0 maximum = bList[0][item] count = 0 pos = 0 for record in bList: if maximum < record[item]: maximum = record[item] pos = count count = count + 1 return pos def sortListUsingSelectionSort (item, bList): sortedList = list(bList) pos = 0 for index in range(len(sortedList)): #insert code here return sortedList def sortAccountTypeLastName (bList): print "sortAccountTypeLastName yet to be implemented" def printListToFile (bList): print "printListToFile yet to be implemented" def addTenPercent (bList): print "addTenPercent is yet to be implemented" def binarySearch (target, item, bList): print "binarySearch yet to be implemented" def sortAnyTwoFields (field1, field2, bList): print "sortAnyTwoFields yet to be implemented" def main (): bankList = readInputFileIntoList () printAllRecords (bankList) sortedList = sortListUsingSelectionSort (CUSTOMER_ACCOUNT_NO, bankList) printAllRecords (sortedList) pos = binarySearch ("Phili", CUSTOMER_FIRST_NAME, sortedList) if (pos == -1): print "Item not found" else: printIndividualRecord (sortedList[pos]) addTenPercent (bankList) sortedList = sortAccountTypeLastName (bankList) printAllRecords (sortedList) printListToFile (sortedList) main ()
New folder/bank.txt
Phil Smith 321971 cheque 420.90 Eldar Hajilarov 567834 savings 670.25 Sasa Ivkovic 452312 credit 190.80 Charlynn Miller 823848 cheque 23.49 Adil Bagirov 723124 credit -873.56 Ewan Barker 812840 cheque -22.45 David Gao 452353 credit 512.90 Shannon Bell 123948 credit 2090.45 Phil Smith 542321 credit 657.89 Marcello Bertoli 672314 savings 234.76 Alex Kruger 812043 cheque -900.82 Leo Brunelle 823843 savings 1293.87 Grant Meredith 380284 savings 438.00 Peter Vamplew 928483 cheque -658.80 Glenn Stevens 284934 credit 1892.00 Jason Giri 453920 cheque 298.00 Peter Martin 659330 savings 902.89 Kylie Turville 349583 credit 800.78 Chris Turville 568373 credit -900.00 Phil Smith 292871 savings 60.00 Daniel Morales 87383 cheque 288.00 Guillermo PinedaVillacencio 898989 cheque 567.44 Shamsheer Syed 837437 credit 452.33 Long Jia 283923 savings 190.00 David Stratton 342873 credit 890.00 Greg Simmons 382834 savings -899.00 Musa Mammadov 990990 credit -900.00 Elizabeth Matuschka 700638 savings 200.00 Kaye Lewis 891212 savings 344.00 Frank Deluca 672314 credit 500.00 David Gao 568933 credit -400.00 Brian Firth 828392 savings -450.00 Evan Dekker 434023 cheque -900.00 Sally Firman 932983 cheque -730.00 Rosemary Torney 485374 savings 898.00 Marijke Groothuis 943823 credit 890.00