COMP 1117-1A Computer Programming [Semester 1, 2025]
Assignment 2
Deadline: October 19, 23:59 p.m.
About the submission
- This assignment involves some console input/output. You are reminded that the VPL system on HKU Moodle evaluates your program with a full score under the condition that your program output is the exact match of the expected output. In other words, any additional or missing space character, tab character, newline character, etc. will be treated as errors during the evaluation of your program.
- Write your programs (Assignment 2 Question 1-2) in the corresponding Moodle VPLs under the Assignments section.
- We will grade your programs with another set of test cases (i.e., not limited to the sample test cases in the assignment sheet). Therefore, you are advised to make more test cases on your own for testing your program.
- Late submission will NOT be accepted.
- Plagiarism is prohibited. Please refer to the email sent by the department about plagiarism.
Definition of Plagiarism
As defined in the University's Regulations Governing Conduct at Examinations, plagiarism is "the unacknowledged use, as one's own, of work of another person, whether or not such work has been published.", or put it simply, plagiarism is copying (including paraphrasing) the work of another person (including an idea or argument) without proper acknowledgement.
In case of queries on plagiarism, students are strongly advised to refer to "What is Plagiarism?" at https://tl.hku.hk/plagiarism/.
- If a student commits plagiarism, with evidence after investigation, no matter
whether the student concerned admits or not, a penalty will be imposed:
First Attempt: if the student commits plagiarism (in an assignment/test of a CS course) for the first time in his/her entire course of study, the student shall be warned in writing and receive zero mark for the whole assignment or the whole test; if the student does not agree, s/he can appeal to the BEng(CompSc) Programme Director within a week;
Subsequent Attempt: if the student commits plagiarism more than once in higher course of study, the case shall be referred to the Programme Director for consideration. The Programme Director shall investigate the case and consider referring it to the University Disciplinary Committee, which may impose any of the following penalties: a published reprimand, suspension of study for a period of time, fine, or expulsion from the University.
- Both the student who copies other's work and the student who offers his/her work for copying shall be penalized.
Question 1: Tiered Electricity Bill Calculator (50%)
To encourage energy conservation, a city has implemented a tiered electricity pricing system. The rates are as follows:
• Tier 1: For monthly consumption up to and including 150 kWh, the rate is $0.50 per kWh.
• Tier 2: For the portion of monthly consumption from 151 kWh to 400 kWh, the rate is $0.80 per kWh.
• Tier 3: For the portion of monthly consumption exceeding 400 kWh, the rate is $1.20 per kWh.
Please write a program that takes the electricity consumption (in kWh) for several consecutive months as input and calculates the total electricity bill for that period.
Input Format
A single line of integers, representing the electricity consumption in kWh for each month, separated by spaces.
Output Format
A single floating-point number representing the total bill, formatted to two decimal places.
The test cases are as follows.
Sample inputs
|
Sample outputs
|
Explanation
|
100 200 500 400
|
835.00
|
Month 1 (100 kWh): 100 * 0.50 = 50.00
Month 2 (200 kWh): 150 * 0.50 + (200 - 150) *
0.80 = 75.00 + 40.00 = 115.00
Month 3 (500 kWh): 150 * 0.50 + (400 - 150) *
0.80 + (500 - 400) * 1.20 = 75.00 + 200.00 +
120.00 = 395.00
Month 4 (400 kWh): 150 * 0.50 + (400 - 150) *
0.80 = 75.00 + 200.00 = 275.00
Total Bill: 50.00 + 115.00 + 395.00 + 275.00 =
835.00
|
50 150 75
|
137.50
|
Month 1 (50 kWh): 50 * 0.50 = 25.00
Month 2 (150 kWh): 150 * 0.50 = 75.00
Month 3 (75 kWh): 75 * 0.50 = 37.50
Total Bill: 25.00 + 75.00 + 37.50 = 137.50
|
151 300
|
270.80
|
Month 1 (151 kWh): 150 * 0.50 + (151 - 150) *
0.80 = 75.00 + 0.80 = 75.80
Month 2 (300 kWh): 150 * 0.50 + (300 - 150) *
0.80 = 75.00 + 120.00 = 195.00
Total Bill: 75.80 + 195.00 = 270.80
|
401 600
|
791.20
|
Month 1 (401 kWh): 150 * 0.50 + 250 * 0.80 +
(401 - 400) * 1.20 =
75.00 + 200.00 + 1.20 = 276.20
Month 2 (600 kWh): 150 * 0.50 + 250 * 0.80 +
(600 - 400) * 1.20 =
75.00 + 200.00 + 240.00 = 515.00
Total Bill: 276.20 +
515.00 = 791.20
|
1000
|
995.00
|
Month 1 (1000 kWh):
150 * 0.50 + 250 * 0.80 + (1000 - 400) * 1.20 = 75.00 + 200.00 + 720.00 = 995.00
Total Bill: 995.00
|
Hint:
1. Reading the Input: Read the entire line and split it into a list of strings. You will need to convert each of these strings into a number to perform calculations.
2. Tiered Logic: Think about how to calculate the cost for a usage amount that spans multiple tiers. For example, for 200 kWh, you don't just calculate 200 * 0.80. You need to calculate the cost for the first 150 kWh at the Tier 1 rate, and then the cost for the remaining kWh (from 151 to 200) at the Tier 2 rate.
Note:
1. Floating-Point Numbers: The electricity rates are decimal numbers, so your fee calculations will result in floating-point numbers (float).
2. Edge Cases: Consider how your code would handle edge cases. Your loop should handle both cases correctly.
3. Output Formatting: The output bill should be rounded with 2-digit decimal. You can use print(f"{var:.2f}") to format a floating point number var.
Question 2: Autokey Cipher (50%)
Cryptography is the fundamental discipline of information security. In this question you are expected to implement encoding and decoding of Autokey Cipher.
Autokey Cipher is an example of a polyalphabetic substitution cipher. To use the Autokey Cipher to encrypt a message, a coder first chooses a keyword to use.
Then the key corresponds to the keyword concatenated with the plaintext. Each letter of the key will tell what cipher(which row) to use for each letter of the message to be coded. The cipher alphabet on the second row uses B for A and C for B, etc. That is cipher alphabet ‘ B,. Each cipher alphabet is named by the first letter in it. For example, if the keyword is LEMON and the message to encode is ATTACKATDAWN, then the encoding is
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
Vigenère square
Plaintext
|
ATTACKATDAWN
|
Key
|
LEMONATTACKA
|
Ciphertext
|
LXFOPKTMDQGN
|
For example, for the first letter ‘A,, its corresponding key is ‘ L,, so the ciphertext should be row L column A in the Vigenère square, that is L. For the second letter in ciphertext, that is X, which corresponds to row E column T in the square. If the key is shorter than the plaintext, then use the plaintext sequentially after the keyword until the plaintext reaches the end.
The program first asks for:
- A number on the first line(either 1 for encoding, or 2 for decoding).
- The plaintext/ciphertext on the second line.
- The keyword on the third line.
The test cases are as follows.
Input
|
Output
|
Explanation
|
1
ATTACKATDAWN
|
LXFOPKTMDCGN
|
Encryption process
|
LEMON
|
|
|
2
LXFOPKTMDCGN
LEMON
|
ATTACKATDAWN
|
Decryption of
“ LXFOPKTMDCGN ” should give the
original text
“ATTACKATDAWN ”
|
1
PYTHONPROGRAMMING PINEAPPLE
|
EGGLOCECSVPTTAVCX
|
|
2
EGGLOCECSVPTTAVCX PINEAPPLE
|
PYTHONPROGRAMMING
|
|
Hint:
- It is guaranteed that the input plaintexts, keywords and ciphers only contain capital letters, so you only need to handle the case for capital letter texts, ciphers and keys.
- You can use ord() function to convert a character to its ASCII value. For example, ord(‘A9) gives you the number 65, which is the ASCII value of A.
- You can use chr() function to convert an ASCII value to its corresponding character. For example, chr(65) gives you the character A.
- It is convenient to use modulo operation to index elements in a list repeatedly. In python we use % to perform. this operation. For example, 15 % 4 returns 3.
- You can create the Vigenère square for encoding and decoding. Since the table is regular, you can also use mathematical operation to find the corresponding character in this table without explicitly creating it.
Note:
Ascii values for English letters are continuous, you can use this property to index the Vigenère square.