diff --git a/input_output.ipynb b/input_output.ipynb index f0bc31c5..12177580 100644 --- a/input_output.ipynb +++ b/input_output.ipynb @@ -5,21 +5,30 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Table of contents\n", - "\n", - "- [String input](#String-input)\n", - "- [File I/O](#File-I/O)\n", - "- [Writing to a file](#Writing-to-a-file)\n", - "- [Context managers](#Context-managers)\n", - "- [Binary I/O](#Binary-I/O)\n", - " - [Bytes and strings](#Bytes-and-strings)\n", - " - [Converting bytes to text](#Converting-bytes-to-text)\n", - "- [Reading/Writing CSV files](#Reading/Writing-CSV-files)\n", - "- [Exercises](#Exercises)\n", + "# Table of Contents\n", + " - [References](#References)\n", + " - [Introduction](#Introduction)\n", + " - [String input and output ](#String-input-and-output)\n", + " - [String output](#String-output)\n", + " - [String input](#String-input)\n", + " - [Warm-up exercises](#Warm-up-exercises)\n", + " - [File I/O](#File-I/O)\n", + " - [Paths](#Paths)\n", + " - [Exercises on Paths](#Exercises-on-Paths)\n", + " - [Reading from a file](#Reading-from-a-file)\n", + " - [Writing to a file](#Writing-to-a-file)\n", + " - [Exercises on file reading and writing](#Exercises-on-file-reading-and-writing)\n", + " - [Context managers](#Context-managers)\n", + " - [Binary I/O](#Binary-I/O)\n", + " - [Bytes and strings](#Bytes-and-strings)\n", + " - [Converting bytes to text ](#Converting-bytes-to-text)\n", + " - [Reading/Writing CSV files](#Reading/Writing-CSV-files)\n", + " - [Exercises](#Exercises)\n", " - [Exercise 1: CSV to dictionary 🌶️🌶️](#Exercise-1:-CSV-to-dictionary-🌶️🌶️)\n", " - [Exercise 2: Counting words 🌶️](#Exercise-2:-Counting-words-🌶️)\n", + " - [Exercise 3: Letter statistics 🌶️🌶️](#Exercise-3:-Letter-statistics-🌶️🌶️)\n", " - [Exercise 4: Translating words 🌶️🌶️](#Exercise-4:-Translating-words-🌶️🌶️)\n", - " - [Exercise 5: Binary format 🌶️🌶️🌶️](#Exercise-5:-Binary-format-🌶️🌶️🌶️)\n" + " - [Exercise 5: Binary format 🌶️🌶️🌶️](#Exercise-5:-Binary-format-🌶️🌶️🌶️)" ] }, { @@ -120,7 +129,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercises" + "### Warm-up exercises" ] }, { @@ -356,7 +365,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercises" + "### Exercises on Paths" ] }, { @@ -505,37 +514,6 @@ "This is the most *pythonic* way to read a file line-by-line instead of reading the full contents at once." ] }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Exercises" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "1. Modify the function `solution_read_file` to return the content of the file passed as the `input_file` argument. Return the content as a **list of strings**, one string per line.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%ipytest\n", - "\n", - "from pathlib import Path\n", - "\n", - "def solution_read_file(input_file: Path) -> \"list[str]\": \n", - " \"\"\"Write your solution here\"\"\"\n", - " pass" - ] - }, { "attachments": {}, "cell_type": "markdown", @@ -605,7 +583,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exercises" + "### Exercises on file reading and writing" ] }, { @@ -622,7 +600,30 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "1. Modify the function `solution_write_file` to write the sentence \"python tutorial 2023\" (**without quotes**) to the file `output_file`, which is available as a `Path` object as argument to the function." + "1. Modify the function `solution_read_file` to return the content of the file passed as the `input_file` argument. Return the content as a **list of strings**, one string per line.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%ipytest\n", + "\n", + "from pathlib import Path\n", + "\n", + "def solution_read_file(input_file: Path) -> \"list[str]\": \n", + " \"\"\"Write your solution here\"\"\"\n", + " pass" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Modify the function `solution_write_file` to write the sentence \"python tutorial 2023\" (**without quotes**) to the file `output_file`, which is available as a `Path` object as argument to the function." ] }, { @@ -647,7 +648,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "1. Modify the function `solution_read_write_file` to read the lines from the file `input_file` and write them in the form `line, length`, to the file `output_file`. Here `line` is the line of text in `input_file` **without the line ending**, `length` is **number of characters** in that line.\n", + "3. Modify the function `solution_read_write_file` to read the lines from the file `input_file` and write them in the form `line, length`, to the file `output_file`. Here `line` is the line of text in `input_file` **without the line ending**, `length` is **number of characters** in that line.\n", "If `input_file` contains these lines:\n", " ```\n", " first\n", @@ -1104,7 +1105,7 @@ "tags": [] }, "source": [ - "Write a function to read all the lines from [`lines.txt`](./tutorial/tests/data/lines.txt) and count the number of words in the file. The solution should be a single number.\n", + "Write a function to read all the lines from `input_file` and count the number of words in the file. The solution should be a single number.\n", "\n", "For example, for the file\n", "```\n", @@ -1118,7 +1119,11 @@ "\n", "
input_file of solution_exercise2 function\n",
+ " input_file of solution_exercise2 function