Skip to content

Commit

Permalink
add crypto task to builtin functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Rother committed Feb 12, 2024
1 parent fbab82f commit 20f68e9
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 116 deletions.
149 changes: 149 additions & 0 deletions first_steps/builtin_functions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
Substitution Cipher
===================

In this chapter you learn:
--------------------------

==== ==============================================
area topic
==== ==============================================
🚀 encrypt text with a substitution cipher
⚙ convert strings to lists and back
⚙ use a random seed
💡 use the ``random`` module
💡 use the ``string`` module
💡 use the ``zip`` function
🔀 use the ``dict(zip())`` pattern
🐞 fix IndexErrors
==== ==============================================

Exercise 1: Random string
-------------------------

The following code creates a random chiffre that we will use for encryption.
Insert ``alphabet``, ``chars``, ``join`` and ``random`` into the code:

.. code:: python3
import random
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
___ = list(___)
___.shuffle(chars)
chiffre = "".___(chars)
print(chiffre)
Run the program a couple of times.


Exercise 2: Random seed
-----------------------

Add the following line after the import:

.. code:: python3
random.seed(77)
Run the program a couple of times.
What changes?

Also try running the program with different numbers.


Exercise 3: Chiffre indices
---------------------------

Output each character from the chiffre and its index
Simplify the following code using the function ``enumerate()``:

.. code:: python3
i = 0
for char in chiffre:
print(i, char)
i += 1
Exercise 4: Lookup dictionary
-----------------------------

Create a dictionary that maps unencrypted to encrypted characters:

.. code:: python3
lookup = {}
i = 0
while i < len(alphabet):
char = alphabet[i]
lookup[char] = chiffre[i]
i += 1
print(lookup)
Execute the program slowly on paper to make sure you understand the index operations.

Exercise 5: Zip
---------------

Use the following code to make the program above simpler:

.. code:: python3
for char, chif in zip(alphabet, chiffre)
print(char, chif)
Once you understand what happens, also try:

.. code:: python3
lookup = dict(zip(alphabet, chiffer))
Exercise 6: Encrypt
-------------------

Use the lookup dictionary to encrypt a message.
Use the following lines as a starting point:

.. code:: python3
message = ...
result = ""
for char in message:
result += ...
-
Exercise 7: Debugging
---------------------

What happens when you enter lowercase letters or special characters in the message?

Try the expression:

.. code:: python3
print(lookup.get(";", "X"))
Fix the problem.


Exercise 7: Decrypt
-------------------

Decrypt a message when all you know is the encrypted message and the random seed.


Reflection Questions
--------------------

- what does the ``enumerate()`` function do?
- what does the ``zip()`` function do?
- is a substitution cipher rather safe or unsafe?
- what is **symmetric encryption**?

.. seealso::

**Further Reading**

- `Kerckhoffs Principle <https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle>`__
26 changes: 26 additions & 0 deletions first_steps/dictionaries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ Unfortunately, it contains **five bugs**. Find and fix them.
print("You have reached your destination")
Exercise 6: Zip
---------------

Simplify the following code using the function ``zip()``:

.. code:: python3
fruits = ["apple", "banana", "orange", "cherries"]
prices = [0.5, 1.0, 1.5, 3.0]
table = []
i = 0
while i < len(fruits):
row = (fruits[i], prices[i])
table.append(row)
i += 1
print(table)
Try the expression:

.. code:: python3
for a, b in zip(fruits, prices):
...
Reflection Questions
--------------------

Expand Down
25 changes: 19 additions & 6 deletions first_steps/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,21 @@ What do the following commands result in?
numbers[2:-2]
numbers[::2]

Exercise 3: Ranges
------------------

Use ``list(range())`` to create the following lists:

.. code:: python3
[4, 7, 9, 12, 15]
[10, 20, 30, 40, 50, 60]
[33, 32, 31, 30]
Exercise 3: Decypher
Exercise 4: Decypher
--------------------

The following text contains an encrypted word:
Expand All @@ -68,7 +81,7 @@ The following text contains an encrypted word:
Print every second character, starting with the 2nds.


