Skip to content

Commit

Permalink
add cypher exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Rother committed Feb 10, 2024
1 parent 5a9844a commit 3d71c2d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 90 deletions.
88 changes: 0 additions & 88 deletions data_structures/strings.md

This file was deleted.

132 changes: 132 additions & 0 deletions first_steps/cypher.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
Cesar Cypher
============

.. image:: enigma.jpg

Photo by `Christian Lendl on unsplash.com <https://unsplash.com/@dchris?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash>`__

In this chapter you will:
-------------------------

======= ====================================
area topic
======= ====================================
🚀 encrypt and decrypt text
⚙ index the positions of a string
⚙ loop over a string
💡 use the ``find`` method of the ``string`` data type
🔀 build a longer string by concatenation
🔀 use the same index for two strings
🐞 fix index errors
======= ====================================


Exercise 1: Cesar Cypher
------------------------

Execute the following code calculating a **Cesar Cypher**:

.. code:: python3
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = "DEFGHIJKLMNOPQRSTUVWXYZABC "
for i in range(26):
print(plain[i], " -> ", encrypted[i])
Explain what the code does.


Exercise 2: String methods
--------------------------

Find out what the expressions do to the string in the middle.

.. figure:: strings.png

Exercise 3: Loop over a string
------------------------------

The following program should print the position in the alphabet of every character.
Complete program by inserting ``char``, ``message``, ``plain`` and ``position``:

.. code:: python3
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
message = "MY SECRET MESSAGE"
for char in ___:
position = ___.find(___)
print(char, "is in position", ___)
Why is the space at the end of ``plain`` important?

Exercise 4: String concatenation
--------------------------------

Explain the following code:

.. code:: python3
encrypted = "DEFGHIJKLMNOPQRSTUVWXYZABC "
s = ""
s += encrypted[4]
s += encrypted[1]
s += encrypted[8]
s += encrypted[8]
s += encrypted[11]
print(s)
Exercise 5: Encryption
----------------------

Write a program that:

1. defines a plain and encrypted alphabet as a 26-character string
2. reads a message from the keyboard
3. defines an empty result string
3. goes through each character of the message
4. finds the position in the plain text alphabet
5. looks up that position in the encrypted alphabet
6. adds the encrypted character to the result string
7. when all characters have been processed, output the result

.. hint::

After each step, you should be able to run the program
and see what it already does.

Exercise 6
----------

Explain why the following code does the same as in exercise 1:

.. code:: python3
plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(26):
print(plain[i], " -> ", plain[(i + 3) % 26])
Can you use it to make your encryption program shorter?

.. hint::

If you are not sure what happens, print the value of ``i`` inside the loop.

Exercise 7
----------

Also write a program for **decryption**.

.. hint::

How could you make sure that the input string is in upper case?

Reflection questions
--------------------

- what happens when a string is followed by square brackets?
- describe two ways to loop over the characters of a string?
- what does the ``str.find()`` method do?
- how can you add characters to a string?
Binary file added first_steps/enigma.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
4 changes: 2 additions & 2 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ First Steps
first_steps/for.rst
first_steps/guess_the_number.rst
first_steps/slideshow.rst
data_structures/cypher.rst

--------------

Expand All @@ -44,10 +45,8 @@ Data Structures
.. toctree::
:maxdepth: 1

data_structures/strings.md
data_structures/lists.md
data_structures/dictionaries.md
data_structures/tables.md

--------------

Expand All @@ -57,6 +56,7 @@ Tabular Data
.. toctree::
:maxdepth: 1

data_structures/tables.md
tabular_data/readfile.md
tabular_data/parsing.md
tabular_data/writing_files.md
Expand Down

0 comments on commit 3d71c2d

Please sign in to comment.