{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 9: Mappings & Sets (Review Questions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The questions below assume that you have read the [first ](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/main/09_mappings/00_content.ipynb), [second ](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/main/09_mappings/02_content.ipynb), and [third ](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/main/09_mappings/04_content.ipynb) part of Chapter 9.\n", "\n", "Be concise in your answers! Most questions can be answered in *one* sentence." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Essay Questions " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1**: `dict` objects are well-suited **to model** discrete mathematical **functions** and to approximate continuous ones. What property of dictionaries is the basis for that claim, and how does it relate to functions in the mathematical sense?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2**: Explain why **hash tables** are a **trade-off** between **computational speed** and **memory** usage!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3:** The `dict` type is an **iterable** that **contains** a **finite** number of key-value pairs. Despite that, why is it *not* considered a **sequence**?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4**: Whereas *key* **look-ups** in a `dict` object run in so-called **[constant time ](https://en.wikipedia.org/wiki/Time_complexity#Constant_time)** (i.e., *extremely* fast), that does not hold for *reverse* look-ups. Why is that?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q5**: Why is it conceptually correct that the Python core developers do not implement **slicing** with the `[]` operator for `dict` objects?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q6**: **Memoization** is an essential concept to know to solve problems in the real world. Together with the idea of **recursion**, it enables us to solve problems in a \"backwards\" fashion *effectively*.\n", "\n", "Compare the **recursive** formulation of `fibonacci()` in [Chapter 9 ](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/main/09_mappings/02_content.ipynb#\"Easy-at-second-Glance\"-Example:-Fibonacci-Numbers--(revisited)), the \"*Easy at second Glance*\" example, with the **iterative** version in [Chapter 4 ](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/main/04_iteration/02_content.ipynb#\"Hard-at-first-Glance\"-Example:-Fibonacci-Numbers-%28revisited%29), the \"*Hard at first Glance*\" example!\n", "\n", "How are they similar and how do they differ? Also consider how the flow of execution behaves when the functions are being executed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q7**: How are the `set` and the `dict` types related? How could we use the latter to mimic the former?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## True / False Questions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Motivate your answer with *one short* sentence!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q8**: We may *not* put `dict` objects inside other `dict` objects because they are **mutable**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q9**: **Mutable** objects (e.g., `list`) may generally *not* be used as keys in a `dict` object. However, if we collect, for example, `list` objects in a `tuple` object, the composite object becomes **hashable**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q10**: **Mutability** of a `dict` object works until the underlying hash table becomes too crowded. Then, we cannot insert any items any more making the `dict` object effectively **immutable**. Luckily, that almost never happens in practice." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q11**: A `dict` object's [.update() ](https://docs.python.org/3/library/stdtypes.html#dict.update) method only inserts key-value pairs whose key is *not* yet in the `dict` object. So, it does *not* overwrite anything." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q12**: The `set` type is both a mapping and a sequence." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": false, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }