COSC2429 - Intro To Programming Assessment 2 - Sem A 2021: RMIT Classification: Trusted
COSC2429 - Intro To Programming Assessment 2 - Sem A 2021: RMIT Classification: Trusted
Please follow the instruction carefully. Please copy the header from the Canvas site and put it at
the top of your py file.
- Please write correct docstrings and code comment.
- There are a total of 4 problems. You must put all the code for all the problems below in 1
py file with your student id (e.g., s12345678.py) and upload it to Assessment 2 on Canvas
once done. DO NOT ZIP FILE PLEASE!
- Do not include test of your function, and NO USER INPUT PLEASE!
Problem 1:
Write a function least_o(lst) in Python that takes one parameter, a list of strings, as input and
returns the string in the list that has the fewest total occurrences of the lowercase letter 'o' (note
that this may be zero occurrences). Uppercase ‘O’s should not be counted
You may assume that no two strings in the list have the same total number of 'o', that there is at
least one string in the list, and that all elements of the list are valid strings.
For example: If you receive the list [‘tooo’, ‘ho’, ‘nO’, ‘loo’], you must return ‘ho’.
Function format:
def least_o(lst):
# your logic here
return s
Note: You can write it either as an iterative or recursive function.
Problem 2:
Write a function the take in a str and an int and return the reverse str split by the int number “almost
evenly”. Some examples:
- If you have a str “12345678” and an int 3, you split it into 3 substrings of 3, 3, and 2
characters. Your return must be “32165487”.
- If you have a str “1234567890123456789” and an int 4, you split it into 4 substrings of 5,
5, 5 and 4 characters. Your return must be “5432109876543219876”.
- If the int to split by is larger than then length of the str, you have to reverse the whole str.
Function format:
def reverse(s, i):
# your logic here
return new_s
RMIT Classification: Trusted
Problem 3:
Write a function to calculate the shipping fee on an ecommerce website. The function will take in
a list and a dict, for example:
shopping_list = [‘apple’, ‘orange’, ‘kiwi’, ‘orange’, ‘apple’, ‘apple’]
location_dict = {
‘apple’: [1,3,4,7],
‘orange’: [2,4,5,7],
‘kiwi’: [1,3,5,6]
}
The shopping_list includes all items the customer put in his/her cart. There can be multiple
occurrences of an item, so you have to sum them up. The location_dict includes the items as keys
of the dict, while the dict values are list of warehouse locations that item is in (int numbers). If
there are items that can be dispatched from the same warehouse, you must combine them to save
the shipping fee for the customer. If they are from different warehouses, you must calculate them
as 2 shipments. The shipping fee will be 20,000VND per shipment. Return the final total shipping
fee and total number of shipments.
Function format:
def shipping_fee(shopping_list, location_dict):
# your logic here
return shipping_fee, number_of_shipments
Problem 4:
A chemical research lab wants to send some secret data to another lab. To avoid the information
being stolen. They encode it using the periodic table chart:
RMIT Classification: Trusted
IMPORTANT: There can be multiple versions on this table online. Please use this one only.
Function format:
def encrypt(s):
# your logic here
return encrypted_s
def decrypt(encrypted_s):
# your logic here
return s
NOTE: beware of case sensitive