{ "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": [ { "data": { "text/plain": [ "[4, 16]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def even_squared(N): \n", " '''returns a list of squared even values from 1 to N'''\n", " my_range = range(1, N)\n", " squares = [value**2 for value in my_range if value%2 ==0]\n", " return (squares)\n", "\n", "even_squared(5)" ] }, { "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": [ "Type in an integer: 2\n", "Type in an integer: 4\n", "Type in an integer: 5\n", "Type in an integer: -1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n", "[5]\n" ] } ], "source": [ "even_list = []\n", "odd_list = []\n", "\n", "while True: \n", " i = int(input(\"Type in an integer: \"))\n", " if i == -1: \n", " break\n", " if i%2 ==0: \n", " even_list.append(i)\n", " else: \n", " odd_list.append(i)\n", "print(sorted(even_list))\n", "print(sorted(odd_list))" ] }, { "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": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def even_account(l): \n", " '''count the number of given even numbers'''\n", " list1 = len([num for num in l if num%2 ==0])\n", " return list1\n", "\n", "my_list = [4, 5, 2, 12]\n", "even_account(my_list)" ] }, { "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": [ { "data": { "text/plain": [ "[1, 9]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def square_list(lis):\n", " '''return list of swaures of all the elements in the given list'''\n", " squares = [value **2 for value in lis]\n", " return squares\n", "\n", "my_test_list = [1, 3]\n", "square_list(my_test_list)" ] }, { "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": [ { "data": { "text/plain": [ "[7, 5, 4, 3, 3, 2]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def descending(list1, list2): \n", " '''merge the elements of two lists in decending order'''\n", " list3 = (list1 +list2)\n", " list3.sort(reverse=True)\n", " return (list3)\n", " \n", "my_first = [2, 4, 3]\n", "my_second = [5, 7, 3]\n", "\n", "descending(my_first, my_second)" ] }, { "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": [ "[1, 2, 3, 4, 5]\n" ] } ], "source": [ "def adding(A, *num): \n", " '''combine the received list and the arbitrary number of integers to a new list \n", " in the order that was given'''\n", " numb = [value for value in num]\n", " my_list = A + numb\n", " print(my_list)\n", "\n", "A = [1, 2, 3]\n", "adding(A, 4,5)" ] }, { "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": [ { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def intersection(list1, list2): \n", " '''receive 2 lists and return one list with the intersection, without duplicates in ascending order'''\n", " my_intersection = [element for element in list1 if element in list2]\n", " return (my_intersection)\n", "\n", "A = [1, 2, 3]\n", "B = [2, 3, 4]\n", "intersection(A, B)" ] }, { "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": [ { "data": { "text/plain": [ "[1, 2, 3, 5]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def union(list1,list2):\n", " '''receive two input lists, return new list with union of \n", " elements without duplicates in ascending order'''\n", " for e in list2:\n", " if e not in list1:\n", " list1.append(e)\n", " list1.sort()\n", " return list1\n", "A = [1, 5, 3]\n", "B = [2, 3, 1]\n", "union(A, B)" ] }, { "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": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 6, 7]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = [1, 2, 3]\n", "B = [2, 3, 4]\n", "C = [6, 7, 4]\n", "\n", "def intersection2(*lists): \n", " '''generalised intersection function: intersection of provided list elements'''\n", " my_intersection = []\n", " my_intersection2 = []\n", " lists_together = [part for part in lists]\n", " for myList in lists_together:\n", " for item in myList:\n", " my_intersection.append(item)\n", " for i in my_intersection: \n", " if i not in my_intersection2: \n", " my_intersection2.append(i)\n", " return (my_intersection2)\n", "\n", "intersection2(A, B, C)" ] }, { "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": 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.12" } }, "nbformat": 4, "nbformat_minor": 4 }