From 3d17488d231102d56729cfe7b94bcabf1af299f1 Mon Sep 17 00:00:00 2001 From: Kristian Rother Date: Sun, 11 Feb 2024 23:11:52 +0100 Subject: [PATCH] clean up indexing chapters --- challenges/ctree.rst | 9 +- data_structures/dictionaries.md | 69 ----------- data_structures/indexing.rst | 114 ------------------ first_steps/dictionaries.rst | 33 +++++ {data_structures => first_steps}/indexing.png | Bin first_steps/indexing.rst | 114 ++++++++++++++++++ {data_structures => first_steps}/indexing.svg | 0 .../list_funcs1.png | Bin index.rst | 4 +- 9 files changed, 156 insertions(+), 187 deletions(-) delete mode 100644 data_structures/dictionaries.md delete mode 100644 data_structures/indexing.rst rename {data_structures => first_steps}/indexing.png (100%) create mode 100644 first_steps/indexing.rst rename {data_structures => first_steps}/indexing.svg (100%) rename {data_structures => first_steps}/list_funcs1.png (100%) diff --git a/challenges/ctree.rst b/challenges/ctree.rst index b600118..906167d 100644 --- a/challenges/ctree.rst +++ b/challenges/ctree.rst @@ -1,5 +1,5 @@ -Christmas Tree -============== +Star Tree +========= **🎯 Write a program that outputs the following image:** @@ -11,3 +11,8 @@ Christmas Tree ******* ********* *********** + +.. hint:: + + You don't need to come up with an elegant solution right aways. + For your first implementation, a straightforward solution is good enough. diff --git a/data_structures/dictionaries.md b/data_structures/dictionaries.md deleted file mode 100644 index 3f5590c..0000000 --- a/data_structures/dictionaries.md +++ /dev/null @@ -1,69 +0,0 @@ - -# Dictionaries - -## Exercise 1: Explore - -Find out what each of the expressions does to the dictionary in the center. - -![dict exercise](dicts.png) - ----- - -## Exercise 2: Commands - -Define a dictionary: - - d = { - 'cat':'Katze', - 'dog':'Hund', - 'fish':'Fisch' - } - -Then run the following commands and find out what they result in: - - print(d['fish']) - - print('Hund' in d) - - print(list(d.keys())) - - print(d.get('Katze', 'unknown')) - - d.setdefault('cat', 'Stubentiger') - print(d['cat']) - ----- - -## Exercise 3 - -The following program allows you to travel from one city to the next. -Unfortunately, it contains **three bugs**. Find and fix them. - - cities = { - "New York": ["Tokyo", "Paris", "London"], - "Poznan": ["London", "Berlin"], - "London": ["New York", "Poznan"] - "Berlin": ["Tokyo", "Poznan"], - "Tokyo": ["New York", "Berlin"], - "Paris": ["Katmandu"] - } - - location = "Paris" - - print "\nYour task: fly to Katmandu\n" - - while location in cities and location == 'Katmandu': - print(f"You are in {location}") - - print("There are flights to ", cities[location]) - location = input("Where would you like to travel?") - - print("You have reached your destination") - ----- - -## Reflection Questions - -* How can you create a dictionary? -* How can you modify values in a dictionary? -* Is it possible to run a for loop over a dictionary? diff --git a/data_structures/indexing.rst b/data_structures/indexing.rst deleted file mode 100644 index e67d928..0000000 --- a/data_structures/indexing.rst +++ /dev/null @@ -1,114 +0,0 @@ - -Indexing and Slicing -==================== - -Strings and lists are both ordered sequences of elements. -In both, you can address elements by their position. -However, Python is counting differently than humans: - -.. figure:: indexing.png - :alt: Indexing - - -Exercise 1: slicing strings --------------------------------- - -What do the following expressions result in? - -.. code:: python3 - - name = "hello world" - - name[0] - name[3] - name[-1] - name[0] + name[6] - name[5:] - name[5:10] - name[:10:2] - - -Exercise 2: Indexing lists --------------------------- - -What do the following expressions result in? - -.. code:: python3 - - numbers = [1, 4, 9, 16, 25, 36] - - numbers[4] - movies[0] - movies[-1] - numbers[-3] - - -Exercise 3: Slicing -------------------- - -What do the following commands result in? - -.. code:: python3 - - movies = ["Star Wars", "Star Trek", "Ratatouille", "One Piece"] - - movies[2:] - - movies[:2] - - numbers[2:-2] - - numbers[::2] - - -Exercise 4: Decypher --------------------- - -The following text contains an encrypted word: - -.. code:: python3 - - name = "CSAIPRALKAINACZEYLVOST" - -Print every second character, starting with the 2nd). - -Exercise 5: Slicing puzzle --------------------------- - -Use the expressions to modify the list as indicated. Use each expression -once. - -.. figure:: list_funcs1.png - :alt: list funcs exercise1 - - list funcs exercise1 - - -Recap: String manipulation --------------------------- - -Fill in the blanks so that the assertions pass - -.. code:: python3s - - 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" diff --git a/first_steps/dictionaries.rst b/first_steps/dictionaries.rst index 20ec6e8..5f1aad1 100644 --- a/first_steps/dictionaries.rst +++ b/first_steps/dictionaries.rst @@ -11,6 +11,8 @@ area topic ⚙ define a dictionary ⚙ look up values of a dictionary 🔀 iterate through a list of dictionary keys +🔀 use lists as dictionary values +🐞 fix errors in dictionary definitions 🐞 fix index errors ======= ==================================== @@ -73,9 +75,40 @@ Improve the receipt assistant from the previous chapter so that it uses a dictionary of prices. +Exercise 5: Traveler +-------------------- + +The following program allows you to travel from one city to the next. +Unfortunately, it contains **five bugs**. Find and fix them. + +.. code:: python3 + + cities = { + "New York": ["Tokyo", "Delhi", "London"], + "Poznan": ["London", "Berlin"], + "London": ["New York", "Poznan"] + "Berlin": ["Tokyo", "Poznan"], + "Tokyo": ["New York" "Berlin"], + "Delhi": ["Katmandu"] + } + + location = "Berlin" + print "\nYour task: fly to Katmandu\n" + + while location in cities and location == 'Katmandu': + print(f"You are in {location}") + + print("There are flights to ", cities["location"]) + location = input("Where would you like to travel?") + + print("You have reached your destination") + + Reflection Questions -------------------- - How can you create a dictionary? +- What data types can you use as keys of a dictionary? +- What data types can you use as values of a dictionary? - How can you modify values in a dictionary? - Is it possible to run a for loop over a dictionary? diff --git a/data_structures/indexing.png b/first_steps/indexing.png similarity index 100% rename from data_structures/indexing.png rename to first_steps/indexing.png diff --git a/first_steps/indexing.rst b/first_steps/indexing.rst new file mode 100644 index 0000000..17ae86f --- /dev/null +++ b/first_steps/indexing.rst @@ -0,0 +1,114 @@ + +Block Cipher +============ + +In this chapter you will: +------------------------- + +======= ==================================== +area topic +======= ==================================== +🚀 implement a transposition cipher +⚙ slice lists and strings +💡 use the ``join`` method of the ``string`` data type +🔀 use a loop variable for indexing +======= ==================================== + + +Indexing and Slicing +-------------------- + +Strings and lists are both ordered sequences of elements. +In both, you can address elements by their position. +However, Python is counting differently than humans: + +.. figure:: indexing.png + :alt: Indexing + + +Exercise 1: Indexing lists +-------------------------- + +What do the following expressions result in? + +.. code:: python3 + + numbers = [1, 4, 9, 16, 25, 36] + + numbers[4] + movies[0] + movies[-1] + numbers[-3] + + +Exercise 2: Slicing strings +--------------------------- + +What do the following commands result in? + + name = "hello world" + + name[5:] + name[5:10] + name[:10:2] + numbers[2:-2] + numbers[::2] + + +Exercise 3: Decypher +-------------------- + +The following text contains an encrypted word: + +.. code:: python3 + + name = "CSAIPRALKAINACZEYLVOST" + +Print every second character, starting with the 2nds. + + +Exercise 4: Slicing puzzle +-------------------------- + +Use the expressions to modify the list as indicated. Use each expression +once. + +.. figure:: list_funcs1.png + :alt: list funcs exercise1 + + list funcs exercise1 + + +Exercise 5: Blocks +------------------ + +The following code is creating the first two blocks for a `transposition cipher `__ . +Complete the code by creating the other two blocks as well. + +.. code:: python3 + + message = "MEETINGATDAWNATTHEBRIDGE" + + block1 = message[0::4] + block2 = message[1::4] + ___ + ___ + encrypted = block1 + block2 + block3 + block4 + + +Exercise 6: Transposition Cipher +-------------------------------- + +Complete the program that encrypts a text using a transposition cipher: + +.. code:: python3 + + message = input("enter the text to encrypt: ") + encrypted = "" + for start in range(4): + ___ + +Exercise 7: Decrypt +------------------- + +Write a program to decrypt an encrypted message again. diff --git a/data_structures/indexing.svg b/first_steps/indexing.svg similarity index 100% rename from data_structures/indexing.svg rename to first_steps/indexing.svg diff --git a/data_structures/list_funcs1.png b/first_steps/list_funcs1.png similarity index 100% rename from data_structures/list_funcs1.png rename to first_steps/list_funcs1.png diff --git a/index.rst b/index.rst index bdb8ae2..21f908a 100644 --- a/index.rst +++ b/index.rst @@ -36,8 +36,9 @@ First Steps first_steps/guess_the_number.rst first_steps/cypher.rst first_steps/bill.rst - first_steps/slideshow.rst first_steps/dictionaries.md + first_steps/indexing.rst + first_steps/slideshow.rst Tabular Data @@ -46,7 +47,6 @@ Tabular Data .. toctree:: :maxdepth: 1 - data_structures/indexing.rst data_structures/tables.md tabular_data/readfile.md tabular_data/parsing.md