explain and extend a python program that manipulates some data involving flight schedule system to and from various locations in Australia.This is an extension of assignment one. The main focus of your extension is to provide a facility to sort the flights and save the sorted list of flights into a new file.
You are provided with 3 files:
• this document
• a data file, “flights.txt”
• a Python file, “flightsystem-sorting.py”
1) Answer specific questions throughout the assignment in a separate Word document. Marks are awarded according to how well your answers reflect an understanding of the concepts involved.
2) Make and document changes to the python code as required. Save your changes. Comment the code.
The data file,“flights.txt”, consists of a number of lines (records) each of which have seven values (fields) separated by spaces:
• A Flight code (e.g. QF345, VB123)
• A code number (1-7) representing the weekday for departure of the flight 1=Sunday, 2=Monday…
• The Departure city (e.g. MEL, SYD, BNE, ADE, PER, TSV)
• The Destination city (e.g. MEL, SYD, BNE, ADE, PER, TSV)
• The Departure Time in 24hr format (e.g. 0800, 1230, 2115)
• A number representing the departure gate (expected to be between 1 and 15)
• The ticket price for the flight
ITECH1000 students should respond to sections A, B, and C.
ITECH5000 students should respond to sections A, B, C and D.
answer the following questions relating to the .
1) If someone were to sabotage your flight system code and change the constant DATA_FILE to equal test.txt, what impact would this have on the system? Explain why and in what circumstances the system would be unreliable/incorrect.
2) Explain in detail what the following code does in addflight function:
outfile = open(DATA_file, ‘a”)
3) What is the purpose of the isGT(flightslist, skey, posn1, posn2) function?
4) Examine the convertNumbers(inf), how could you make the code more readable?
5) Explain the use of result in isGT.
6) Examine writeout function definition, explain what arguments does it take? What does the following line do:
str(keyposn + 1)
7) Explain the line ‘sortkeyval = eval(sortkey) -1′ that occurs in the elif choice ==13 block.
8) Examine bubblesort function. Explain in words how it works. Why do we use two for loops?
9) Have a look at the function convertNumbers, particularly the line:
convertedFlightlist.append([element, element, element, element, element, eval[element, eval(element)])
What would this line do? Why eval function is used for element and element? What happens if the eval function is not used? Will the program still work?
Make the following modifications to the existing programming in addition to written answers. Document each change with a comment in your code. The content of this section is supported by your lab work.
1) Before making any modifications, test the working of the existing file by choosing option 1 followed by option 20. If there is any problem, consult your lecturer or lab tutor.
2) Write the code for function isGT(). You have the flights in a list of lists, so first, extract each of the 2 flight details lists into 2 lists list1 and list2. Then you need to compare the values in each of these lists at the key position. Save the Boolean result of the comparison in result and return result as the return value for this function.
3) Complete the code for function swap(sList, posn1, posn2) This function takes the list sList and swaps the items at posn1 and posn2.Explain how it is used in bubblesort function.
4) Examine the mysort() function. Explain what it is doing. Which inbuilt python function is used to sort?Explain the arguments that are given to sorted. Explain in detail the algorithm for mysort().
5) The function bubbleSort() is incomplete. All the code is there, but comments to explain the code are missing. Insert the comments in the python code to appropriately explain how it works.
You are to completely write a new sort function, using the algorithm: insertionSort. Before you write the code, you should design your logic, write your algorithms, and document your design.
In this section, you are required to document, for all functions to be written:
• all parameters, describing each data type and what each represents;
• all outputs according to the return statements; if there is no data returned then what effect has been achieved by the processing within the function;
• pseudocode representing the processing required within the function.
1) Examine the code of function insertionSort that is provided in comments BGT58
def insertionSort (tList):
sList = list (tList)
for index1 in range(1,len(sList)):
index2 = index1 # first element of unsorted part
currNum = sList[index1] # value at this position
while (currNum < sList[index2 – 1] and index2 != 0):
index2 = index2 – 1
sList.insert (index2, sList.pop(index1)) #insert at the correct spot
The function insertionSort for theflights can be modelled on this and aspects of mysort() and bubbleSort(). Use the functions convertNumbers(), isGT(), swap() and writeout() in your code.
2. Write the algorithm pseudocode, describe your inputs (parameters) and outputs for insertionSort of the flights schedule list
3. Write your code to implement insertionSort, making sure you modify the above provided code to work for your flights lists to get the value at a given key position.
4. Explain what the following code does in the function insertionSort:
sortedList.insert (index2, sortedList.pop(index1))
5. Test by adding another two flights and then displaying the updated file with option 1 then sorting with option 13. Explain how you chose the flight details and the results of your tests.