{ "cells": [ { "cell_type": "markdown", "id": "43589e5b-ed0d-4f6e-a74e-a37d0914dcc0", "metadata": {}, "source": [ "# Practice: Dictionaries, Files, and Graphs\n", "\n", "## Family Tree\n", "\n", "\n", "You have collected the relationship between people in a family and stored it in\n", "a file in the following format:\n", "\n", "```\n", "leo parent jerry\n", "joyce parent jerry\n", "rick parent beth\n", "beth spouse jerry\n", "beth parent summer\n", "jerry parent summer\n", "beth parent morty\n", "jerry parent morty\n", "summer sibling morty\n", "```\n", "\n", "Each line is composed of three parts, one person, one relationship, and another\n", "person. Sometimes the relationship is symmetrical (e.g. *sibling*), but\n", "sometimes it is asymmetrical (e.g. *parent*). You can assume that all\n", "possible relationships are: *spouse*, *parent*, and *sibling*.\n", "\n", "You want to store this information in a graph in order to be able to find out\n", "later other relationships, such as uncles/aunt, cousin, grandparents, etc. Since\n", "some relationships are not symmetrical, this must be a directed graph. Also,\n", "edges should be labelled with the relationships. Visually, the graph for the\n", "file above is:\n", "\n", "\n", "![graph](smiths.png)\n", "\n", "\n", "This graph should be stored in an adjacency matrix, where each position contains\n", "the relationship between the two people. The adjacency matrix for the graph\n", "above is:\n", "\n", "```\n", "{\n", " 'leo': {'jerry': 'parent'}, \n", " 'joyce': {'jerry': 'parent'}, \n", " 'rick': {'beth': 'parent'}, \n", " 'beth': {'jerry': 'spouse', 'summer': 'parent', 'morty': 'parent'}, \n", " 'jerry': {'beth': 'spouse', 'summer': 'parent', 'morty': 'parent'}, \n", " 'summer': {'morty': 'sibling'}, 'morty': {'summer': 'sibling'}\n", "}\n", "```\n", "The inner dictionaries should contain only those that are related with the\n", "person in the key.\n", "\n" ] }, { "cell_type": "markdown", "id": "8746b2b3", "metadata": {}, "source": [ "### Read the file and create a dictionary\n", "\n", "Implement the function `familyTree(filename)` that takes as\n", " input a file containing family relations as the one above, and returns the\n", " graph of relations as a dictionary of dictionaries." ] }, { "cell_type": "code", "execution_count": null, "id": "7bc0cbc0-0469-4af2-b6c4-1d3ac7f67e52", "metadata": {}, "outputs": [], "source": [ "def familyTree(filename):\n", " " ] }, { "cell_type": "markdown", "id": "7d351b0b", "metadata": {}, "source": [ "### Create a Graph using `networkx` and visualize it\n", "\n", "Implement the function `visualize(ft)` that takes a dictionary of dictionaries representing a family\n", " tree as described above. The function uses `networkx` and `matplotlib` to visualize the data." ] }, { "cell_type": "code", "execution_count": null, "id": "83cea909-5177-4945-8f6b-0bdeb61c3431", "metadata": {}, "outputs": [], "source": [ "def visualize(ft):\n", " " ] }, { "cell_type": "markdown", "id": "992da544", "metadata": {}, "source": [ "### Grandchildren\n", "\n", "Implement the function `grandchildren(p, ft)` that takes as input\n", " the name of a person and a dictionary of dictionaries representing a family\n", " tree as described above. The function returns a list of `p`'s\n", " grandchildren according to `ft` in alphabetical order." ] }, { "cell_type": "code", "execution_count": null, "id": "0cde5593-e01d-4fc0-961f-5b0e3d39b777", "metadata": {}, "outputs": [], "source": [ "def grandchildren(p, ft):\n", " " ] }, { "cell_type": "markdown", "id": "eed77f34", "metadata": {}, "source": [ "### Grandparents\n", "\n", "Implement the function `grandparents(p, ft)` that takes as input\n", " the name of a person and a dictionary of dictionaries representing a family\n", " tree as described above. The function returns a list of `p`'s\n", " grandparents according to `ft` in alphabetical order." ] }, { "cell_type": "code", "execution_count": null, "id": "f982d35d", "metadata": {}, "outputs": [], "source": [ "def grandparents(p, ft):\n", " " ] } ], "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.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }