|
5 | 5 | "cell_type": "markdown",
|
6 | 6 | "metadata": {},
|
7 | 7 | "source": [
|
8 |
| - "## Table of contents\n", |
9 |
| - "\n", |
10 |
| - "- [String input](#String-input)\n", |
11 |
| - "- [File I/O](#File-I/O)\n", |
12 |
| - "- [Writing to a file](#Writing-to-a-file)\n", |
13 |
| - "- [Context managers](#Context-managers)\n", |
14 |
| - "- [Binary I/O](#Binary-I/O)\n", |
15 |
| - " - [Bytes and strings](#Bytes-and-strings)\n", |
16 |
| - " - [Converting bytes to text](#Converting-bytes-to-text)\n", |
17 |
| - "- [Reading/Writing CSV files](#Reading/Writing-CSV-files)\n", |
18 |
| - "- [Exercises](#Exercises)\n", |
| 8 | + "# Table of Contents\n", |
| 9 | + " - [References](#References)\n", |
| 10 | + " - [Introduction](#Introduction)\n", |
| 11 | + " - [String input and output ](#String-input-and-output)\n", |
| 12 | + " - [String output](#String-output)\n", |
| 13 | + " - [String input](#String-input)\n", |
| 14 | + " - [Warm-up exercises](#Warm-up-exercises)\n", |
| 15 | + " - [File I/O](#File-I/O)\n", |
| 16 | + " - [Paths](#Paths)\n", |
| 17 | + " - [Exercises on Paths](#Exercises-on-Paths)\n", |
| 18 | + " - [Reading from a file](#Reading-from-a-file)\n", |
| 19 | + " - [Writing to a file](#Writing-to-a-file)\n", |
| 20 | + " - [Exercises on file reading and writing](#Exercises-on-file-reading-and-writing)\n", |
| 21 | + " - [Context managers](#Context-managers)\n", |
| 22 | + " - [Binary I/O](#Binary-I/O)\n", |
| 23 | + " - [Bytes and strings](#Bytes-and-strings)\n", |
| 24 | + " - [Converting bytes to text ](#Converting-bytes-to-text)\n", |
| 25 | + " - [Reading/Writing CSV files](#Reading/Writing-CSV-files)\n", |
| 26 | + " - [Exercises](#Exercises)\n", |
19 | 27 | " - [Exercise 1: CSV to dictionary 🌶️🌶️](#Exercise-1:-CSV-to-dictionary-🌶️🌶️)\n",
|
20 | 28 | " - [Exercise 2: Counting words 🌶️](#Exercise-2:-Counting-words-🌶️)\n",
|
| 29 | + " - [Exercise 3: Letter statistics 🌶️🌶️](#Exercise-3:-Letter-statistics-🌶️🌶️)\n", |
21 | 30 | " - [Exercise 4: Translating words 🌶️🌶️](#Exercise-4:-Translating-words-🌶️🌶️)\n",
|
22 |
| - " - [Exercise 5: Binary format 🌶️🌶️🌶️](#Exercise-5:-Binary-format-🌶️🌶️🌶️)\n" |
| 31 | + " - [Exercise 5: Binary format 🌶️🌶️🌶️](#Exercise-5:-Binary-format-🌶️🌶️🌶️)" |
23 | 32 | ]
|
24 | 33 | },
|
25 | 34 | {
|
|
120 | 129 | "cell_type": "markdown",
|
121 | 130 | "metadata": {},
|
122 | 131 | "source": [
|
123 |
| - "### Exercises" |
| 132 | + "### Warm-up exercises" |
124 | 133 | ]
|
125 | 134 | },
|
126 | 135 | {
|
|
356 | 365 | "cell_type": "markdown",
|
357 | 366 | "metadata": {},
|
358 | 367 | "source": [
|
359 |
| - "### Exercises" |
| 368 | + "### Exercises on Paths" |
360 | 369 | ]
|
361 | 370 | },
|
362 | 371 | {
|
|
505 | 514 | "This is the most *pythonic* way to read a file line-by-line instead of reading the full contents at once."
|
506 | 515 | ]
|
507 | 516 | },
|
508 |
| - { |
509 |
| - "attachments": {}, |
510 |
| - "cell_type": "markdown", |
511 |
| - "metadata": {}, |
512 |
| - "source": [ |
513 |
| - "### Exercises" |
514 |
| - ] |
515 |
| - }, |
516 |
| - { |
517 |
| - "attachments": {}, |
518 |
| - "cell_type": "markdown", |
519 |
| - "metadata": {}, |
520 |
| - "source": [ |
521 |
| - "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" |
522 |
| - ] |
523 |
| - }, |
524 |
| - { |
525 |
| - "cell_type": "code", |
526 |
| - "execution_count": null, |
527 |
| - "metadata": {}, |
528 |
| - "outputs": [], |
529 |
| - "source": [ |
530 |
| - "%%ipytest\n", |
531 |
| - "\n", |
532 |
| - "from pathlib import Path\n", |
533 |
| - "\n", |
534 |
| - "def solution_read_file(input_file: Path) -> \"list[str]\": \n", |
535 |
| - " \"\"\"Write your solution here\"\"\"\n", |
536 |
| - " pass" |
537 |
| - ] |
538 |
| - }, |
539 | 517 | {
|
540 | 518 | "attachments": {},
|
541 | 519 | "cell_type": "markdown",
|
|
605 | 583 | "cell_type": "markdown",
|
606 | 584 | "metadata": {},
|
607 | 585 | "source": [
|
608 |
| - "### Exercises" |
| 586 | + "### Exercises on file reading and writing" |
609 | 587 | ]
|
610 | 588 | },
|
611 | 589 | {
|
|
622 | 600 | "cell_type": "markdown",
|
623 | 601 | "metadata": {},
|
624 | 602 | "source": [
|
625 |
| - "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
F41A
a `Path` object as argument to the function." |
| 603 | + "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" |
| 604 | + ] |
| 605 | + }, |
| 606 | + { |
| 607 | + "cell_type": "code", |
| 608 | + "execution_count": null, |
| 609 | + "metadata": {}, |
| 610 | + "outputs": [], |
| 611 | + "source": [ |
| 612 | + "%%ipytest\n", |
| 613 | + "\n", |
| 614 | + "from pathlib import Path\n", |
| 615 | + "\n", |
| 616 | + "def solution_read_file(input_file: Path) -> \"list[str]\": \n", |
| 617 | + " \"\"\"Write your solution here\"\"\"\n", |
| 618 | + " pass" |
| 619 | + ] |
| 620 | + }, |
| 621 | + { |
| 622 | + "attachments": {}, |
| 623 | + "cell_type": "markdown", |
| 624 | + "metadata": {}, |
| 625 | + "source": [ |
| 626 | + "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." |
626 | 627 | ]
|
627 | 628 | },
|
628 | 629 | {
|
|
647 | 648 | "cell_type": "markdown",
|
648 | 649 | "metadata": {},
|
649 | 650 | "source": [
|
650 |
| - "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", |
| 651 | + "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", |
651 | 652 | "If `input_file` contains these lines:\n",
|
652 | 653 | " ```\n",
|
653 | 654 | " first\n",
|
|
1104 | 1105 | "tags": []
|
1105 | 1106 | },
|
1106 | 1107 | "source": [
|
1107 |
| - "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", |
| 1108 | + "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", |
1108 | 1109 | "\n",
|
1109 | 1110 | "For example, for the file\n",
|
1110 | 1111 | "```\n",
|
|
1118 | 1119 | "\n",
|
1119 | 1120 | "<div class=\"alert alert-block alert-info\">\n",
|
1120 | 1121 | " <h4><b>Hint</b></h4>\n",
|
1121 |
| - " The file is available as the parameter <code>input_file</code> of <code>solution_exercise2</code> function\n", |
| 1122 | + " <ul>\n", |
| 1123 | + " <li>The file is available as the parameter <code>input_file</code> of <code>solution_exercise2</code> function</li>\n", |
| 1124 | + " <li>A word consists of printable characters without line ends and spaces</li>\n", |
| 1125 | + " </ul>\n", |
| 1126 | + " \n", |
1122 | 1127 | "</div>\n",
|
1123 | 1128 | "\n",
|
1124 | 1129 | "\n"
|
|
1338 | 1343 | "name": "python",
|
1339 | 1344 | "nbconvert_exporter": "python",
|
1340 | 1345 | "pygments_lexer": "ipython3",
|
1341 |
| - "version": "3.11.4" |
| 1346 | + "version": "3.10.14" |
1342 | 1347 | },
|
1343 | 1348 | "vscode": {
|
1344 | 1349 | "interpreter": {
|
|
0 commit comments