Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENGG 1330 Computer Programming I (22-23 Semester 2)

Assignment 2

Due date: noon, 2-May-2023 (Tue)

Late submission: 20% deduction per day

Introduction

This assignment provides you an opportunity to practice your programming skills, particularly on use of functions, control flow, file handling, different data structures, and command line processing.

You have probably used the MTR’s trip planner (https://www.mtr.com .hk/en/customer/jp2/) to get around Hong Kong. After we enter the origin and destination stations, the software calculates  all the different options for the journey and tells us the quickest and alternative options.              Whenever there are multiple routes, such as in a big city with complex transit network like Hong    Kong, this leads to a great number of possible options and therefore a computationally expensive task.

In this assignment you are going to code a simplified travel planner for the MTR sub-network using the knowledge you have learnt.

For ease of explanation, a sample sub-network consisting of two partial lines is shown below in Figure 1 for guiding you through the tasks.  For the sake of doing this assignment, you need to consider a      larger sub-network consisting of additional partial lines given at the end this worksheet. The given     sub-network will be used for evaluating your submitted programs. To reduce the difficulty, it is   assumed that there are at most two lines passing through an interchange station in the given      sub-network.

 

Figure 1. An MTR sub-network (adapted from                                                                  https://www.mtr.com.hk/en/customer/jp/mtrMap.html). Red line: Tsuen Wan line, blue line: Island line.

Important notes: The naming of lines and stations follows the legend and labels on the MTR network map, which is available on                                                                                      https://www.mtr.com.hk/en/customer/images/jp/system_map.png.

Level 1 (10%): Standardize format of names from user input

In this task, you will standardize the format of names input by user.

1)   Ask user for the name of a line (e.g., Island line) or the name of a station (e.g., HKU). The input can be in either upper or lower case.

2)   Format the input name in title case, except for HKU, which must be all in upper case.  You are not allowed to use the .title() method.

3)   Print the formatted name.

Input:

•   The name of a line or the name of a station

Output:

•    Print the name in title format (except for HKU), ended with a newline.

Examples:

 

Level 2 (20%): Construct lines and stations from input

In this task, you will construct lines and stations and save them in text files for display in specified format.

1)   Ask the user for the number of lines to be constructed.

2)   For each line, ask for the name of a line and the names of stations on the line, until the stop code '-1' is entered.

3)   Format the names according to the requirements described in Level 1.

4)   Create a text file using the formatted name of the line.

5)   Store in the file the formatted names of stations in input order, one station per line.    Below is an example text file of "Tsuen Wan Line" as shown in Figure 1.  Note that one file is created for each line.

 

6)   Read the created files in input order.

7)   For each file, print the name of the line followed by the names of stations.

Inputs:

    Number of the lines to be constructed

   Name of each line

•    For each line, names of stations on the line, until the stop code '-1' is entered

Outputs:

•    Create a file for each line, using the name of the line as the filename.

•    Print the constructed lines in input order in the following format, ended with a newline. [line name]: [station name 1]<->[station name 2]<->…<->[station name N]

Examples:

 

Level 3 (30%): Search for a station

In this task, you will search for an input station from the given sub-network stored in text files     which are added to the VPL system . The format of the text files meets the requirements            described in Level 2. The contents of the text files are attached at the end of this worksheet for your reference.

1)    Read the given text files containing names of stations of several partial lines of the sub-network.

2)   Ask for the name of the station to be searched for.

3)    Print one of the following possible outcomes.

a)    If the input station cannot be found, print 'Station not found'.

b)   If the input station can be found on one of the lines, print a station found message and the name of the line.  If there are interchange stations on the line, print the names of the          interchange stations in alphabetical order right after the name of the line.

c)    If the input station can be found on more than one line, print a station found message and   the names of the lines in alphabetical order.  If there are interchange stations on the line,     print the names of the interchange stations in alphabetical order right after the name of the line.

Inputs:

•    Name of the station to be searched for

Outputs:

•    If the station cannot be found, print a ‘Station not found’ message, ended with a newline.

    If the station can be found, print a station found message ended with a newline, the

name(s) of the line(s) in alphabetical order, a list of name(s) of interchange station(s), if any, in alphabetical order in the following format, each line is ended with a newline.

[line name]: [interchange station 1 name], [interchange station 2 name], , [interchange station N name]

