Reference no: EM132540464
Question 1- Translating Text Messages
Instant messaging (IM) and texting on portable devices has resulted in a set of common abbreviations useful for brief messages. However, some individuals may not understand these abbreviations.
Write a program that reads a one-line text message containing common ab- breviations and translates the message into English using a set of translations stored in a file. For example, if the user enters the text message
y r u 18
the program should print
why are you late
As a simplification, you can assume that are no punctuation marks. Proceed as follows:
a. Build a dictionary with abbreviations as keys and associated texts as values.
Read the provided text file abbreviations.txt, each line of which contains an abbreviation and the associated text it.
The following code segment reads the file and store each line of the text file as a string in the list lines:
filename = "abbreviations.txt" infile = open(filename, "r") lines = file.readlines()
(You should display the list lines to see what a string of this list looks like.)
Complete the code to build a dictionary with abbreviations as keys and the associated texts as value.
b. Translate a message.
Split the message into "words". If a "word" is in the dictionary, replace it by the associated text. Otherwise simply copy the "word" to the translation.
Question 2- Merge Big Files
Suppose we have two very big text files, so big that we cannot read them into the computer's memory. The files are already sorted.
Now, we want to merge them. How can we do this?
We can use the following approach. Imagine that the names are on two piles of cards.
• We look at the cards on top of the two piles.
• We then pick out the smaller one to add to the merged pile. If the two top cards have the same name, we add one card to the merged pile and discard the other one.
• We repeat this until one pile is exhausted. We simply add all the cards of the other pile, if any, to the merged pile.
Write a program to implement the above approach. For demonstration pur- pose, you can take names set1.txt and names set2.txt as the two input files. Call the output file names merged big.txt.
Note: The merge problem we consider here is part of the algorithm known as external merge sort, which is used to sort very big files, without reading the whole files into computer memory.
Question 3 - The 72 Rule
The program below gets the annual interest rate from the user. It then calcu- lates and displays the estimate of number of years it takes to double the initial investment amount.
# Get annual interest rate
rate = float(input("Enter annual interest rate (5 for 5 ):"))
# Calculate and display number of of years it takes to double # the investment account
years = 72 / rate
print("at the rate of" , rate, " , it takes", years, "to double the initial investment")
The above program does not regard a negative interest rate as an error. From the business application viewpoint, a negative interest rate should be regarded as an error.
Modify the program to take this issue into account.
Handle any exception that may be raised. You can do this in any way you like. But the output should distinguish (i) the case in which is the input is not a number from (ii) the case in which the input is not a positive number.
Question 4 - The Student Class
a. Define a class named Student that holds the following data about a student in attributes id, name, and marks.
Attribute marks is a list that holds the marks of three assessment compo- nents, assignment 1, assignment 2 and the exam, with the weights of 30%, 30% and 40%, respectively.
Include the following:
• A constructor to initialize the student's id, name and marks.
• A method to display the student's details for inspection purpose,
• A method to change a particular mark (the mark to be changed is specified by its index in the list),
• A method to calculate the final mark.
b. Write statements to
• Create a student
• Display all of the student's details
• Display the student's id, name and marks on three separate lines (one line for id, one line for name and one line for the list of marks).
• Change the mark of one assessment component, and display the stu- dent's marks after the change.
• Get and display the final mark.
In your testing, observe the following usage-conforming rules:
• The student id is a non-empty string
• The student name is a non-empty string
• marks is a list of three float values between 0 and 100, inclusive State your assumptions, if any.
Question 5 - Classes Payment and Subclasses
a. Define class Payment that has an attribute amount (intended to be of type
float) that stores a payment amount.
In addition to init and repr , include method printDetails to print on the screen the payment details as shown in the example below:
PAYMENT
Amount: 400
b. Define class CashPayment as a subclass of Payment.
This class redefines printDetails to display the payment details as shown in the example below:
PAYMENT
Amount: 400 By cash
c. Define class CreditCardPayment as a subclass of Payment. This class has attribute creditCardNr for the credit card number.
This class redefines printDetails to display the payment details as shown in the example below:
PAYMENT
Amount: 400
By credit card: 12345678
d. A list of payments is created as shown below:
p1 = CashPayment(100)
p2 = CreditCardPayment(200, "AB123") p3 = CashPayment(200)
p4 = CreditCardPayment(100, "CD456") payments = [p1, p2, p3, p4]
Write statements to calculate and display the total payment amount of all the payments by credit cards.
Question 6 - Reading Bus Route Data
In this question and next two questions, you will work with a text file that contains information about the bus route that goes from the Bundoora campus of La Trobe University to the City of Melbourne.
The data (which is route 250) is stored in the text file BusRoute250.txt. It lists all the bus stops on the route, 67 of them actually. Details about a stop is stored on one line. The first 5 lines and the last 5 lines are shown below:
1/La Trobe Uni Terminus/Science Dr/Bundoora
2/La Trobe Uni Thomas Cherry Building/Science Dr/Bundoora 3/Kingsbury Dr/Waterdale Rd/Bundoora
4/Crissane Rd/Waterdale Rd/Heidelberg West 5/Percy St/Waterdale Rd/Heidelberg West
...
63/Exhibition St/Lonsdale St/Melbourne City 64/Melbourne Central/Lonsdale St/Melbourne City 65/Elizabeth St/Lonsdale St/Melbourne City 66/Little Bourke St/Queen St/Melbourne City 67/Little Collins St/Queen St/Melbourne City
Each line has four fields, separated by slash characters:
• The first is the stop number (an integer), counting from the La Trobe Uni- versity end.
• The second is the stop's name. You may be interested in how bus stops are named. In general, the name of a bus stop can be
- Either the name of a nearby street that cuts across the route (which will be referred to as the "cross-street"), e.g. Kingsbury Dr
- Or the name of a place of interest near the stop (which will be referred to as the "cross-site"), e.g. La Trobe Uni Thomas Cherry Building
• The third is the street along the route on which the stop lies, e.g. Science Dr for stop number 1.
• The fourth field is the name of the suburb of the stop. It is enclosed between a pair of brackets, e.g. Bundoora.
Write a program to read the file and display the details about the bus stops. The details of a bus stop is displayed on four lines, showing the bus stop number, the name, the street and the suburb. For example:
1
La Trobe Uni Terminus Science Dr
Bundoora
...
Question 7 - Defining the BusStop Class
Define the BusStop class that has the following attributes
• number, which is intended to be a positive integer
• name: the name of the bus stop, a non-empty string (note that, as you can tell from the file, they are also unique)
• street: the name of the street along the route
• suburb: the name of the suburb
Question 8 - Loading and Querying about the Bus Route
In this question, you write a number of functions to load the data into a list of bus stops (i.e. BusStop instances) and make various queries about the bus stops on the route.
Complete the program shown below:
from A2Q7 import BusStop busStopList = []
def loadData(): pass
def listAllBusStopNames(): pass
def listAllStreetsOnRoute(): pass
def listAllSuburbsOnRoute(): pass
def searchByNumber(start: "int, value: a stop number", end: "int, value: a stop number >= start"):
pass
def searchByName(nameKey): pass
def searchByStreet(streetKey): pass
def searchBySuburb(suburbKey): pass
def searchByAnyField(key): pass
#== Tests ==
# Add tests to test each of the functions
Attachment:- Translating Text Messages.rar