· 6 years ago · Jan 21, 2020, 03:12 PM
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "<h1>CMPUT 175 - Lab 2: Caesar Cipher </h1>\n",
8 "<p><b>Goal: </b> Review file I/O, basic input data validation, and defining functions that return values.\n",
9 "Learn how to include docstrings for each function you write. Learn about another encryption method, and more about the Unicode table. </p>\n",
10 "<p>Useful functions: chr(), ord()</p>\n",
11 "<p>Look up details about how to use chr() and ord() in the Python 3 online documentation:\n",
12 "<a href='https://docs.python.org/3/index.html'>https://docs.python.org/3/index.html.</a> </p>\n",
13 "\n",
14 "<p>Look up the Unicode table: <a href='https://unicode-table.com/en/'>https://unicode-table.com/en/</a> </p>\n",
15 "\n",
16 "<p>Look up examples of Python docstrings: <a href='https://www.geeksforgeeks.org/python-docstrings/'>https://www.geeksforgeeks.org/python-docstrings/ </a> </p>"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "<h2>Background Information</h2>\n",
24 "<p>The Caesar cipher is a simple encryption method that works by substituting each letter in a word\n",
25 "(or message) with a letter that is a specific number of letters ahead of it in the alphabet. That\n",
26 "specific number is called the <b>cipher key</b>, and both the encrypter and decrypter must know the\n",
27 "value of the cipher key in order to successfully exchange secret (encoded) messages.</p>\n",
28 "\n",
29 "<p><b>Example:</b> If the cipher key is 5, then the letter ‘a’ in a clear message will be encoded as the letter\n",
30 "‘f’ in the encrypted message. Using the same cipher key, the letter ‘v’ in a clear message will be encoded as the letter ‘a’ (because we wrap around to the beginning of the alphabet when we reach the end).</p>\n",
31 "\n",
32 "<p>In order to decrypt a message encoded with a Caesar cipher, simply perform the encryption process in reverse.</p>\n",
33 " \n",
34 "<p><b>Example:</b> If the cipher key is 5, then the letter ‘a’ in the encoded message becomes the letter ‘v’ in the decoded message.</p>\n",
35 " \n",
36 "<p>For more details about the Caesar cipher, please watch the optional online video <a href='https://www.youtube.com/watch?v=mEFko9nf5UU'>“Encryption & Public Keys Explained”</a>\n",
37 "</p>"
38 ]
39 },
40 {
41 "cell_type": "markdown",
42 "metadata": {},
43 "source": [
44 "<h2>Problem</h2>\n",
45 "<p>In this lab, you will create a new Python program (lab2.py). In that program, you will ask the\n",
46 "user for the name of a text file. You should check that this file ends with a “.txt” extension. If it\n",
47 "does not, print an error message on the screen and continue to ask the user for the name of a text\n",
48 "file until a valid one is provided. This is the only validation you need to do for the file name –\n",
49 "you can assume that the .txt file exists in the same directory as your Python file. This should all\n",
50 "be done in a function called getInputFile(), which returns the valid name of the text file as a\n",
51 "string. </p>\n",
52 "<p>When provided with a valid text file name, you can assume that the file will contain exactly two\n",
53 "lines. The first line will consist of a positive integer: this is the cipher key. The second line will\n",
54 "consist of a series of encrypted words, where each word is encoded using the Caesar cipher\n",
55 "described above. You are tasked with decrypting the message, and printing the clear message\n",
56 "(all in lower case letters) to the screen. For example, if you have a cipher key of 1, the encrypted\n",
57 "message IfmmP XpsMe becomes hello world. This should all be done in a function called\n",
58 "decrypt(filename). </p>"
59 ]
60 },
61 {
62 "cell_type": "markdown",
63 "metadata": {},
64 "source": [
65 "1. Write a function named *getInputFile()* that ask the user to input a file name ending on '.txt'.\n",
66 " - If the name does not end in .txt, show an error and ask the user to input a valid filename\n",
67 " - You can assume that the .txt file exists in the same directory as your Python file."
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": 2,
73 "metadata": {},
74 "outputs": [],
75 "source": [
76 "def getInputFile():\n",
77 " # TODO: WRITE YOUR ANSQUER FOR QUESTION 1 HERE\n",
78 " pass"
79 ]
80 },
81 {
82 "cell_type": "markdown",
83 "metadata": {},
84 "source": [
85 "2. Write a function named *decrypt(filename)* that receives a filename and returns the decrypted message **all lowercase** on the screen.\n",
86 " - Assume that the file contain two lines: the first contains an integer, which is the cypher; the second contains a message encrypted using the Caesar Cypher, described above.\n",
87 " - **Example:** File contains: cypher 1, and the encrypted message is: 'IfmmP XpsMe'. The correct output is 'hello world'. "
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 1,
93 "metadata": {},
94 "outputs": [],
95 "source": [
96 "def decrypt(filename):\n",
97 " # TODO: WRITE YOUR ANSQUER FOR QUESTION 2 HERE \n",
98 " pass"
99 ]
100 },
101 {
102 "cell_type": "markdown",
103 "metadata": {},
104 "source": [
105 "### Things to keep in mind:\n",
106 "1. Both lines in the text file may or may not have leading and/or trailing whitespace that you should strip away before processing the data.\n",
107 "2. There are an unknown number of encrypted words in the text file, and these words are separated by inconsistent whitespace (e.g. tabs, single space, multiple spaces). Your decrypted message should separate each word by a single space. You can include a single space after the last word in your decrypted message.\n",
108 "3. You can assume that the encrypted words will only contain letters (i.e. no numbers, punctuation, or symbols), but these letters could be uppercase or lowercase. Your decrypted message should only contain lowercase letters.\n",
109 "4. The cipher key can be greater than 26.\n",
110 "5. Letters should wrap around, so that if you have a cipher key of 1, the encrypted message **qjaab** becomes **pizza**. "
111 ]
112 },
113 {
114 "cell_type": "markdown",
115 "metadata": {},
116 "source": [
117 "<h3>Be sure to include a docstring at the top of each of your function definitions.</h3> \n",
118 "<p>In Python,\n",
119 "docstrings are located on the line directly after def function_name(param1, …): and is\n",
120 "indented one level. The docstring may be one line or many lines, but must be enclosed by triple\n",
121 "quotes (\"\"\"). Each docstring should describe the purpose of the function, the function\n",
122 "parameters, and what the function returns. Test your docstrings by calling\n",
123 "help(getInputFile) and help(decrypt) in your main function.</p>"
124 ]
125 },
126 {
127 "cell_type": "markdown",
128 "metadata": {},
129 "source": [
130 "3. Download the files called **secretMessage1.txt** and **secretMessage2.txt** from eClass, and save it in the same directory as your solution file. When you test your code using these files, your output should match what is shown in the above sample runs. However, the sample runs only test some of the functionality of your *getInputFile()* and *decrypt(filename)* functions. Once you are satisfied that your program meets these minimum requirements, modify these files or create your own text files to test all 5 points under “things to keep in mind.” Try to be efficient in your testing (i.e. don’t test the same thing multiple times unless you're testing it in a different way), and consider any \"edge\" cases. \n"
131 ]
132 },
133 {
134 "cell_type": "code",
135 "execution_count": 3,
136 "metadata": {},
137 "outputs": [],
138 "source": [
139 "def testCases():\n",
140 " # TODO: WRITE YOUR ANSQUER FOR QUESTION 3 HERE \n",
141 " pass"
142 ]
143 },
144 {
145 "cell_type": "markdown",
146 "metadata": {},
147 "source": [
148 "## Challenge Problem (Optional)\n",
149 "\n",
150 "1. Write an encrypt() function\n",
151 " - Ask the user for the name of a text file – this file may or may not exist yet, but it must have a \".txt\" extension.\n",
152 " - If the file already exists, the contents of it will be overwritten during the encryption; \n",
153 " - otherwise, a new file will be created.\n",
154 " - The user should be asked to enter a message that s/he would like to be encrypted – this message may consist of multiple words, but you can assume that it only consists of letters and spaces (i.e. no numbers, punctuation, or special characters).\n",
155 " - Ask the user for a cypher key that should be an integer\n",
156 " - Validate the user input\n",
157 " - If it is not a whole number, an error message should be printed, and the user should be re-prompted to enter a valid integer cipher key. \n",
158 " - encrypt() should encode the entered message using the Caesar cipher method and the entered cipher key. \n",
159 " - The cipher key should be printed to line 1 of the text file, and the encoded message should be printed in all CAPITAL letters to line 2 of the text file."
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "def encrypt():\n",
169 " # TODO: WRITE YOUR ANSQUER FOR QUESTION 1 HERE\n",
170 " pass"
171 ]
172 },
173 {
174 "cell_type": "markdown",
175 "metadata": {},
176 "source": [
177 "### *Hints:*\n",
178 "- lookup string methods isalpha(), isalnum(), isdecimal(), isdigit(), and isnumeric() – can you use any of those to help?\n",
179 "- You may want to create additional functions to support your encrypt function.\n",
180 "- You can also rename/modify functions that you created in the non-optional decryption part of this lab, if appropriate – just be sure to test any changes you make to ensure that your decryption still works. "
181 ]
182 }
183 ],
184 "metadata": {
185 "kernelspec": {
186 "display_name": "Python 3",
187 "language": "python",
188 "name": "python3"
189 },
190 "language_info": {
191 "codemirror_mode": {
192 "name": "ipython",
193 "version": 3
194 },
195 "file_extension": ".py",
196 "mimetype": "text/x-python",
197 "name": "python",
198 "nbconvert_exporter": "python",
199 "pygments_lexer": "ipython3",
200 "version": "3.7.6"
201 }
202 },
203 "nbformat": 4,
204 "nbformat_minor": 4
205}