{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# ZHUDDLE | Chapter 08: Lists\n\n8 coding missions + 10 MCQs. 100 XP.\n\nAdapted from Charles R. Severance, Python for Everybody (CC BY 4.0).\n\nSource: https://www.py4e.com/lessons/lists\n\nReading: https://www.py4e.com/html3/08-lists\n\nThe browser runs five independent cases per coding mission. This notebook supplies the first sample; input prompts here are interactive.",
      "id": "lists-000"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C01 | Index and mutate\n\nFor supplied nonempty `numbers`, save its first item as `first` and its last item as `last`. Replace the first item with `replacement`. Save the list length as `length`.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nnumbers = [10, 20, 30]\nreplacement = 99\n```\n\n**Source checkpoint:** A list is a sequence; Lists are mutable",
      "id": "lists-001"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "numbers = [10, 20, 30]\nreplacement = 99",
      "execution_count": null,
      "outputs": [],
      "id": "lists-002"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "first = None\nlast = None\nlength = None\n# Replace the first item.",
      "execution_count": null,
      "outputs": [],
      "id": "lists-003"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C02 | Combine, repeat, and slice\n\nSave `left + right` as `joined`, `left * 2` as `repeated`, and `joined[1:3]` as `middle`. Keep left and right unchanged.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nleft = [1, 2]\nright = [3, 4]\n```\n\n**Source checkpoint:** List operations; List slices",
      "id": "lists-004"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "left = [1, 2]\nright = [3, 4]",
      "execution_count": null,
      "outputs": [],
      "id": "lists-005"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "joined = None\nrepeated = None\nmiddle = None",
      "execution_count": null,
      "outputs": [],
      "id": "lists-006"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C03 | Append, extend, and sort\n\nAppend supplied `item` to `values`, extend values with the individual items from `extra`, then sort values in place in ascending order. Do not assign the return value of sort to values.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nvalues = [3, 1]\nitem = 2\nextra = [5, 4]\n```\n\n**Source checkpoint:** List methods",
      "id": "lists-007"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "values = [3, 1]\nitem = 2\nextra = [5, 4]",
      "execution_count": null,
      "outputs": [],
      "id": "lists-008"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Modify values with three list methods.",
      "execution_count": null,
      "outputs": [],
      "id": "lists-009"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C04 | Remove an element\n\nUse pop with supplied valid `index` on `values`. Store the removed item in `removed`, leaving values with the remaining items in their original order.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nvalues = [10, 20, 30]\nindex = 1\n```\n\n**Source checkpoint:** Deleting elements",
      "id": "lists-010"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "values = [10, 20, 30]\nindex = 1",
      "execution_count": null,
      "outputs": [],
      "id": "lists-011"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "removed = None",
      "execution_count": null,
      "outputs": [],
      "id": "lists-012"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C05 | Lists and string methods\n\nSplit supplied `text` on whitespace into `words`. Join those words with a hyphen into `joined`. Save the number of words as `count`.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\ntext = 'pining for the fjords'\n```\n\n**Source checkpoint:** Lists and strings",
      "id": "lists-013"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "text = 'pining for the fjords'",
      "execution_count": null,
      "outputs": [],
      "id": "lists-014"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "words = None\njoined = None\ncount = None",
      "execution_count": null,
      "outputs": [],
      "id": "lists-015"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C06 | Copy without aliasing\n\nCopy supplied nonempty list `original` using a full slice into `copy`. Change copy[0] to supplied `replacement`. Leave original unchanged. Save `same_object` as the result of `copy is original`.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\noriginal = [1, 2]\nreplacement = 9\n```\n\n**Source checkpoint:** Objects and values; Aliasing; Debugging",
      "id": "lists-016"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "original = [1, 2]\nreplacement = 9",
      "execution_count": null,
      "outputs": [],
      "id": "lists-017"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "copy = None\nsame_object = None",
      "execution_count": null,
      "outputs": [],
      "id": "lists-018"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C07 | Unique words in a file\n\nOpen `romeo.txt`, split each line into words, and append only words not already in `unique`. Sort unique alphabetically. Keep capitalization and punctuation as they are. Use a list and membership checks, without sets or dictionaries. No printing is required.\n\nFive independent checks are worth 2 XP each. \n\nPractice files are created afresh for every check. Sample file contents:\n\n**romeo.txt**\n```text\nBut soft what light\nwhat light breaks\n\n```\n\n**Source checkpoint:** Exercises: Exercise 4",
      "id": "lists-019"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "\n# Practice fixture supplied by ZHUDDLE\nwith open('romeo.txt', \"w\") as fixture:\n    fixture.write('But soft what light\\nwhat light breaks\\n')\n",
      "execution_count": null,
      "outputs": [],
      "id": "lists-020"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "unique = []\n# Read, collect distinct words, and sort.",
      "execution_count": null,
      "outputs": [],
      "id": "lists-021"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C08 | A guarded mail reader\n\nRead `mail.txt`. Split each line and skip it unless it has at least two words and the first word is exactly `From` (not `From:`). Print the second word for each accepted line. Track `count` and finish by printing `Count:` and count. Handle blank and one-word lines safely.\n\nFive independent checks are worth 2 XP each. \n\nPractice files are created afresh for every check. Sample file contents:\n\n**mail.txt**\n```text\nFrom ada@example.org Sat Jan 5\nFrom: header@example.org\n\nFrom\nFrom lin@example.org Sun Jan 6\n\n```\n\n**Source checkpoint:** Debugging: Lists, split, and files; Exercises 2 and 5",
      "id": "lists-022"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "\n# Practice fixture supplied by ZHUDDLE\nwith open('mail.txt', \"w\") as fixture:\n    fixture.write('From ada@example.org Sat Jan 5\\nFrom: header@example.org\\n\\nFrom\\nFrom lin@example.org Sun Jan 6\\n')\n",
      "execution_count": null,
      "outputs": [],
      "id": "lists-023"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "count = 0\n# Guard list indexes before reading a sender.",
      "execution_count": null,
      "outputs": [],
      "id": "lists-024"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M01 | Mutable lists\n\nWhich operation is valid when values is a nonempty list?\n\nA. values[0] = 7\nB. values(0) = 7\nC. values = values.sort() always preserves the list\nD. Lists cannot change",
      "id": "lists-025"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m01 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-026"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M02 | Nested elements\n\nWhat is the length of `[1, [2, 3], 4]`?\n\nA. 4\nB. 2\nC. 3\nD. 5",
      "id": "lists-027"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m02 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-028"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M03 | List concatenation\n\nWhat is `[1, 2] + [3]`?\n\nA. [4, 5]\nB. [1, 2, 3]\nC. [1, 2, [3]]\nD. An error",
      "id": "lists-029"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m03 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-030"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M04 | Append or extend\n\nWhat does `items.append([2, 3])` add?\n\nA. Two separate numbers\nB. Nothing\nC. Only 2\nD. One nested list",
      "id": "lists-031"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m04 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-032"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M05 | Sort return value\n\nWhat does a list sort method return?\n\nA. None\nB. The sorted list\nC. True\nD. The final element",
      "id": "lists-033"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m05 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-034"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M06 | Pop result\n\nWhat does pop return?\n\nA. The remaining list\nB. Nothing\nC. The removed element\nD. The new length",
      "id": "lists-035"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m06 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-036"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M07 | Whitespace split\n\nWhat is `\"a  b\".split()`?\n\nA. [\"a\", \"\", \"b\"]\nB. [\"a\", \"b\"]\nC. [\"a  b\"]\nD. \"ab\"",
      "id": "lists-037"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m07 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-038"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M08 | Aliased names\n\nAfter `a = [1, 2]; b = a; b[0] = 9`, what is a?\n\nA. [1, 2]\nB. [2, 9]\nC. None\nD. [9, 2]",
      "id": "lists-039"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m08 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-040"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M09 | Make a copy\n\nWhich creates a separate shallow copy of list a?\n\nA. b = a[:]\nB. b = a\nC. b = a.sort()\nD. b = a.append(0)",
      "id": "lists-041"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m09 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-042"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M10 | Guard an index\n\nBefore reading words[1], which guard ensures that index exists?\n\nA. len(words) > 0\nB. words[0] == \"From\"\nC. len(words) >= 2\nD. words is not None",
      "id": "lists-043"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m10 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "lists-044"
    }
  ]
}
