代做COMP 1117-1A Computer Programming [Semester 1, 2025] Assignment 2代做留学生Python程序

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.



热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图