{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "module-prog", "module-dsml" ] }, "source": [ "(Nested_lists)=\n", "# Nested lists\n", "``` {index} Nested list (Python)\n", "```\n", "Lists can contain elements which are also lists." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45], [100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128]]\n", "[-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]\n", "[100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128]\n", "-15\n" ] } ], "source": [ "list1 = list(range(-20, 50, 5))\n", "list2 = list(range(100, 130, 2))\n", "\n", "list3 = [list1, list2]\n", "\n", "print(list3) # Print new list\n", "print(list3[0]) # Print list1\n", "print(list3[1]) # Print list2\n", "print(list3[0][1]) # Print second element in first list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Above, we created a table with rows. Using `zip()` function we can loop through two or more lists at the same time. With that we can create a table with columns:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-20, 100], [-15, 102], [-10, 104], [-5, 106], [0, 108], [5, 110], [10, 112], [15, 114], [20, 116], [25, 118], [30, 120], [35, 122], [40, 124], [45, 126]]\n", "[[-20, 100], [-15, 102], [-10, 104], [-5, 106], [0, 108], [5, 110], [10, 112], [15, 114], [20, 116], [25, 118], [30, 120], [35, 122], [40, 124], [45, 126]]\n" ] } ], "source": [ "list4 = []\n", "\n", "for i, j in zip(list1, list2):\n", " row = [i, j]\n", " list4. append(row)\n", " \n", "print(list4)\n", "\n", "# This can be also done with list comprehension\n", "\n", "list5 = [[i, j] for i, j in zip(list1, list2)]\n", "print(list5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(nested_lists_exercises)=\n", "## Exercises\n", "\n", "----------\n", "\n", "* **Local peaks**: Powerful alien civilisation plans to invade Earth. Thanks to their adavanced technology, they were able to scan the surface of our planet without anyone noticing. You, as their head general, need to decide where the UFOs will land. The best alien scientists provide you with a map of a terrain where the invasion will begin, which can be generated by the code:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "168 391 491 132 230 160 353 489 330 341\n", "433 294 207 148 349 114 299 321 411 490\n", "492 101 456 328 236 469 217 402 152 262\n", "115 111 113 432 377 104 295 451 210 316\n", "471 114 370 213 491 324 353 383 219 276\n", "218 446 212 489 335 248 111 313 384 428\n", "151 195 422 470 251 161 480 270 469 464\n", "356 316 359 443 197 255 245 400 355 358\n", "301 401 117 345 224 480 306 312 440 188\n", "287 380 459 497 445 477 291 144 324 439\n" ] } ], "source": [ "import random\n", "\n", "# A seed prints the same sequence of numbers every time \n", "# USE FOR TESTING ONLY\n", "# Delete the following line, when done\n", "random.seed(1) \n", "\n", "a = 10\n", "i_map=[[random.randint(100,500) for i in range(a)] for j in range(a)]\n", " \n", "for i in range(a):\n", " print(*i_map[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The numbers in the grid represent the height of a particulair coordinate. The UFO can land only on a local peak i.e. a point surrounded by the points of *equal-or-lower* height, e.g\n", "\n", " 1 2 3\n", " 5 9 6\n", " 6 9 4\n", "\n", "The middle cell with $9$ inside satisfies the local peak condition.\n", "\n", "Return a list of coordinates with local peaks, do not consider the edge cells (but use them in determining other peaks). Good luck General!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{admonition} Answer\n", ":class: dropdown\n", "\n", "```python\n", "import random\n", "random.seed(1)\n", "\n", "a = 10\n", "i_map=[[random.randint(100,500) for i in range(a)] for j in range(a)]\n", "\n", "coords = []\n", "\n", "#Scanning through the grid\n", "for i in range(1,a-1):\n", " for j in range(1,a-1):\n", "\n", " #Checking the peak condition\n", " success = True\n", " for x in range(i-1,i+2):\n", " for y in range(j-1,j+2):\n", " if i_map[x][y] > i_map[i][j]:\n", " success = False\n", " #Found a peak\n", " if success:\n", " coords.append([i,j]) \n", "print(coords) \n", "```\n", "\n", "````" ] } ], "metadata": { "celltoolbar": "Tags", "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" } }, "nbformat": 4, "nbformat_minor": 4 }