Skip to content

Commit

Permalink
clean up indexing chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Rother committed Feb 11, 2024
1 parent c1d457e commit 3d17488
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 187 deletions.
9 changes: 7 additions & 2 deletions challenges/ctree.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Christmas Tree
==============
Star Tree
=========

**🎯 Write a program that outputs the following image:**

Expand All @@ -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.
69 changes: 0 additions & 69 deletions data_structures/dictionaries.md

This file was deleted.

114 changes: 0 additions & 114 deletions data_structures/indexing.rst

This file was deleted.

33 changes: 33 additions & 0 deletions first_steps/dictionaries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
======= ====================================

Expand Down Expand Up @@ -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?
File renamed without changes
114 changes: 114 additions & 0 deletions first_steps/indexing.rst
Original file line number Diff line number Diff line change
@@ -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 <https://en.wikipedia.org/wiki/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.
File renamed without changes
File renamed without changes
4 changes: 2 additions & 2 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3d17488

Please sign in to comment.