{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Second Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "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": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 16, 36, 64, 100, 144, 196]\n" ] } ], "source": [ "def even_squared(N):\n", " '''Receives a positive integer N and returns a list of the even squares from 1 to N'''\n", " return [x**2 for x in range(2,N+1,2)]\n", "\n", "print(even_squared(15))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 2, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Insert an integer: 1\n", "Insert an integer: 2\n", "Insert an integer: 3\n", "Insert an integer: 1\n", "Insert an integer: 4\n", "Insert an integer: 3\n", "Insert an integer: -1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n", "[1, 1, 3, 3]\n" ] } ], "source": [ "even, odd = [], []\n", "while 1:\n", " number_read = int(input(\"Insert an integer: \"))\n", " if number_read == -1:\n", " break\n", " if number_read%2:\n", " odd.append(number_read)\n", " else:\n", " even.append(number_read)\n", "print(sorted(even))\n", "print(sorted(odd))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "def even_account(l):\n", " '''Receives a list of integers and returns the number of even numbers in it'''\n", " return len(list(filter(lambda x:x%2==0,l)))\n", "\n", "print(even_account([1,5,2,6,8,13,27,154,20,22,25,17]))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 9, 16, 4, 36]\n" ] } ], "source": [ "def squared_list(l):\n", " '''Receive a list of integers and returns another list with the squared elements from the original list'''\n", " return list(map(lambda x: x**2, l)) \n", " # return [element ** 2 for element in l]\n", "\n", "print(squared_list([1,3,4,2,6]))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7, 6, 5, 4, 3, 3, 2, 2, 1, 1, 0]\n" ] } ], "source": [ "def descending(A,B):\n", " '''Receives two lists of integers and returns a joint list of elements in descending order'''\n", " C = A + B\n", " C.sort(reverse=True)\n", " return C\n", "\n", "print(descending([1,3,2,4,7],[0,6,5,2,1,3]))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 10, 15, 20, 11, 25, 30, 35, 40, 45, 127, 12]\n" ] } ], "source": [ "def adding(A,*integers):\n", " '''Receives a list of integers and an arbitray amount of integers and returns \n", " a new list with the original list extended with the given integers'''\n", " A.extend(integers)\n", " return A\n", "\n", "print(adding([5,10,15,20,11],25,30,35,40,45,127,12))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n" ] } ], "source": [ "def intersection(A,B):\n", " '''Receives two lists and returns a new list \n", " with the common elements in ascending order'''\n", " C = [value for value in A if value in B]\n", " res = [] \n", " [res.append(x) for x in C if x not in res]\n", " return sorted(res)\n", "\n", "print(intersection([1,3,2,2,7,4],[2,4,8,16]))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6]\n" ] } ], "source": [ "def union(A,B):\n", " '''Receives two lists of integers and returns a new list with all\n", " elements from both, in ascending order, without repetitions'''\n", " C = A + B\n", " res = []\n", " [res.append(x) for x in C if x not in res]\n", " return sorted(res)\n", "\n", "print(union([1,3,2],[4,6,5,3,2]))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n" ] } ], "source": [ "import functools\n", "\n", "def intersection2(*lists):\n", " '''Receives an arbitrary number of lists of integers and returns\n", " a new list with the elements common to all of them, without repetitions\n", " and in ascending order'''\n", " \n", " #return functools.reduce(intercept,lists) #one way to do\n", "\n", " l1 = lists[0] #another way to do\n", " for l2 in lists:\n", " l1 = list(set(l1) & set(l2))\n", " return l1\n", " \n", "print(intersection2([1,2,3,4,5,6],[1,2,4,8],[2,4,6,8],[2,3,4,5,6]))" ] }, { "cell_type": "markdown", "metadata": {}, "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": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[5, 2, 1], [2, 1, 1], [15, 6, 3]]\n" ] } ], "source": [ "def matrix(A, B):\n", " '''Receives two matrices (lists of lists) and returns the \n", " matrix multiplication, also as a list of lists '''\n", " if len(A)!=len(B[0]) or len(A[0])!=len(B):\n", " raise ValueError('Incompatible dimensions')\n", " m, n , lst, C, element= len(B[0]), len(A), [], [], 0\n", " for i in range(n):\n", " for j in range(m):\n", " for k in range(len(B)):\n", " element+=A[i][k]*B[k][j]\n", " lst.append(element)\n", " element = 0\n", " C.append(lst) \n", " lst= []\n", " return C\n", "\n", "X=[[1,0],[0,1],[3,0]]\n", "Y=[[5,2,1],[2,1,1]]\n", "print(matrix(X,Y))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[8, 11], [23, 32]]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix([[1,2],[4,5]],[[2,3],[3,4]])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "Incompatible dimensions", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [12]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m matrix([[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m],[\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m]],[[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m],[\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m],[\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m]])\n", "Input \u001b[0;32mIn [10]\u001b[0m, in \u001b[0;36mmatrix\u001b[0;34m(A, B)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;124;03m'''Receives two matrices (lists of lists) and returns the \u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;124;03mmatrix multiplication, also as a list of lists '''\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(A)\u001b[38;5;241m!=\u001b[39m\u001b[38;5;28mlen\u001b[39m(B[\u001b[38;5;241m0\u001b[39m]) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(A[\u001b[38;5;241m0\u001b[39m])\u001b[38;5;241m!=\u001b[39m\u001b[38;5;28mlen\u001b[39m(B):\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mIncompatible dimensions\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 6\u001b[0m m, n , lst, C, element\u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlen\u001b[39m(B[\u001b[38;5;241m0\u001b[39m]), \u001b[38;5;28mlen\u001b[39m(A), [], [], \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(n):\n", "\u001b[0;31mValueError\u001b[0m: Incompatible dimensions" ] } ], "source": [ "matrix([[1,2],[4,5]],[[1,2],[2,3],[3,4]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Tests for solutions" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ".........\n", "----------------------------------------------------------------------\n", "Ran 9 tests in 0.004s\n", "\n", "OK\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import unittest\n", "\n", "class TestLista02(unittest.TestCase):\n", "\n", " def test_even_squared(self):\n", " self.assertEqual(even_squared(25),[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576])\n", " self.assertEqual(even_squared(1),[])\n", " self.assertEqual(even_squared(123),[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576,\\\n", " 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936,\\\n", " 2116, 2304, 2500, 2704, 2916, 3136, 3364, 3600, 3844, 4096,\\\n", " 4356, 4624, 4900, 5184, 5476, 5776, 6084, 6400, 6724, 7056,\\\n", " 7396, 7744, 8100, 8464, 8836, 9216, 9604, 10000, 10404, 10816,\\\n", " 11236, 11664, 12100, 12544, 12996, 13456, 13924, 14400, 14884])\n", "\n", " '''Question 2 manually corrected'''\n", " \n", " def test_even_account(self):\n", " self.assertEqual(even_account([1,2,3,8,46,12,0,13,27,135,26,168,1649]),7)\n", " self.assertEqual(even_account([827, 647, 664, 270, 561, 665, 781, 69, 245, 324, 957, 911, 213, 9, 93]),3)\n", " self.assertEqual(even_account([422, 11, 469, 340, 717, 310, 956, 220, 557, 804, 751, 908, 917, 524, 234, 308]),10)\n", " \n", "\n", "\n", " def test_squared_list(self):\n", " self.assertEqual(squared_list([697, 983, 600, 651, 747, 884, 769]),\\\n", " [485809, 966289, 360000, 423801, 558009, 781456, 591361])\n", " self.assertEqual(squared_list([96, 89, 35, 38, 19, 12, 63, 24, 81]),\\\n", " [9216, 7921, 1225, 1444, 361, 144, 3969, 576, 6561])\n", " self.assertEqual(squared_list([4, 52, 95, 71, 64, 42, 44, 9, 77, 41]),\\\n", " [16, 2704, 9025, 5041, 4096, 1764, 1936, 81, 5929, 1681])\n", "\n", "\n", " def test_descending(self):\n", " self.assertEqual(descending([54, 23, 9, 63, 26, 60, 85, 84, 62, 42, 28, 23, 64, 2, 73],\\\n", " [46, 94, 89, 14, 2, 40, 1, 3, 10, 7]),\\\n", " [94, 89, 85, 84, 73, 64, 63, 62, 60, 54, 46, 42, 40, 28, 26, 23, 23, 14, 10, 9, 7, 3, 2, 2, 1])\n", " self.assertEqual(descending([69, 52, 4, 21, 51],\\\n", " [71, 7, 86, 100, 18, 90]),\\\n", " [100, 90, 86, 71, 69, 52, 51, 21, 18, 7, 4])\n", " self.assertEqual(descending([74, 22, 32, 52, 45, 29, 99, 54, 3, 21, 54, 5, 26],\\\n", " [58, 72, 16, 69, 56, 49, 6, 52]),\\\n", " [99, 74, 72, 69, 58, 56, 54, 54, 52, 52, 49, 45, 32, 29, 26, 22, 21, 16, 6, 5, 3])\n", " \n", " \n", "\n", " def test_adding(self):\n", " self.assertEqual(adding([95, 23],51, 51, 96, 47, 45, 23, 64, 2, 10, 75, 49),\\\n", " [95, 23, 51, 51, 96, 47, 45, 23, 64, 2, 10, 75, 49])\n", " self.assertEqual(adding([47, 99, 22, 45, 18, 90, 81, 27, 79, 49, 52, 33, 95],33, 88, 89, 93, 9, 29, 95, 94, 12, 36, 6, 0),\\\n", " [47, 99, 22, 45, 18, 90, 81, 27, 79, 49, 52, 33, 95, 33, 88, 89, 93, 9, 29, 95, 94, 12, 36, 6, 0])\n", " self.assertEqual(adding([56, 96, 21, 57, 70],29, 13, 100, 18, 96, 73, 25, 50, 39),\\\n", " [56, 96, 21, 57, 70, 29, 13, 100, 18, 96, 73, 25, 50, 39])\n", "\n", "\n", " def test_intersection(self):\n", " self.assertEqual(intersection([37, 94, 61, 12, 50, 37, 10],[36, 2, 76, 31, 67, 2, 40]),[])\n", " self.assertEqual(intersection([67, 19, 76, 31, 55, 53],[76, 7, 24, 48, 8]),[76])\n", " self.assertEqual(intersection([7, 3, 9, 3, 3, 5, 7, 1],[4, 2, 4, 4, 7, 5, 1, 9]),[1, 5, 7, 9])\n", " \n", "\n", " def test_union(self):\n", " self.assertEqual(union([1, 3, 42, 23, 14, 28, 86, 86, 0, 68],[52, 18, 11, 14, 27, 38, 79, 51, 38]),\\\n", " [0, 1, 3, 11, 14, 18, 23, 27, 28, 38, 42, 51, 52, 68, 79, 86])\n", " self.assertEqual(union([4, 5, 9, 3, 9, 6, 9, 10, 3, 6],[1, 8, 3, 1, 8, 10, 3, 6, 8, 9, 5, 10, 6]),\\\n", " [1, 3, 4, 5, 6, 8, 9, 10])\n", " self.assertEqual(union([6, 0, 2, 4, 4, 0, 7, 5, 1, 5],[2, 1, 7]),\\\n", " [0, 1, 2, 4, 5, 6, 7])\n", " \n", " def test_intersection2(self):\n", " self.assertEqual(intersection2([2, 4, 5, 0, 4, 3, 5, 2, 2, 0, 0, 3],\\\n", " [1, 5, 4, 5, 4, 3, 4, 3, 5, 4, 2, 0, 0, 0],\\\n", " [0, 0, 5, 1, 4, 4, 4, 3, 4, 2, 1, 5, 3, 3],\\\n", " [4, 3, 4, 2, 5, 0, 3, 1, 0, 4, 4],\\\n", " [3, 5, 0, 5, 0, 0, 5, 4, 0, 4, 0, 2, 3, 4],\\\n", " [2, 0, 1, 0, 5, 2, 3, 2, 4, 5, 2, 4, 2],\\\n", " [1, 4, 5, 3, 1, 5, 2, 2, 5, 1, 1, 5, 1, 0, 5],\\\n", " [3, 2, 1, 5, 1, 0, 5, 5, 2, 2, 2, 5, 0, 4, 5],\\\n", " [4, 5, 3, 3, 4, 1, 1, 1, 0, 5],\\\n", " [2, 2, 1, 2, 4, 3, 1, 2, 5, 3, 4, 4, 3]),\\\n", " [3, 4, 5])\n", " self.assertEqual(intersection2([4, 4, 5, 1, 0, 3, 0, 4, 5, 2, 5, 2, 3],\\\n", " [3, 2, 2, 2, 5, 3, 3, 2, 0, 1, 4],\\\n", " [4, 1, 2, 3, 0, 0, 2, 3, 3, 2, 3, 3, 4],\\\n", " [1, 3, 1, 4, 4, 5, 4, 4, 5, 2, 5],\\\n", " [3, 4, 3, 1, 3, 2, 3, 2, 5, 1, 1],\\\n", " [1, 0, 0, 3, 1, 2, 4, 0, 0, 4, 2, 4, 2, 0]),\\\n", " [1, 2, 3, 4])\n", " self.assertEqual(intersection2([5, 4, 0, 3, 5, 1, 0, 3, 1, 3],\\\n", " [1, 5, 4, 2, 1, 0, 4, 3, 4, 3],\\\n", " [4, 5, 3, 3, 5, 2, 2, 3, 0, 0, 4, 4, 5],\\\n", " [2, 2, 4, 2, 5, 0, 0, 5, 4, 4, 1, 3, 0, 4],\\\n", " [1, 4, 3, 5, 3, 0, 3, 0, 3, 1],\\\n", " [3, 1, 3, 4, 2, 2, 4, 2, 0, 0, 3, 3, 5, 3, 3]),\\\n", " [0, 3, 4, 5])\n", "\n", " def test_matrix(self):\n", " self.assertEqual(matrix([[97, 22, 30, 3, 49, 16, 10, 55, 53], [83, 50, 62, 13, 51, 18, 28, 89, 17],\\\n", " [12, 4, 99, 90, 70, 76, 84, 59, 96], [59, 7, 81, 91, 81, 94, 45, 91, 57]],\\\n", " [[27, 94, 22, 22], [33, 14, 81, 91], [75, 28, 13, 51], [79, 70, 53, 78],\\\n", " [19, 29, 93, 99], [18, 8, 50, 3], [31, 92, 63, 66], [81, 49, 53, 75], [34, 2, 64, 49]]),\\\n", " [[13618, 15746, 16759, 18181], [19516, 19742, 20583, 25011], [28336, 23705, 31518, 34528],\\\n", " [29023, 26096, 31280, 34053]])\n", " \n", " self.assertEqual(matrix([[7, 45, 49, 39, 4], [0, 26, 88, 44, 51], [32, 69, 16, 99, 10], [17, 15, 37, 19, 20]],\\\n", " [[56, 77, 33, 39], [40, 94, 1, 95], [20, 55, 55, 72], [40, 75, 86, 13], [20, 29, 82, 25]]),\\\n", " [[4812, 10505, 6653, 8683], [5580, 12063, 12832, 10653], [9032, 17545, 11339, 10492], [3452, 6759, 5885, 5499]])\n", " self.assertEqual(matrix([[80, 56], [44, 22], [8, 63]],[[88, 18, 100], [94, 37, 95]]),\\\n", " [[12304, 3512, 13320], [5940, 1606, 6490], [6626, 2475, 6785]])\n", " \n", "\n", "unittest.main(argv=['first-arg-is-ignored'], exit=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10" } }, "nbformat": 4, "nbformat_minor": 4 }