Exercise 4: Slicing puzzle
Exercise 5: Slicing puzzle
--------------------------

Use the expressions to modify the list as indicated. Use each expression
Expand All @@ -78,7 +91,7 @@ once.
:alt: list funcs exercise1


Exercise 5: Blocks
Exercise 6: Blocks
------------------

The following code is creating the first two blocks for a `transposition cipher <https://en.wikipedia.org/wiki/Transposition_cipher>`__ .
Expand All @@ -95,7 +108,7 @@ Complete the code by creating the other two blocks as well.
encrypted = block1 + block2 + block3 + block4
Exercise 6: Transposition Cipher
Exercise 7: Transposition Cipher
--------------------------------

Complete the program that encrypts a text using a transposition cipher:
Expand All @@ -107,13 +120,13 @@ Complete the program that encrypts a text using a transposition cipher:
for start in range(4):
___
Exercise 7: Decrypt
Exercise 8: Decrypt
-------------------

Write a program to decrypt an encrypted message again.


Exercise 8: Encryption Key
Exercise 9: Encryption Key
--------------------------

Use an encryption key like ``2031`` that specifies a new order for the blocks.
Expand Down
15 changes: 12 additions & 3 deletions first_steps/statistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ area topic
⚙ implement a function
⚙ call a function you defined
💡 use the ``math`` module
💡 use the ``sum`` function
🔀 use a recursive function
==== ==============================================

Expand Down Expand Up @@ -56,7 +57,15 @@ Write a function that calculates the arithmetic mean from the following numbers:
Don't forget about the ``return`` statement.


Exercise 3: Standard Deviation
Exercise 3: Shortcut
--------------------

Simplify the mean function using the function ``sum()``.

Use your own or the builtin ``sum()`` function.


Exercise 4: Standard Deviation
------------------------------

The following program calculates the standard deviation from a list of
Expand All @@ -81,7 +90,7 @@ data – in a function.
print(f"Standard Deviation: {stdev:8.2f}")
Exercise 4: Optional Parameters
Exercise 5: Optional Parameters
-------------------------------

Explain the program:
Expand All @@ -97,7 +106,7 @@ Explain the program:
print(add(b=4))
Exercise 5: Recursion
Exercise 6: Recursion
---------------------

Explain the code:
Expand Down
32 changes: 9 additions & 23 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,39 @@ First Steps
first_steps/dictionaries.md
first_steps/indexing.rst
first_steps/nested_lists.rst
first_steps/builtin_functions.rst
first_steps/slideshow.rst
first_steps/babynames.rst

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

Program Structures
------------------

.. toctree::
:maxdepth: 1

structure/functions.md
first_steps/statistics.rst
debugging/README.rst
structure/builtin_functions.md
structure/modules.md
structure/refactoring.rst
refactoring/refactoring.rst
challenges/text_adventure.rst

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

Challenges
==========
Coding Challenges
-----------------

.. toctree::
:maxdepth: 1

challenges/ctree.rst
challenges/pyramid.rst
challenges/fizzbuzz.rst
challenges/tictac.rst
challenges/quiz.rst
challenges/checker.rst
challenges/baby_name_generator.rst
challenges/palindrome.rst
challenges/anagrams.rst
challenges/spiral.rst
challenges/roman.rst
challenges/encrypt.rst
challenges/tictac.rst
challenges/minesweeper.rst
challenges/gcd.rst
challenges/fibonacci.rst
challenges/querprodukt.rst
challenges/birthdays.rst
challenges/seven_peaks.rst
challenges/misty_mountains.rst
challenges/count_words.rst
challenges/postcard.rst
challenges/text_adventure.rst
challenges/spiral.rst

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

Expand Down Expand Up @@ -118,7 +105,6 @@ Examples of common Python commands
reference/while.rst
reference/builtin_functions.rst
reference/functions.rst
reference/introspection.rst


Appendix
Expand Down
Loading

0 comments on commit 20f68e9

Please sign in to comment.