{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "welcome",
      "metadata": {},
      "source": ["# DASKEREL · Python foundations\n", "**Linked learning:** Years 7–9 Computing and GCSE Computer Science.\n", "\n", "Work through the cells in order. Before each run, predict the output in your own notes. All values are fictional. No account, files, internet access or package installation is needed.\n", "\n", "**Goals:** trace assignment and selection; explain list indices; implement a linear search; test boundary cases."]
    },
    {
      "cell_type": "markdown",
      "id": "selection-notes",
      "metadata": {},
      "source": ["## 1. Selection and a boundary\n", "`>=` includes the boundary value. Predict the result for 49, 50 and 72 before running the cell. Then add a value of your own."]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "selection-code",
      "metadata": {},
      "outputs": [],
      "source": ["def result(score):\n", "    if score >= 50:\n", "        return 'Pass'\n", "    return 'Review'\n", "\n", "for score in [49, 50, 72]:\n", "    print(score, result(score))\n", "\n", "assert result(49) == 'Review'\n", "assert result(50) == 'Pass'"]
    },
    {
      "cell_type": "markdown",
      "id": "lists-notes",
      "metadata": {},
      "source": ["## 2. Lists and indices\n", "Python list indices start at zero. Predict the first item and the mean. Explain why dividing by `len(scores)` is necessary. Then add a fourth score and recompute."]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "lists-code",
      "metadata": {},
      "outputs": [],
      "source": ["scores = [4, 7, 5]\n", "first = scores[0]\n", "mean = sum(scores) / len(scores)\n", "print('First:', first)\n", "print('Mean:', round(mean, 2))\n", "\n", "assert first == 4\n", "assert round(mean, 2) == 5.33"]
    },
    {
      "cell_type": "markdown",
      "id": "search-notes",
      "metadata": {},
      "source": ["## 3. Linear search\n", "The function returns the first matching index or `-1` when no match exists. Trace `target=5` by hand. What happens if the target is 7? Why does the loop stop at the first 5?"]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "search-code",
      "metadata": {},
      "outputs": [],
      "source": ["def first_position(values, target):\n", "    for index, value in enumerate(values):\n", "        if value == target:\n", "            return index\n", "    return -1\n", "\n", "values = [3, 5, 8, 5]\n", "print(first_position(values, 5))\n", "print(first_position(values, 7))\n", "\n", "assert first_position(values, 5) == 1\n", "assert first_position(values, 7) == -1\n", "assert first_position([], 5) == -1"]
    },
    {
      "cell_type": "markdown",
      "id": "challenge",
      "metadata": {},
      "source": ["## 4. Change, test and explain\n", "1. Modify the search to return **all** matching indices. For `[3, 5, 8, 5]` and target `5`, the result should be `[1, 3]`.\n", "2. Test an empty list, a single match and no match.\n", "3. Explain the difference between returning `-1` and returning an empty list.\n", "4. Keep a short record of your prediction, test result and correction. A teacher can review the reasoning; these assertions are only self checks."]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "challenge-code",
      "metadata": {},
      "outputs": [],
      "source": ["def all_positions(values, target):\n", "    # Replace this line with your loop.\n", "    return []\n", "\n", "# When ready, uncomment these checks:\n", "# assert all_positions([3, 5, 8, 5], 5) == [1, 3]\n", "# assert all_positions([], 5) == []\n", "# assert all_positions([2, 4], 5) == []"]
    }
  ],
  "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"name": "python"}},
  "nbformat": 4,
  "nbformat_minor": 5
}
