diff --git a/README.md b/README.md deleted file mode 100644 index 1397ddb..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# Week_5_Assignment - diff --git a/Week_5_Assignment.ipynb b/Week_5_Assignment.ipynb new file mode 100644 index 0000000..e6e8d15 --- /dev/null +++ b/Week_5_Assignment.ipynb @@ -0,0 +1,175 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Intelligent World Informatics LectureV: Assignment Week 5\n", + "- Author: Paul Lödige (ploedige@g.ecc.u-tokyo.ac.jp)\n", + "- Student ID: 37-229753\n", + " \n", + "## Assignment\n", + "- As usual, create short Python code (anything) experimenting with today’s topics: for-loop, if-elif-else statements, and functions.\n", + " - e.g.: Try to create for-loop inside an if-statement, create a nested list inside a for-loop, put them inside a function, etc\n", + " - Experiment by looping through nested lists/nested dictionaries, see what happens when you use “continue” and “break”...\n", + " - You can pass and return any data types for a function\n", + " - Try passing and returning list, tuple, dictionary" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "102334155" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# use a recursive function to print fibonacci\n", + "def recursive_fibonacci(number):\n", + " if number in [1,2]:\n", + " return 1\n", + " elif number == 0:\n", + " return 0\n", + " else:\n", + " return recursive_fibonacci(number - 1) + recursive_fibonacci(number - 2)\n", + "\n", + "recursive_fibonacci(40)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "39909473435004422792081248094960912600792570982820257852628876326523051818641373433549136769424132442293969306537520118273879628025443235370362250955435654171592897966790864814458223141914272590897468472180370639695334449662650312874735560926298246249404168309064214351044459077749425236777660809226095151852052781352975449482565838369809183771787439660825140502824343131911711296392457138867486593923544177893735428602238212249156564631452507658603400012003685322984838488962351492632577755354452904049241294565662519417235020049873873878602731379207893212335423484873469083054556329894167262818692599815209582517277965059068235543139459375028276851221435815957374273143824422909416395375178739268544368126894240979135322176080374780998010657710775625856041594078495411724236560242597759185543824798332467919613598667003025993715274875" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# use a iterative function to print fibonacci\n", + "def efficient_fibonacci(number):\n", + " if number in [1,2]:\n", + " return 1\n", + " elif number == 0:\n", + " return 0\n", + " else:\n", + " n_1 = 1\n", + " n_2 = 1\n", + " for i in range(number - 2):\n", + " n_2 += n_1\n", + " n_1 = n_2 - n_1\n", + " return n_2\n", + "\n", + "efficient_fibonacci(4000)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "LEVEL 0::\t\"int\":\t\"42\"\n", + "LEVEL 0::\t\"string\":\t\"test\"\n", + "LEVEL 0::\t\"list\":\t\n", + "LEVEL 1::\t\"42\"\n", + "LEVEL 1::\t\"test 2\"\n", + "LEVEL 1::\t\n", + "LEVEL 2::\t\"string 2\":\t\"388\"\n", + "LEVEL 0::\t\"tuple\":\t\n", + "LEVEL 1::\t\"test\"\n", + "LEVEL 1::\t\"5333\"\n", + "LEVEL 1::\t\n", + "LEVEL 2::\t\"563\"\n", + "LEVEL 2::\t\"23423\"\n", + "LEVEL 2::\t\"4547\"\n" + ] + } + ], + "source": [ + "# function that loops through any collection and prints its contents to any sublevel\n", + "def print_collection(collection, level_offset= 0):\n", + " if type(collection) not in [list, tuple, dict]:\n", + " print(\"This is not a collection\")\n", + " else:\n", + " if type(collection) is dict:\n", + " for key, value in collection.items():\n", + " if type(value) in [list, tuple, dict]:\n", + " print(f'LEVEL {level_offset}::\\t\"{key}\":\\t{type(value)}')\n", + " print_collection(value, level_offset + 1)\n", + " else:\n", + " print(f'LEVEL {level_offset}::\\t\"{key}\":\\t\"{value}\"')\n", + " if type(collection) in [list, tuple]:\n", + " for element in collection:\n", + " if type(element) in [list, tuple, dict]:\n", + " print(f'LEVEL {level_offset}::\\t{type(element)}')\n", + " print_collection(element, level_offset + 1)\n", + " else:\n", + " print(f'LEVEL {level_offset}::\\t\"{element}\"')\n", + "\n", + "collection = {\n", + " \"int\" : 42,\n", + " \"string\" : \"test\",\n", + " \"list\" : [\n", + " 42,\n", + " \"test 2\",\n", + " {\"string 2\": 388}\n", + " ],\n", + " \"tuple\" : (\n", + " \"test\",\n", + " 5333,\n", + " [563, 23423, 4547]\n", + " )\n", + "}\n", + "\n", + "print_collection(collection)" + ] + } + ], + "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 +}