Note that the interchange stations are separated by a comma followed by a space.

Hint: you have learnt to use .sort() in the lectures and tutorials for sorting abstract data types like lists, tuples. If you have several sub-lists within a list, you may find it useful to sort the list based on a specific item in the sub-lists by defining a key in the method:

def sort_by_second_item (element):

return element[1]

a = [[1, 10, 1], [2, 40, 1], [3, 20, 2]]

a.sort(key=sort_by_second_item, reverse=False)

print(a) # a = [[1, 10, 1], [3, 20, 2], [2, 40, 1]]

Examples:

 

Level 4 (40%): List routes from an origin to a destination

In this task, you will list all the routes from an origin station to a destination station input by the user via at most one interchange station.

1)    Read the given text files containing names of stations of several partial lines of the sub- network.

2)   Ask for the names of the origin and destination stations.

3)    Print one of the following possible outcomes.

a)    If one or both stations cannot be found in the given sub-network, print 'Station(s) not found'.

b)   If there is more than one change or no route from the origin station to the destination station, print 'More than one change or no route found'.

c)    If both stations can be found on the same line and so no change is needed, print the name of the line.   If there is more than one such line, print the names of the lines in alphabetical order.

d)   If there is a route from the origin station to the destination station via one      interchange station, print the route.  If there is more than one route, print the routes in alphabetical order of the names of the interchange stations.

Note that the user should not be asked to change line unnecessarily if the origin and destination are on the same line.

Inputs:

•    Name of the origin station

•    Name of the destination station

Output:

•    If one or both stations cannot be found on the given sub-network, print a 'Station(s) not found' message, ended with a newline.

•    If there is more than one change or no route from the origin station to the         destination station, print a 'More than one change or no route found' message, ended with a newline.

•    If both stations can be found on the same line and so no change is needed, print the name of the line.  If there is more than one such line, print the names of the lines in alphabetical order.  Each line is ended with a newline.

•    If there is a route from the origin station to the destination station via one interchange station, print the route in the following format.

[line name] : [origin station name]->[interchange station name]         [line name] : [interchange station name]->[destination station name]

Note that each line is ended with a newline.  If there is more than one route, print the routes in alphabetical order of the names of the interchange stations.


Examples:


Important notes

•     Only Python libraries/features covered in this course should be used. Using other libraries,    non-built-in functions, and features is not allowed and you will be given zero mark. If you are unsure, feel free to contact us for clarification.

•     While programming style is not graded, you are highly recommended to use functions to factor your program, write clear and tidy codes, e.g., descriptive comments, concise and clear naming, appropriate spacing.

Submission

•     Virtual Programming Lab (VPL) will be set up for submission and testing. Only the last submission will be considered as the final submission.

•    Test your program thoroughly before the deadline. Your programs must generate output according to the given formats, i.e., without extra text/space.  If you are unsure about the requirements, feel free to contact us for clarification.

•    The test cases provided in VPLs only check whether your programs meet the basic                 requirements. You should test the robustness of your programs by creating more test cases on your own.  Your programs will be assessed with another set of private test cases.

•     20% will be deducted from the final mark for every 24 hours after the submission due date.

•     Do not submit any program after the due date if your work is final. Any submission after the due date is regarded as late submission.

Plagiarism

•     Confirmed plagiarism cases (detected by the system) will get zero mark and subject to          disciplinary actions. The penalty applies to the source provider(s) as well. In other words,     students who submit same/highly similar programs will all get zero mark. Students have full responsibility to protect their programs from being accessed by others.

•     Last year, 6 students had found engaged in plagiarism. They all got zero mark for the assignment and a warning letter issued by the Department Head.

Questions

•     If you have any questions, please send email Miss LI Jiaqi ([email protected]) who oversees this assignment.

Island Line.txt

HKU

Sai Ying Pun

Sheung Wan

Central

Admiralty

Wan Chai

Causeway Bay

Tsuen Wan Line.txt

Hung Hom

Mong Kok East

Kowloon Tong

Tai Wai

Sha Tin

Kwun Tong Line.txt

Austin

East Tsim Sha Tsui

Hung Hom

Ho Man Tin

To Kwa Wan

Sung Wong Toi

Kai Tak

Diamond Hill

Hin Keng

Tai Wai

Che Kung Temple