Skip to content

Commit 20f68e9

Browse files
author
Kristian Rother
committed
add crypto task to builtin functions
1 parent fbab82f commit 20f68e9

File tree

7 files changed

+227
-116
lines changed

7 files changed

+227
-116
lines changed

first_steps/builtin_functions.rst

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Substitution Cipher
2+
===================
3+
4+
In this chapter you learn:
5+
--------------------------
6+
7+
==== ==============================================
8+
area topic
9+
==== ==============================================
10+
🚀 encrypt text with a substitution cipher
11+
⚙ convert strings to lists and back
12+
⚙ use a random seed
13+
💡 use the ``random`` module
14+
💡 use the ``string`` module
15+
💡 use the ``zip`` function
16+
🔀 use the ``dict(zip())`` pattern
17+
🐞 fix IndexErrors
18+
==== ==============================================
19+
20+
Exercise 1: Random string
21+
-------------------------
22+
23+
The following code creates a random chiffre that we will use for encryption.
24+
Insert ``alphabet``, ``chars``, ``join`` and ``random`` into the code:
25+
26+
.. code:: python3
27+
28+
import random
29+
30+
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31+
32+
___ = list(___)
33+
___.shuffle(chars)
34+
chiffre = "".___(chars)
35+
print(chiffre)
36+
37+
Run the program a couple of times.
38+
39+
40+
Exercise 2: Random seed
41+
-----------------------
42+
43+
Add the following line after the import:
44+
45+
.. code:: python3
46+
47+
random.seed(77)
48+
49+
Run the program a couple of times.
50+
What changes?
51+
52+
Also try running the program with different numbers.
53+
54+
55+
Exercise 3: Chiffre indices
56+
---------------------------
57+
58+
Output each character from the chiffre and its index
59+
Simplify the following code using the function ``enumerate()``:
60+
61+
.. code:: python3
62+
63+
i = 0
64+
for char in chiffre:
65+
print(i, char)
66+
i += 1
67+
68+
69+
Exercise 4: Lookup dictionary
70+
-----------------------------
71+
72+
Create a dictionary that maps unencrypted to encrypted characters:
73+
74+
.. code:: python3
75+
76+
lookup = {}
77+
i = 0
78+
while i < len(alphabet):
79+
char = alphabet[i]
80+
lookup[char] = chiffre[i]
81+
i += 1
82+
print(lookup)
83+
84+
Execute the program slowly on paper to make sure you understand the index operations.
85+
86+
Exercise 5: Zip
87+
---------------
88+
89+
Use the following code to make the program above simpler:
90+
91+
.. code:: python3
92+
93+
for char, chif in zip(alphabet, chiffre)
94+
print(char, chif)
95+
96+
Once you understand what happens, also try:
97+
98+
.. code:: python3
99+
100+
lookup = dict(zip(alphabet, chiffer))
101+
102+
103+
Exercise 6: Encrypt
104+
-------------------
105+
106+
Use the lookup dictionary to encrypt a message.
107+
Use the following lines as a starting point:
108+
109+
.. code:: python3
110+
111+
message = ...
112+
result = ""
113+
114+
for char in message:
115+
result += ...
116+
-
117+
Exercise 7: Debugging
118+
---------------------
119+
120+
What happens when you enter lowercase letters or special characters in the message?
121+
122+
Try the expression:
123+
124+
.. code:: python3
125+
126+
print(lookup.get(";", "X"))
127+
128+
Fix the problem.
129+
130+
131+
Exercise 7: Decrypt
132+
-------------------
133+
134+
Decrypt a message when all you know is the encrypted message and the random seed.
135+
136+
137+
Reflection Questions
138+
--------------------
139+
140+
- what does the ``enumerate()`` function do?
141+
- what does the ``zip()`` function do?
142+
- is a substitution cipher rather safe or unsafe?
143+
- what is **symmetric encryption**?
144+
145+
.. seealso::
146+
147+
**Further Reading**
148+
149+
- `Kerckhoffs Principle <https://en.wikipedia.org/wiki/Kerckhoffs%27s_principle>`__

first_steps/dictionaries.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ Unfortunately, it contains **five bugs**. Find and fix them.
104104
print("You have reached your destination")
105105
106106
107+
Exercise 6: Zip
108+
---------------
109+
110+
Simplify the following code using the function ``zip()``:
111+
112+
.. code:: python3
113+
114+
fruits = ["apple", "banana", "orange", "cherries"]
115+
prices = [0.5, 1.0, 1.5, 3.0]
116+
117+
table = []
118+
i = 0
119+
while i < len(fruits):
120+
row = (fruits[i], prices[i])
121+
table.append(row)
122+
i += 1
123+
print(table)
124+
125+
Try the expression:
126+
127+
.. code:: python3
128+
129+
for a, b in zip(fruits, prices):
130+
...
131+
132+
107133
Reflection Questions
108134
--------------------
109135

