{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "KYaqaJJDSGmJ" }, "source": [ "## Second Assignment" ] }, { "cell_type": "markdown", "metadata": { "id": "SxKFYBNKSGmS" }, "source": [ "#### 1) Create a function called **\"even_squared\"** that receives an integer value **N**, and returns a list containing, in ascending order, the square of each of the even values, from 1 to N, including N if applicable. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ACmUKWIcSGmU", "outputId": "858f93f0-79f2-4b50-9f4c-bf2a4e77f92a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter a number: 7\n", "[1, 4, 9, 16, 25, 36, 49]\n" ] } ], "source": [ "def even_squared():\n", " list = []\n", " for i in range(1,N+1):\n", " list.append(i**2)\n", " return list\n", "N=int(input(\"Enter a number: \"))\n", "print(even_squared())" ] }, { "cell_type": "markdown", "metadata": { "id": "3Js8j45lSGmW" }, "source": [ "#### 2) Using a while loop and the **input()** function, read an indefinite amount of **integers** until the number read is **-1**. After this process, print two lists on the screen: The first containing the even integers, and the second containing the odd integers. Both must be in ascending order. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "SHDrCJpBSGmY", "outputId": "849d8277-cf4b-49e4-88bf-2ce056e3501d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Insert an integer: 34\n", "Insert an integer: 52\n", "Insert an integer: 65\n", "Insert an integer: 42\n", "Insert an integer: 11\n", "Insert an integer: 33\n", "Insert an integer: -1\n", "[34, 42, 52]\n", "[11, 33, 65]\n" ] } ], "source": [ "even, odd = [], []\n", "while True:\n", " n = int(input(\"Insert an integer: \"))\n", " if n == -1:\n", " break\n", " if n%2:\n", " odd.append(n)\n", " else:\n", " even.append(n)\n", "print(sorted(even))\n", "print(sorted(odd))" ] }, { "cell_type": "markdown", "metadata": { "id": "IsIQbkoCSGmZ" }, "source": [ "3) Create a function called **\"even_account\"** that receives a list of integers, counts the number of existing even elements, and returns this count. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "KFMxCwO5SGma", "outputId": "63f21ffa-4e03-437a-ccbe-d163b023e061", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the size of the list 5\n", "\n", "\n", "Enter the list items separated by space 2 4 5 7 11\n", "2\n" ] } ], "source": [ "n = int(input(\"Enter the size of the list \"))\n", "print(\"\\n\")\n", "n_list = list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "\n", "def even_account(n_list):\n", " return len(list(filter(lambda x:x%2==0,n_list)))\n", "\n", "print(even_account(n_list))" ] }, { "cell_type": "markdown", "metadata": { "id": "g4PayvhVSGmb" }, "source": [ "#### 4) Create a function called **\"squared_list\"** that receives a list of integers and returns another list whose elements are the squares of the elements of the first. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "1tBdHf9vSGmc", "outputId": "c5e491d8-870a-42f4-e3e2-cd385b7cf9c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the size of the list 5\n", "\n", "\n", "Enter the list items separated by space 0 2 4 7 10\n", "[0, 4, 16, 49, 100]\n" ] } ], "source": [ "n = int(input(\"Enter the size of the list \"))\n", "print(\"\\n\")\n", "n_list = list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "\n", "def squared_list(n_list):\n", " return list(map(lambda x: x**2, n_list)) \n", " \n", "print(squared_list(n_list))" ] }, { "cell_type": "markdown", "metadata": { "id": "lm6gHbfUSGmd" }, "source": [ "#### 5) Create a function called **\"descending\"** that receives two lists of integers and returns a single list, which contains all the elements in descending order, and may include repeated elements. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "r1h4kpjPSGme", "outputId": "1d42ce58-82ca-4b4f-cb0e-db16a418b7fd", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the size of the two lists 5\n", "\n", "\n", "Enter the list items separated by space 4 3 5 1 2\n", "Enter the list items separated by space 9 6 8 7 10\n", "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n" ] } ], "source": [ "n = int(input(\"Enter the size of the two lists \"))\n", "print(\"\\n\")\n", "list_1 = list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "list_2= list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "\n", "def descending(list_1, list_2):\n", " list = list_1 + list_2\n", " list.sort(reverse=True)\n", " return list\n", "\n", "print(descending(list_1,list_2))" ] }, { "cell_type": "markdown", "metadata": { "id": "U1MKOk1LSGmf" }, "source": [ "#### 6) Create a function called **\"adding\"** that receives a list **A**, and an arbitrary number of integers as input. Return a new list containing the elements of **A** plus the integers passed as input, in the order in which they were given. Here is an example: \n", "\n", ">```python\n", ">>>> A = [10,20,30]\n", ">>>> adding(A, 4, 10, 50, 1)\n", "> [10, 20, 30, 4, 10, 50, 1]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9e8C7MjbSGmg", "outputId": "afc2e59d-8a0c-4505-e54f-dd25ab8b6c38", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the list items separated by space 10 20 30\n", "Enter the list items separated by space 4 10 50 1\n", "[10, 20, 30, 4, 10, 50, 1]\n" ] } ], "source": [ "list_1 = list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())\n", "list_2= list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())\n", "\n", "\n", "def adding(list_1, list_2):\n", " list_1.extend(list_2)\n", " return(list_1)\n", "\n", "print(adding(list_1, list_2))" ] }, { "cell_type": "markdown", "metadata": { "id": "I-d0vxXSSGmh" }, "source": [ "#### 7) Create a function called **\"intersection\"** that receives two input lists and returns another list with the values that belong to the two lists simultaneously (intersection) without repetition of values and in ascending order. Use only lists (do not use sets); loops and conditionals. See the example: \n", "\n", ">```python\n", ">>>> A = [-2, 0, 1, 2, 3]\n", ">>>> B = [-1, 2, 3, 6, 8]\n", ">>>> intersection(A,B)\n", "> [2, 3]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "pHml5NiSSGmm", "outputId": "b8f8262e-6873-4b8d-e1db-61eed99fcd12", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the size of the two lists 5\n", "\n", "\n", "Enter the list items separated by space -2 0 1 2 3\n", "Enter the list items separated by space -1 2 3 6 8\n", "[2, 3]\n" ] } ], "source": [ "n = int(input(\"Enter the size of the two lists \"))\n", "print(\"\\n\")\n", "list1 = list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "list2= list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "\n", "def intersection(list1, list2):\n", " list3 = [value for value in list1 if value in list2]\n", " return list3\n", " \n", "print(intersection(list1, list2))" ] }, { "cell_type": "markdown", "source": [ "" ], "metadata": { "id": "NBkWf3_F4s3b" } }, { "cell_type": "markdown", "metadata": { "id": "oxNJms0YSGmm" }, "source": [ "#### 8) Create a function called **\"union\"** that receives two input lists and returns another list with the union of the elements of the two received, without repetition of elements and in ascending order. Use only lists (do not use sets); loops and conditionals. See the example: \n", "\n", ">```python\n", ">>>> A = [-2, 0, 1, 2]\n", ">>>> B = [-1, 1, 2, 10]\n", ">>>> union(A,B)\n", "> [-2, ,-1, 0, 1, 2, 10]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "VqoYA4PuSGmo", "outputId": "c261e445-b8a1-4960-850d-d210912beb70", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter the size of the two lists 5\n", "\n", "\n", "Enter the list items separated by space 1 2 3 4 5\n", "Enter the list items separated by space 4 5 6 7 8\n", "[1, 2, 3, 4, 5, 6, 7, 8]\n" ] } ], "source": [ "n = int(input(\"Enter the size of the two lists \"))\n", "print(\"\\n\")\n", "list1 = list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "list2= list(int(num) for num in input(\"Enter the list items separated by space \").strip().split())[:n]\n", "\n", "def union(list1,list2):\n", " list3 = list1 + list2\n", " united = []\n", " [united.append(x) for x in list3 if x not in united]\n", " return sorted(united)\n", "\n", "print(union(list1, list2))" ] }, { "cell_type": "markdown", "metadata": { "id": "GkNeDWl9SGmp" }, "source": [ "#### 9) Generalize the **\"intersection\"** function so that it receives an indefinite number of lists and returns the intersection of all of them. Call the new function **intersection2**. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "sg6Q6AyjSGmp", "outputId": "8cbb0c8a-7857-4f27-b434-0f2233226704", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[4]\n" ] } ], "source": [ "list1= [3,4,5,6]\n", "list2=[4,5,6,7]\n", "list3=[1,2,3,4]\n", "\n", "def intersection2(*lists):\n", " return list(set.intersection(*map(set, lists)))\n", "\n", "print(intersection2(list1, list2, list3))" ] }, { "cell_type": "markdown", "metadata": { "id": "gxLtPnNYSGmq" }, "source": [ "## Challenge\n", "\n", "#### 10) Create a function named **\"matrix\"** that implements matrix multiplication:\n", " \n", "Given the matrices:\n", " \n", "$A_{m\\times n}=\n", "\\left[\\begin{matrix}\n", "a_{11}&a_{12}&...&a_{1n}\\\\\n", "a_{21}&a_{22}&...&a_{2n}\\\\\n", "\\vdots &\\vdots &&\\vdots\\\\\n", "a_{m1}&a_{m2}&...&a_{mn}\\\\\n", "\\end{matrix}\\right]$\n", " \n", "We will represent then as a list of lists.\n", "\n", "$A = [[a_{11},a_{12},...,a_{1n}],[a_{21},a_{22},...,a_{2n}], . . . ,[a_{m1},a_{m2},...,a_{mn}]]$\n", "\n", "The **\"matrix\"** funtion must receive two matrices $A$ e $B$ in the specified format and return $A\\times B$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "AlAF73B0SGmr" }, "outputs": [], "source": [ "" ] } ], "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.5" }, "colab": { "name": "Assignment_2.ipynb másolata", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 0 }