diff --git a/data_structures/strings.md b/data_structures/strings.md deleted file mode 100644 index 9c9c921..0000000 --- a/data_structures/strings.md +++ /dev/null @@ -1,88 +0,0 @@ - -# Strings - -In this chapter you will store text (**strings**) in variables. - -## Exercise 1: Indexing and slicing strings - -What do the following expressions result in? - - name = "Ada Lovelace" - - name[0] - name[3] - name[-1] - name[0] + name[6] - name[5:] - name[5:10] - name[:10:2] - ----- - -## Exercise 2: Decypher - -The following text contains an encrypted word: - - name = "CSAIPRALKAINACZEYLVOST" - -Print every second character, starting with the 2nd). - ----- - -## Exercise 3: String methods - -Find out what the expressions do to the string in the middle. - -![string exercise](strings.png) - ----- - -## Exercise 4: String parsing - -Store all the first characters of every name in a new string. - - names = """ - Adelie - Bartek - Charly - Djamal - Emilia - Florin - """ - ----- - -## Recap: String manipulation - -Fill in the blanks so that the assertions pass - - s = "Hello" - - # 1. Concatenate the string - ... - assert s == "Hello World" - - # 2. Convert the string to upper case - ... - assert s == "HELLO WORLD" - - # 3. Exract the first word - ... - assert s == "HELLO" - - # 4. Capitalize the string - ... - assert s == "Hello" - - # 5. Substitute characters - ... - assert s == "Hero" - - ----- - -## Reflection questions - -* Can you modify a string? -* Is there a difference between single-quoted and triple-quoted strings? -* What characters start with a backslash? diff --git a/first_steps/cypher.rst b/first_steps/cypher.rst new file mode 100644 index 0000000..13c764e --- /dev/null +++ b/first_steps/cypher.rst @@ -0,0 +1,132 @@ +Cesar Cypher +============ + +.. image:: enigma.jpg + +Photo by `Christian Lendl on unsplash.com `__ + +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? diff --git a/first_steps/enigma.jpg b/first_steps/enigma.jpg new file mode 100644 index 0000000..07738a7 Binary files /dev/null and b/first_steps/enigma.jpg differ diff --git a/data_structures/strings.png b/first_steps/strings.png similarity index 100% rename from data_structures/strings.png rename to first_steps/strings.png diff --git a/index.rst b/index.rst index 6b32329..5f763f8 100644 --- a/index.rst +++ b/index.rst @@ -35,6 +35,7 @@ First Steps first_steps/for.rst first_steps/guess_the_number.rst first_steps/slideshow.rst + data_structures/cypher.rst -------------- @@ -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 -------------- @@ -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