first_steps/indexing.rst

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,21 @@ What do the following commands result in?
5555
numbers[2:-2]
5656
numbers[::2]
5757

58+
Exercise 3: Ranges
59+
------------------
60+
61+
Use ``list(range())`` to create the following lists:
62+
63+
.. code:: python3
64+
65+
[4, 7, 9, 12, 15]
66+
67+
[10, 20, 30, 40, 50, 60]
68+
69+
[33, 32, 31, 30]
70+
5871
59-
Exercise 3: Decypher
72+
Exercise 4: Decypher
6073
--------------------
6174

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

7083

71-
Exercise 4: Slicing puzzle
84+
Exercise 5: Slicing puzzle
7285
--------------------------
7386

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

8093

81-
Exercise 5: Blocks
94+
Exercise 6: Blocks
8295
------------------
8396

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

101114
Complete the program that encrypts a text using a transposition cipher:
@@ -107,13 +120,13 @@ Complete the program that encrypts a text using a transposition cipher:
107120
for start in range(4):
108121
___
109122
110-
Exercise 7: Decrypt
123+
Exercise 8: Decrypt
111124
-------------------
112125

113126
Write a program to decrypt an encrypted message again.
114127

115128

116-
Exercise 8: Encryption Key
129+
Exercise 9: Encryption Key
117130
--------------------------
118131

119132
Use an encryption key like ``2031`` that specifies a new order for the blocks.

first_steps/statistics.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ area topic
1515
⚙ implement a function
1616
⚙ call a function you defined
1717
💡 use the ``math`` module
18+
💡 use the ``sum`` function
1819
🔀 use a recursive function
1920
==== ==============================================
2021

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

5859

59-
Exercise 3: Standard Deviation
60+
Exercise 3: Shortcut
61+
--------------------
62+
63+
Simplify the mean function using the function ``sum()``.
64+
65+
Use your own or the builtin ``sum()`` function.
66+
67+
68+
Exercise 4: Standard Deviation
6069
------------------------------
6170

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

8796
Explain the program:
@@ -97,7 +106,7 @@ Explain the program:
97106
print(add(b=4))
98107
99108
100-
Exercise 5: Recursion
109+
Exercise 6: Recursion
101110
---------------------
102111

103112
Explain the code:

index.rst

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,52 +39,39 @@ First Steps
3939
first_steps/dictionaries.md
4040
first_steps/indexing.rst
4141
first_steps/nested_lists.rst
42+
first_steps/builtin_functions.rst
4243
first_steps/slideshow.rst
4344
first_steps/babynames.rst
44-
45-
--------------
46-
47-
Program Structures
48-
------------------
49-
50-
.. toctree::
51-
:maxdepth: 1
52-
53-
structure/functions.md
45+
first_steps/statistics.rst
5446
debugging/README.rst
55-
structure/builtin_functions.md
56-
structure/modules.md
57-
structure/refactoring.rst
47+
refactoring/refactoring.rst
48+
challenges/text_adventure.rst
5849

5950
--------------
6051

61-
Challenges
62-
==========
52+
Coding Challenges
53+
-----------------
6354

6455
.. toctree::
6556
:maxdepth: 1
6657

6758
challenges/ctree.rst
6859
challenges/pyramid.rst
6960
challenges/fizzbuzz.rst
70-
challenges/tictac.rst
7161
challenges/quiz.rst
7262
challenges/checker.rst
7363
challenges/baby_name_generator.rst
7464
challenges/palindrome.rst
7565
challenges/anagrams.rst
76-
challenges/spiral.rst
77-
challenges/roman.rst
78-
challenges/encrypt.rst
66+
challenges/tictac.rst
7967
challenges/minesweeper.rst
8068
challenges/gcd.rst
8169
challenges/fibonacci.rst
8270
challenges/querprodukt.rst
8371
challenges/birthdays.rst
84-
challenges/seven_peaks.rst
72+
challenges/misty_mountains.rst
8573
challenges/count_words.rst
86-
challenges/postcard.rst
87-
challenges/text_adventure.rst
74+
challenges/spiral.rst
8875

8976
--------------
9077

@@ -118,7 +105,6 @@ Examples of common Python commands
118105
reference/while.rst
119106
reference/builtin_functions.rst
120107
reference/functions.rst
121-
reference/introspection.rst
122108

123109

124110
Appendix

0 commit comments

Comments
 (0)