CDS523 Principle of Data Analytics

人生苦短6發表於2024-11-18

CDS523 Principle of Data Analytics and Programming

Programming Assignment 3

(Due: 20 th November 2024, 23:59)

  1. (5 marks) In the past, the frequency analysis of letters was an important step in breaking cipher. Write aPython program that prompt the user to input the path of a text file and output the occurrence count of

all 26 English alphabet (ignoring case) in the text file specified by the user.Sample input and output:TerminalPlease enter the path of the text file: asg3_q1_sample.txtThe frequency of English alphabet:a: 1b: 1c: 1…z: 1You should output the count of every English alphabet one byone in alphabetical order. Each line shouldcontain an Englishalphabet followed by its count. The input and output are omitted to save space in this assignment specification but your program shouldoutput the count of all English alphabets.

15 marks in total) In cryptography, a Caesar cipher is one of the simplest forms of substitution cipher.The encryption is performed through shifting each letter in the plaintext forward by a number of positions defined by the user along the alphabetical order. For example, if the user has defined the numberof forward position shift to be 4, every letter E in the plaintext will be replaced by A in the ciphertextbecause character A is 4 positions in front of letter E in alphabetical order.Consider the following mapping between each alphabet in plaintext and its corresponding alphabet in

ciphertext where the encryption is performed by shifting 4 alphabets forward.PlainA B C D E F G H IJ K L M N O P Q R S T U V W X Y ZCipher W X Y Z A B C D E F G H IJ K L M N O P Q R S T U VProcess of Encryption:Input plaintext:The quick brown fox jumps over the lazy dog.

Encryption ProcessPlain T h e q u i c k b r o w n f o x j u m p s o v e r t h e l a z y d o g .↓↓↓ ↓↓↓↓↓ ↓↓↓↓↓ ↓↓↓ ↓↓↓↓↓ ↓↓↓↓ ↓↓↓ ↓↓↓↓ ↓↓↓Cipher P d a m q e y g x n k s j b k t f q i l ok r a n p d a h w v u z k c .Output ciphertext:Pda mqeyg xnksj bkt fqilo kran pda hwvu zkc.Process of Decryption:The decryption of a ciphertext encrypted with Caesar cipher can be achieved by reversing the encryptionprocess (i.e. shifting the letters backward).Input ciphertext:Pdamqeyg xnksj bkt fqilo kran pda hwvu zkc.Decryption ProcessCipher P d a m q e y g x n k s j b k t f q i l o k r a n p d a h w v u z k c .↓↓↓ ↓↓↓↓↓↓↓↓↓↓ ↓↓↓ ↓↓↓↓↓ ↓↓↓↓ ↓↓↓ ↓↓↓↓ ↓↓↓Plain T h e q u i c k b r o w n f o x j u m p s o v e r t h e l a z y d o g .Output plaintext:The quick brown fox jumps over the lazy dog.a) (6 marks) Write a program that prompt the user to input (1) the number of forward position shift an(2) the plaintext message that they代寫CDS523 Principle of Data Analytics want to encrypt into the console. The program then output the encrypted message to the console.Sample input andoutput:TerminalPlease enter the number of position shift: 4Please enter the plaintext message: The quick brown foxjumps over thelazy dog.The encrypted message is: Pda mqeyg xnksjbkt fqilo kran pda hwvu zkc.

) (5 marks) Modify the program in (a) such that it will prompt the user to input (1) the number of forward position shift, (2) the path of the plaintext text file and (3) the path of the ciphertext text file.The program then encrypts the content of the plaintext text file and output the result ciphertext to the ciphertext text file.Sample input and output:

encrypted.txt

  1. c) (2 marks) Modify the program in (a) such that it will prompt the user to input (1) the number of forward position shift and (2) the encrypted message that they want to decrypt into the console. Theprogram then output the decrypted plaintext message to the console.Sample input and output:TerminalPlease enter the number of position shift: 4

enter the encrypted message: Pda mqeyg xnksj bkt fqilo kran pdahwvu zkc.The decrypted message is: The quick brown fox jumps over the lazy dog.

  1. d) (2 marks) Modify the program in (c) such that it will prompt the user to input (1) the number of forward position for shifting, (2) the path of the ciphertext text file and (3) the path of the decrypted plaintext text file. The program then decrypts the content of thciphertext text file and output the result plaintext to the plaintext text file.Sample input and output:Terminal enter the number of position shift: 4Please enter the path of the input encrypted file: encrypted.txtPlease enter the path of the output decrypted file: decrypted.txtAssumption:
  • Only English alphabet (upper case or lower case) characters have to be encrypted and decrypted.
  • Characters other than English alphabet are preserve during the encryption and decryption.
  • Case of every English alphabet is preserved during the encryption and decryption process.

Assumptions You may assume that every input of the program is valid in format.

Submission Students should submit their source code as (1) a single Jupiter Notebook file (i.e. .ipynb file) OR (2) a zip filethat contains standalone Python script files (i.e. .py files) for answering the programing questions to thesubmission box on the Moodle elearning platform on or before 20 th November 2024, 23:59. Students areexpected to name their file submission in the name of <your_student_ID>_asg3.ipynb OR <your_student_ID>_asg3.zip and their source code should follow the following format:

相關文章