Skip to content

Commit 3d71c2d

Browse files
author
Kristian Rother
committed
add cypher exercise
1 parent 5a9844a commit 3d71c2d

File tree

5 files changed

+134
-90
lines changed

5 files changed

+134
-90
lines changed

data_structures/strings.md

Lines changed: 0 additions & 88 deletions
This file was deleted.

first_steps/cypher.rst

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
Cesar Cypher
2+
============
3+
4+
.. image:: enigma.jpg
5+
6+
Photo by `Christian Lendl on unsplash.com <https://unsplash.com/@dchris?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash>`__
7+
8+
In this chapter you will:
9+
-------------------------
10+
11+
======= ====================================
12+
area topic
13+
======= ====================================
14+
🚀 encrypt and decrypt text
15+
⚙ index the positions of a string
16+
⚙ loop over a string
17+
💡 use the ``find`` method of the ``string`` data type
18+
🔀 build a longer string by concatenation
19+
🔀 use the same index for two strings
20+
🐞 fix index errors
21+
======= ====================================
22+
23+
24+
Exercise 1: Cesar Cypher
25+
------------------------
26+
27+
Execute the following code calculating a **Cesar Cypher**:
28+
29+
.. code:: python3
30+
31+
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
32+
encrypted = "DEFGHIJKLMNOPQRSTUVWXYZABC "
33+
34+
for i in range(26):
35+
print(plain[i], " -> ", encrypted[i])
36+
37+
Explain what the code does.
38+
39+
40+
Exercise 2: String methods
41+
--------------------------
42+
43+
Find out what the expressions do to the string in the middle.
44+
45+
.. figure:: strings.png
46+
47+
Exercise 3: Loop over a string
48+
------------------------------
49+
50+
The following program should print the position in the alphabet of every character.
51+
Complete program by inserting ``char``, ``message``, ``plain`` and ``position``:
52+
53+
.. code:: python3
54+
55+
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
56+
message = "MY SECRET MESSAGE"
57+
58+
for char in ___:
59+
position = ___.find(___)
60+
print(char, "is in position", ___)
61+
62+
Why is the space at the end of ``plain`` important?
63+
64+
Exercise 4: String concatenation
65+
--------------------------------
66+
67+
Explain the following code:
68+
69+
.. code:: python3
70+
71+
encrypted = "DEFGHIJKLMNOPQRSTUVWXYZABC "
72+
73+
s = ""
74+
s += encrypted[4]
75+
s += encrypted[1]
76+
s += encrypted[8]
77+
s += encrypted[8]
78+
s += encrypted[11]
79+
print(s)
80+
81+
Exercise 5: Encryption
82+
----------------------
83+
84+
Write a program that:
85+
86+
1. defines a plain and encrypted alphabet as a 26-character string
87+
2. reads a message from the keyboard
88+
3. defines an empty result string
89+
3. goes through each character of the message
90+
4. finds the position in the plain text alphabet
91+
5. looks up that position in the encrypted alphabet
92+
6. adds the encrypted character to the result string
93+
7. when all characters have been processed, output the result
94+
95+
.. hint::
96+
97+
After each step, you should be able to run the program
98+
and see what it already does.
99+
100+
Exercise 6
101+
----------
102+
103+
Explain why the following code does the same as in exercise 1:
104+
105+
.. code:: python3
106+
107+
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
108+
for i in range(26):
109+
print(plain[i], " -> ", plain[(i + 3) % 26])
110+
111+
Can you use it to make your encryption program shorter?
112+
113+
.. hint::
114+
115+
If you are not sure what happens, print the value of ``i`` inside the loop.
116+
117+
Exercise 7
118+
----------
119+
120+
Also write a program for **decryption**.
121+
122+
.. hint::
123+
124+
How could you make sure that the input string is in upper case?
125+
126+
Reflection questions
127+
--------------------
128+
129+
- what happens when a string is followed by square brackets?
130+
- describe two ways to loop over the characters of a string?
131+
- what does the ``str.find()`` method do?
132+
- how can you add characters to a string?

first_steps/enigma.jpg

153 KB
Loading
File renamed without changes.

index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ First Steps
3535
first_steps/for.rst
3636
first_steps/guess_the_number.rst
3737
first_steps/slideshow.rst
38+
data_structures/cypher.rst
3839

3940
--------------
4041

@@ -44,10 +45,8 @@ Data Structures
4445
.. toctree::
4546
:maxdepth: 1
4647

47-
data_structures/strings.md
4848
data_structures/lists.md
4949
data_structures/dictionaries.md
50-
data_structures/tables.md
5150

5251
--------------
5352

@@ -57,6 +56,7 @@ Tabular Data
5756
.. toctree::
5857
:maxdepth: 1
5958

59+
data_structures/tables.md
6060
tabular_data/readfile.md
6161
tabular_data/parsing.md
6262
tabular_data/writing_files.md

0 commit comments

Comments
 (0)