$29.99
CSIT110/CSIT810
Assignment 3 (10%)
Objectives
- Able to write clear code with comments and follow coding convention
- Able to use variables with meaningful names and correct data types
- Able to define functions and class objects
Question 1. Define a function that satisfies the following specifications.
Function name process_subscription
Parameters -
Return Value -
Detailed description With the use of a dictionary-typed variable, the function must display the
six plans available for subscription and take subscription orders. Your
program should work exactly as the following examples. The text in bold
indicates user input.
Lines 2 to 7 has two columns. The two columns are 39 characters and 7
characters in width.
There is a space after each colon in the prompts.
The bullets points before the list of selections is a space, a hyphen and a
space.
. Example 1: The user subscribes to 2 plans
Packages available for subscription(price/mth)
Standard Food Delivery Plan $13.40
Premium Food Delivery Plan $25.40
Basic Commute Plan $39.80
Premium Commute Plan $69.80
All Access Plan(Lite) $79.60
All Access Plan(Premium) $108.60
Subscribe to Standard Food Delivery Plan? (Y/N): Y
Subscribe to Premium Food Delivery Plan? (Y/N): N
Subscribe to Basic Commute Plan? (Y/N): N
Subscribe to Premium Commute Plan? (Y/N): N
Subscribe to All Access Plan(Lite)? (Y/N): Y
Subscribe to All Access Plan(Premium)? (Y/N): N
Your selection:
- Food Delivery Plan($21.40)
- All Access Plan(Lite)($9.56)
Total cost $30.96
Example 2: The user subscribes to 0 plan.
Packages available for subscription(price/mth)
Standard Food Delivery Plan $13.40
Premium Food Delivery Plan $25.40
Basic Commute Plan $39.80
Premium Commute Plan $69.80
All Access Plan(Lite) $79.60
All Access Plan(Premium) $108.60
Subscribe to Standard Food Delivery Plan? (Y/N): Y
Subscribe to Premium Food Delivery Plan? (Y/N): N
Subscribe to Basic Commute Plan? (Y/N): N
Subscribe to Premium Commute Plan? (Y/N): N
Subscribe to All Access Plan(Lite)? (Y/N): N
Subscribe to All Access Plan(Premium)? (Y/N): N
Your selection:
- None
Total cost $0.00
Important requirement:
Your program must use string addition to produce the exact output as illustrated in the above
example. You may assume the input are of letters n and y.
Question 2. Define a function that satisfies the following specifications.
Function generate_qns_from_list
Parameters 1. A list.
Each element of this list is a list of integers.
Return Value 1. A list of dictionary objects
Detail Information This function should convert the list parameter into a list of dictionaries.
Each dictionary will have two string-typed keys – "qns" and "ans".
The value for the "qns" key will be a str of the integers taken from each
list element in the list parameter. In the str, the integers are separated with
the characters " + ".
The value for the "ans" key will be the sumof all the numbers in the list.
You should skip the lists that contain less than 2 integers. There is no need
to print anything out.
Example:
input_list = [[1,3,3], [2,5,-1],[3,2],[4,5,3],[0,23],[1,2,3,4]]
generate_qns_from_list(input_list)
The function should return the following list
[{"qns": "1 + 3 + 3", "ans": 7},
{"qns": "2 + 5 + -1", "ans": 6},
{"qns": "3 + 2", "ans": 5},
{"qns": "4 + 5 + 3", "ans": 12},
{"qns": "0 + 23", "ans": 23},
{"qns": "1 + 2 + 3 + 4", "ans": 10}]
Question 3a.
Define a class that satisfy the following specifications.
Class name ShoppingCart
Class attribute server_url: str
Instance attributes account_id: str
cart: dict
Parameter account_id: str
Detailed information Assign the str "128.123.123.0" to the class attribute, server_url.
The value of the instance attribute, account_id, should be assigned
with the parameter of the same name.
The instance attribute, cart, is an empty dictionary when a
ShoppingCart object is instantiated.
The keys in the dictionary are the product names, of str types,
while the values, of int type, mark the quantity of the products in
the cart.
Question 3b
Write an instance method for the ShoppingCart class that satisfies the following
specifications.
Method name add_item_to_cart
Parameter 1. product_name: str
2. quantity: int
Return value -
Detailed description The function should check if the item exists in the keys of the
cart attribute and update the number of the items in the cart
accordingly.
That is, if the item does not exist in the cart, it should create a
key-value pair for the car. Otherwise, it should add the quantity in
the parameter to the existing value.
You may assume that the quantity in the parameter will be more
than 0.
Question 3c
Write an instance method for the ShoppingCart class that satisfies the following
specifications.
Method name remove_item_from_cart
Parameter 1. product_name: str
2. quantity: int
Return value -
Detailed description The function should update the cart attribute with the correct
number of product. If the updated quantity is 0, the key value pair
should be removed from the cart.
You can assume that the product is available for removal and the
quantity to be removed is less than or equal to the quantity in the
cart.
Question 3d
Write a class method for the ShoppingCart class that satisfies the following specifications.
Method name get_url
Parameter -
Return value 1. A str
Detailed description This method returns the class attribute server_url
Question 3e
Write an instance method for the ShoppingCart class that satisfies the following
specifications.
Method name empty_cart
Parameter -
Return value -
Detailed description This method empties the instance attribute, cart
Question 3f
Write an instance method for the ShoppingCart class that satisfies the following
specifications.
Method name count_items
Parameter -
Return value 1. int
Detailed description This method returns the total number of items based on the
existing items in the cart. If there is nothing in the cart, the return
value should be 0.
An example of how your code should work given the following parameters.
newCart = ShoppingCart("1234567")
newCart.add_item_to_cart("fruit juice", 2)
newCart.add_item_to_cart("tissue box", 4)
newCart.add_item_to_cart("ice cream", 3)
# newCart.cart is now {"fruit juice": 2, "tissue box": 4, "ice cream": 3}
newCart.remove_item_from_cart("tissue box",1)
newCart.remove_item_from_cart("fruit juice",2)
# newCart.cart is now {"tissue box": 3, "ice cream": 3}
newCart.count_items() # returns 6
print(newCart.get_url()) # prints 128.123.123.0
newCart.empty_cart()
newCart.count_items() # returns 0
Question 4
Part 1
The checksum letter of a magic 7 digits number is calculated as such:
D = [(i1 i2 i3 i4 i5 i6 i7) • (2 7 6 5 4 3 2 )] mod 11
= ( 2i1 + 7i2 + 6i3 + 5i4 + 4i5 + 3i6 + 2i7 ) mod 11
Where ix is the 1st to last of the 7 digits of the numbers and (2,7,6,5,4,3,2) are the weights.
The checksum is a letter which corresponds to the number d as shown in the look-up table below
Question 4a
Define a function that meets the following specifications.
Method name get_magic_num_checksum
Parameter 1. str
This str contains the numerical series mentioned in the description above.
Return value 1. str
An upper case letter
Detailed description
From the given string of 7 numbers, computer the number d as described in
the section 'Part 2'. Return the letter which corresponds to the number d as
shown in the look-up table below
Part 2
A typical vehicle registration number comes in the format xxx #### y:
• x – prefixes
• #### – Numerical series (from 1 to 9999, without leading zeroes)
• y – Checksum
• The checksum letter is calculated by converting the letters into numbers, i.e., where A=1 and
Z=26, potentially giving seven individual numbers from each registration plate. However, only
two letters of the prefix are used in the checksum. For a three-letter prefix, only the last two letters
are used; for a two-letter prefix, both letters are used; for a single letter prefix, the single letter
corresponds to the second position, with the first position as 0. For numerals less than four digits,
additional zeroes are added in front as placeholders, for example "1" is "0001". SBS 3229 would
therefore give 2, 19, 3, 2, 2 and 9 (note that "S" is discarded); E 12 would give 0, 5, 0, 0, 1 and 2.
SS 108 would be given as 19, 19, 0, 1, 0, 8.
• Each individual number is then multiplied by 6 fixed numbers (9, 4, 5, 4, 3, 2). These are added
up, then divided by 19. The remainder corresponds to one of the 19 letters used (A, Z, Y, X, U, T,
S, R, P, M, L, K, J, H, G, E, D, C, B), with "A" corresponding to a remainder of 0, "Z"
corresponding to 1, "Y" corresponding to 2 and so on. In the case of SBS 3229, the final letter
should be a P; for E 23, the final letter should be a H. SS 11 back letter should be a T. The letters
F, I, N, O, Q, V and W are not used as checksum letters.
Question 4b
Define a function that meets the following specifications.
Function name get_car_plate_checksum
Parameter 1. str
This str contains the prefixes and numerical series mentioned in the
description above.
Return value 1. str
An upper case letter
Detailed description
Compute and return the checksum from the string parameter. The computation
logic is described in the section titled 'Part 1'.
The checksum is one character in length. The return value is case insensitive.
You should use the try and except blocks to find out is a character in a string is
an integer or not. The input string may contain 1-3 letters for prefixes while
there can be 1 to 4 digits for the numerical series that follows.