diff --git a/README.md b/README.md deleted file mode 100644 index 30452da..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# Week_4_Assignment - diff --git a/Week_4_Assignment.ipynb b/Week_4_Assignment.ipynb new file mode 100644 index 0000000..3d51537 --- /dev/null +++ b/Week_4_Assignment.ipynb @@ -0,0 +1,208 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Intelligent World Informatics LectureV: Assignment Week 4\n", + "- Author: Paul Lödige (ploedige@g.ecc.u-tokyo.ac.jp)\n", + "- Student ID: 37-229753\n", + " \n", + "## Assignment\n", + "- Like usual, try to get familiar with dictionaries.\n", + "- Create nested dictionaries, add lists as dictionary values (or even nested lists inside nested dictionaries)\n", + " - Create copies of the dictionaries, modify the lists, and see what happens!" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'dict_list': {'one': 1, 'two': 2, 'three': 3, 'four': [5, 6, 7]}, 'dict_tup': {'one': 1, 'two': 2, 'three': 3, 'four': (4, 5, 6)}, 'deep_dict': {'one': 1, 'two': 2, 'three': 3, 'four': {'one': 4, 'two': 5, 'three': 6, 'four': (7, 8, 9, [10, 11, 12])}}}\n" + ] + } + ], + "source": [ + "# create dictionary that contains a list\n", + "dict_list = {\n", + " \"one\": 1,\n", + " \"two\": 2,\n", + " \"three\": 3,\n", + " \"four\": [5, 6, 7]\n", + "}\n", + "# create dictionary that contains a tuple\n", + "dict_tup = {\n", + " \"one\": 1,\n", + " \"two\": 2,\n", + " \"three\": 3,\n", + " \"four\": (4, 5, 6)\n", + "}\n", + "# create deep nested dictionary\n", + "deep_dict = {\n", + " \"one\": 1,\n", + " \"two\": 2,\n", + " \"three\": 3,\n", + " \"four\": {\n", + " \"one\": 4,\n", + " \"two\": 5,\n", + " \"three\": 6,\n", + " \"four\": (7, 8, 9, [10, 11, 12])\n", + " }\n", + "}\n", + "# create a dictionary of all the objects above\n", + "obj_dict = {\n", + " \"dict_list\": dict_list,\n", + " \"dict_tup\": dict_tup,\n", + " \"deep_dict\": deep_dict\n", + "}\n", + "print(obj_dict)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n", + "4\n" + ] + } + ], + "source": [ + "# print the length of the different objects\n", + "for obj in obj_dict.values():\n", + " print(len(obj))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_list\n", + "\tone: \n", + "\ttwo: \n", + "\tthree: \n", + "\tfour: \n", + "dict_tup\n", + "\tone: \n", + "\ttwo: \n", + "\tthree: \n", + "\tfour: \n", + "deep_dict\n", + "\tone: \n", + "\ttwo: \n", + "\tthree: \n", + "\tfour: \n" + ] + } + ], + "source": [ + "# print the type of each element in each item\n", + "for name, obj in obj_dict.items():\n", + " print(f\"{name}\")\n", + " for key, value in obj.items():\n", + " print(f\"\\t{key}: {type(value)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_list: SUCCESS\n", + "dict_tup: SUCCESS\n", + "deep_dict: SUCCESS\n", + "{'dict_list': {'one': 1, 'two': 42, 'three': 3, 'four': [5, 6, 7]}, 'dict_tup': {'one': 1, 'two': 42, 'three': 3, 'four': (4, 5, 6)}, 'deep_dict': {'one': 1, 'two': 42, 'three': 3, 'four': {'one': 4, 'two': 5, 'three': 6, 'four': (7, 8, 9, [10, 11, 12])}}}\n" + ] + } + ], + "source": [ + "# try to replace every second item with 42\n", + "for name, obj in obj_dict.items():\n", + " try:\n", + " obj[\"two\"] = 42\n", + " print(f\"{name}: SUCCESS\")\n", + " except Exception as e:\n", + " print(f\"{name}: ERROR ({e})\")\n", + "print(obj_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dict_list: SUCCESS\n", + "dict_tup: ERROR ('tuple' object does not support item assignment)\n", + "deep_dict: SUCCESS\n", + "{'dict_list': {'one': 1, 'two': 42, 'three': 3, 'four': [5, 6, 'test']}, 'dict_tup': {'one': 1, 'two': 42, 'three': 3, 'four': (4, 5, 6)}, 'deep_dict': {'one': 1, 'two': 42, 'three': 3, 'four': {'one': 4, 'two': 5, 'three': 'test', 'four': (7, 8, 9, [10, 11, 12])}}}\n" + ] + } + ], + "source": [ + "# try to replace every third item of a subcollection with \"test\" \n", + "for name, obj in obj_dict.items():\n", + " try:\n", + " for collection in [element for element in obj.values() if type(element) is not int]:\n", + " if type(collection) is dict:\n", + " collection[\"three\"] = \"test\"\n", + " else:\n", + " collection[2] = \"test\"\n", + " print(f\"{name}: SUCCESS\")\n", + " except Exception as e:\n", + " print(f\"{name}: ERROR ({e})\")\n", + "print(obj_dict)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.10.8 64-bit", + "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.10.8" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}