diff --git a/Saske_Assignment_1.ipynb b/Saske_Assignment_1.ipynb new file mode 100644 index 00000000..0ca4afb7 --- /dev/null +++ b/Saske_Assignment_1.ipynb @@ -0,0 +1,1133 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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": "Saske_Assignment_1.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "KeQzbyGAvcUu" + }, + "source": [ + "## First Assignment" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RB3tkFHEvcUw" + }, + "source": [ + "#### 1) Apply the appropriate string methods to the **x** variable (as '.upper') to change it exactly to: \"$Dichlorodiphenyltrichloroethane$\"." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "r5Uh4qhevcUx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6a01a70b-8716-4215-e7bb-e03f3a9a1475" + }, + "source": [ + "x = \"DiClOrod IFeNi lTRicLOr oETaNo DiChlorod iPHeny lTrichL oroEThaNe\"\n", + "x = x.upper()\n", + "x = x.capitalize()\n", + "x = x.replace(\" \", \"\")\n", + "x = x.replace(\" \", \"\")\n", + "print(x)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Diclorodifeniltricloroetanodichlorodiphenyltrichloroethane\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "98b-BlmOvcUy" + }, + "source": [ + "#### 2) Assign respectively the values: 'word', 15, 3.14 and 'list' to variables A, B, C and D in a single line of code. Then, print them in that same order on a single line separated by a space, using only one print statement." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "On0M0d1ZvcUy", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d75c5ae0-fb7c-495e-b905-9074c1c4f8ef" + }, + "source": [ + "A = \"word\"; B = 15; C = 3.14; D = \"list\"\n", + "print(A + \" \" + str(B) + \" \" + str(C) + \" \" + D)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "word 15 3.14 list\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BZwDGHgVvcUy" + }, + "source": [ + "#### 3) Use the **input()** function to receive an input in the form **'68.4 1.71'**, that is, two floating point numbers in a line separated by space. Then, assign these numbers to the variables **w** and **h** respectively, which represent an individual's weight and height (hint: take a look at the '.split()' method). With this data, calculate the individual's Body Mass Index (BMI) from the following relationship: \n", + " \n", + "\\begin{equation}\n", + "BMI = \\dfrac{weight}{height^2}\n", + "\\end{equation}" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oIMGgsvi68qc", + "outputId": "a1f761df-2bc2-4fb7-fcde-f62c7630ec0e" + }, + "source": [ + "print(\"Enter weight and height (seperated by space):\")\n", + "w, h = input().split()\n", + "w = (float(w))\n", + "h = (float(h))\n", + "bmi = w / (h**2)\n", + "print(\"\\nThe resulting BMI is \" + str(bmi))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter weight and height (seperated by space):\n", + "68.4 1.71\n", + "\n", + "The resulting BMI is 23.39181286549708\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eiNrVzl3vcUz" + }, + "source": [ + "#### This value can also be classified according to ranges of values, following to the table below. Use conditional structures to classify and print the classification assigned to the individual. \n", + "\n", + "
<\\center> \n", + "\n", + "\n", + "(source: https://healthtravelguide.com/bmi-calculator/)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TI1JDeODvcUz", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "00ce1827-40da-45b1-97e3-4216fc037e38" + }, + "source": [ + "print(\"Enter weight and height (seperated by space):\")\n", + "w, h = input().split()\n", + "w = (float(w))\n", + "h = (float(h))\n", + "bmi = w / (h**2)\n", + "\n", + "if bmi < 18.5 :\n", + " print(\"Underweight\")\n", + "elif bmi >= 18.5 and bmi <= 24.9 :\n", + " print(\"Normal weight\")\n", + "elif bmi >= 25 and bmi <= 29.9 :\n", + " print(\"Pre-obesity\")\n", + "elif bmi >= 30 and bmi <= 34.9 :\n", + " print(\"Obesity class I\")\n", + "elif bmi >= 35 and bmi <= 39.9 :\n", + " print(\"Obesity class II\")\n", + "else :\n", + " print(\"Obesity class III\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter weight and height (seperated by space):\n", + "68.4 1.71\n", + "Normal weight\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jiJT6bgIvcUz" + }, + "source": [ + "#### 4) Receive an integer as an input and, using a loop, calculate the factorial of this number, that is, the product of all the integers from one to the number provided. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "G6_1pOCcvcU0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "e49df5a5-9141-4f78-dec3-8b336bbbc0b5" + }, + "source": [ + "print(\"Enter any number to calculate its factorial:\")\n", + "zahl = int(input())\n", + "factorial = 1\n", + "\n", + "if zahl > 0 :\n", + " for i in range(1, zahl+1):\n", + " factorial = factorial*i\n", + " print(\"calculating...\" + \"\\n\" + \"The factorial of \" + str(zahl) + \" is \" + str(factorial))\n", + "else :\n", + " print (\"calculation of factorial not possible\")\n", + " " + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter any number to calculate its factorial:\n", + "4\n", + "calculating...\n", + "The factorial of 4 is 1\n", + "calculating...\n", + "The factorial of 4 is 2\n", + "calculating...\n", + "The factorial of 4 is 6\n", + "calculating...\n", + "The factorial of 4 is 24\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PlX6wWj4vcU0" + }, + "source": [ + "#### 5) Using a while loop and the input function, read an indefinite number of integers until the number read is -1. Present the sum of all these numbers in the form of a print, excluding the -1 read at the end. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "a0MeQAbSvcU0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f5576eab-ef22-45e0-d1ef-708268ed2563" + }, + "source": [ + "print(\"Enter a number of integers:\")\n", + "a_num = int(input())\n", + "total = 0\n", + "\n", + "while a_num != -1:\n", + " total = total + a_num\n", + " a_num = int(input())\n", + "print(total)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number of integers:\n", + "45\n", + "76\n", + "4\n", + "8\n", + "3\n", + "-1\n", + "136\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wWMEbZfhvcU0" + }, + "source": [ + "#### 6) Read the **first name** of an employee, his **amount of hours worked** and the **salary per hour** in a single line separated by commas. Next, calculate the **total salary** for this employee and show it to two decimal places." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hGkOd7DHvcU0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6a89fe63-261e-4f13-f364-6fcc2c4b272c" + }, + "source": [ + "pers_x = {\"first name\":\"Max Müller\", \"amount of hours worked\":520, \"salary per hour\":12.45}\n", + "total_salary = pers_x[\"amount of hours worked\"] * pers_x[\"salary per hour\"]\n", + "ts_fo = \"{:.2f}\".format(total_salary)\n", + "print(\"The total salary of \" + pers_x[\"first name\"] + \" is \" + str(ts_fo))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The total salary of Max Müller is 6474.00\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b1mO-XzBvcU1" + }, + "source": [ + "#### 7) Read three floating point values **A**, **B** and **C** respectively. Then calculate itens a, b, c, d and e: " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Fz5d3ERnvcU1", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6c657473-a3fc-460f-e049-20ae42e69dbe" + }, + "source": [ + "print(\"Enter values to define A, B and C:\")\n", + "A = float(input())\n", + "B = float(input())\n", + "C = float(input())" + ], + "execution_count": null, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter values to define A, B and C:\n", + "4\n", + "8\n", + "5\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_zoBVkoEvcU1" + }, + "source": [ + " a) the area of the triangle rectangle with A as the base and C as the height." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YjFQAMJGvcU1", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5ced99a8-b48d-4e0e-c509-89cfedde5feb" + }, + "source": [ + "tri = (C*A)/2\n", + "print(\"The area of the triangle is \" + str(tri))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the triangle is 10.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mSgNodjyvcU2" + }, + "source": [ + " b) the area of the circle of radius C. (pi = 3.14159) " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "00bWSM70vcU2", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "72888c0f-818d-457c-e916-f35d1547a0ba" + }, + "source": [ + "circ = 3.14159*(C**2)\n", + "print(\"The area of the circle is \" + str(circ))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the circle is 78.53975\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sk_d0LmtvcU2" + }, + "source": [ + " c) the area of the trapezoid that has A and B for bases and C for height. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-eh8FsxNvcU2", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "806737c6-f647-4270-d8f4-8c1190ea3675" + }, + "source": [ + "trape = ((A+B)/2)*C\n", + "print(\"The area of the trapezoid is \" + str(trape))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the trapezoid is 30.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NvuWzKthvcU2" + }, + "source": [ + " d) the area of the square that has side B. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qXxbE0VavcU2", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "cae1bdaa-98bb-4b08-ab3c-ad6f686548d0" + }, + "source": [ + "squ = B**2\n", + "print(\"The area of the square is \" + str(squ))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the square is 64.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ukAha4IqvcU2" + }, + "source": [ + " e) the area of the rectangle that has sides A and B. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8bxmT6UNvcU3", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "cf8e2c1a-7b29-4ee2-eb6f-61665fd958f3" + }, + "source": [ + "rec = A * B\n", + "print(\"The area of the rectangle is \" + str(rec))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The area of the rectangle is 32.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sH8p8VEHvcU3" + }, + "source": [ + "#### 8) Read **the values a, b and c** and calculate the **roots of the second degree equation** $ax^{2}+bx+c=0$ using [this formula](https://en.wikipedia.org/wiki/Quadratic_equation). If it is not possible to calculate the roots, display the message **“There are no real roots”**. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "v8X51hlavcU3", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "264b634d-ae83-41c6-e083-d65b95bcb257" + }, + "source": [ + "print(\"Enter values to define a, b and c:\")\n", + "a = float(input())\n", + "b = float(input())\n", + "c = float(input())\n", + "\n", + "import cmath\n", + "discr = (b**2) - (4*a*c)\n", + "\n", + "root1 = (-b-cmath.sqrt(discr))/(2*a)\n", + "root2 = (-b+cmath.sqrt(discr))/(2*a)\n", + "\n", + "if discr > 0 :\n", + " print(\"The roots are \" + str(root1) + \" \" + str(root2))\n", + "else :\n", + " print(\"There are no real roots\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter values to define a, b and c:\n", + "1\n", + "7\n", + "7\n", + "The roots are (-5.7912878474779195+0j) (-1.20871215252208+0j)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x9tSXPLOvcU3" + }, + "source": [ + "#### 9) Read four floating point numerical values corresponding to the coordinates of two geographical coordinates in the cartesian plane. Each point will come in a line with its coordinates separated by space. Then calculate and show the distance between these two points. \n", + "\n", + "(obs: $d=\\sqrt{(x_1-x_2)^2 + (y_1-y_2)^2}$)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "OGajl8GdvcU3", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d448e5de-e08f-443e-995a-abd5072510bb" + }, + "source": [ + "print(\"Enter four coordinates for two geographical points:\")\n", + "x_1 = float(input())\n", + "x_2 = float(input())\n", + "y_1 = float(input())\n", + "y_2 = float(input())\n", + "\n", + "print(\"The coordinates are:\" + \"\\n\" + str(x_1) + \"°N \" + str(x_2) + \"°E\\n\" + str(y_1) + \"°N \" + str(y_2) + \"°E\")\n", + "\n", + "dis = cmath.sqrt((x_1 - x_2)**2 + (y_1 - y_2)**2)\n", + "print(\"The distance between these points is \" + str(dis))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter four coordinates for two geographical points:\n", + "48.2084\n", + "16.3735\n", + "51.1662\n", + "13.4715\n", + "The coordinates are:\n", + "48.2084°N 16.3735°E\n", + "51.1662°N 13.4715°E\n", + "The distance between these points is (49.33914537261464+0j)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0vVATFg_vcU4" + }, + "source": [ + "#### 10) Read **two floating point numbers** on a line that represent **coordinates of a cartesian point**. With this, use **conditional structures** to determine if you are at the origin, printing the message **'origin'**; in one of the axes, printing **'x axis'** or **'y axis'**; or in one of the four quadrants, printing **'q1'**, **'q2**', **'q3'** or **'q4'**. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WQLKSW6AvcU4", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "aff1be85-542f-4d91-8913-b245353d6387" + }, + "source": [ + "print(\"Enter two coordinates seperated by space: \")\n", + "X, Y = input().split()\n", + "X = (float(X))\n", + "Y = (float(Y))\n", + "\n", + "print(\"This point's location is:\")\n", + "if X > 0 and Y > 0 :\n", + " print(\"q1\")\n", + "if X < 0 and Y > 0 :\n", + " print(\"q2\")\n", + "if X < 0 and Y < 0 :\n", + " print(\"q3\")\n", + "if X > 0 and Y < 0 :\n", + " print(\"q4\")\n", + "if X == 0 and Y != 0 :\n", + " print(\"y-axis\")\n", + "if X != 0 and Y == 0 :\n", + " print(\"x-axis\")\n", + "if X == 0 and Y == 0 :\n", + " print(\"origin\") " + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter two coordinates seperated by space: \n", + "0 0\n", + "This point's location is:\n", + "origin\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IAcVAqY2vcU5" + }, + "source": [ + "11) Read an integer that represents a phone code for international dialing. \n", + "#### Then, inform to which country the code belongs to, considering the generated table below:\n", + "(You just need to consider the first 10 entries) " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fT3Zk2ZsvcU5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 669 + }, + "outputId": "f7e96cc7-ac20-4325-920e-b708ea309f6c" + }, + "source": [ + "import pandas as pd\n", + "df = pd.read_html('https://en.wikipedia.org/wiki/Telephone_numbers_in_Europe')[1]\n", + "df = df.iloc[:,:2]\n", + "df.head(20)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CountryCountry calling code
0Austria43
1Belgium32
2Bulgaria359
3Croatia385
4Cyprus357
5Czech Republic420
6Denmark45
7Estonia372
8Finland358
9France33
10Germany49
11Greece30
12Hungary36
13Iceland354
14Ireland353
15Italy39
16Latvia371
17Liechtenstein423
18Lithuania370
19Luxembourg352
\n", + "
" + ], + "text/plain": [ + " Country Country calling code\n", + "0 Austria 43\n", + "1 Belgium 32\n", + "2 Bulgaria 359\n", + "3 Croatia 385\n", + "4 Cyprus 357\n", + "5 Czech Republic 420\n", + "6 Denmark 45\n", + "7 Estonia 372\n", + "8 Finland 358\n", + "9 France 33\n", + "10 Germany 49\n", + "11 Greece 30\n", + "12 Hungary 36\n", + "13 Iceland 354\n", + "14 Ireland 353\n", + "15 Italy 39\n", + "16 Latvia 371\n", + "17 Liechtenstein 423\n", + "18 Lithuania 370\n", + "19 Luxembourg 352" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "B2PvwiTkvcU5", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "b052f793-0faa-4f6a-e544-74a787db4be9" + }, + "source": [ + "print(\"Enter a country calling code:\")\n", + "ccd = str(input())\n", + "\n", + "check = df[df[\"Country calling code\"]==ccd]\n", + "\n", + "print(\"\\nThis country calling code belongs to\\n\" + str(check[\"Country\"]))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a country calling code:\n", + "43\n", + "\n", + "This country calling code belongs to\n", + "0 Austria\n", + "Name: Country, dtype: object\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1sBj1OlKvcU5" + }, + "source": [ + "#### 12) Write a piece of code that reads 6 numbers in a row. Next, show the number of positive values entered. On the next line, print the average of the values to one decimal place. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9gXfG-YXvcU5", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "94afd4ff-981f-4c94-ff11-d023bd285b85" + }, + "source": [ + "print(\"Enter 6 numbers: \")\n", + "p = float(input()); o = float(input()); u = float(input()); t = float(input()); r = float(input()); q = float(input())\n", + "nums = [o, p, u, t, r, q]\n", + "aver = (sum(nums))/6\n", + "\n", + "start = 0\n", + "if o > 0 :\n", + " start = start + 1\n", + "if p > 0 :\n", + " start = start + 1\n", + "if u > 0 :\n", + " start = start + 1\n", + "if t > 0 :\n", + " start = start + 1\n", + "if r > 0 :\n", + " start = start + 1\n", + "if q > 0 :\n", + " start = start + 1\n", + "\n", + "print(\"The number of positive values entered is \" + (str(start)))\n", + "print(\"The average of the values is \" + str(aver))\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter 6 numbers: \n", + "3\n", + "5\n", + "2\n", + "5\n", + "-323\n", + "4\n", + "The number of positive values entered is 5\n", + "The average of the values is -50.666666666666664\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kuktgnaTvcU6" + }, + "source": [ + "#### 13) Read an integer **N**. Then print the **square of each of the even values**, from 1 to N, including N, if applicable, arranged one per line. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9ehs97v7vcU6", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "dce86d7f-d699-44a0-dcb0-b93a0d661f9e" + }, + "source": [ + "print(\"Enter an integer:\")\n", + "N = int(input())\n", + "\n", + "sqr = []\n", + "for i in range (1, N):\n", + " if (i % 2) == 0:\n", + " sqr.append(i*i)\n", + "if (N % 2) == 0:\n", + " sqr.append(N*N)\n", + "print(\"square output:\")\n", + "for item in sqr:\n", + " print(item)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter an integer:\n", + "21\n", + "square output:\n", + "4\n", + "16\n", + "36\n", + "64\n", + "100\n", + "144\n", + "196\n", + "256\n", + "324\n", + "400\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UQ0t2WFCvcU6" + }, + "source": [ + "#### 14) Using **input()**, read an integer and print its classification as **'even / odd'** and **'positive / negative'** . The two classes for the number must be printed on the same line separated by a space. In the case of zero, print only **'null'**. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Xf52BjV3vcU7", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "2c44b576-2809-4d08-8b95-2b1c220eca76" + }, + "source": [ + "z = input(\"Enter a number: \")\n", + "z = int(z)\n", + "\n", + "if (z % 2) == 0 and z != 0 :\n", + " print(\"This number is even.\")\n", + "elif z == 0 :\n", + " print(\"null\")\n", + "else :\n", + " print(\"This number is odd.\")\n", + "\n", + "if z > 0 :\n", + " print(\"This number is positive.\")\n", + "elif z == 0:\n", + " print(\"null\")\n", + "else :\n", + " print(\"This number is negative.\")\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 65764356\n", + "This number is even.\n", + "This number is positive.\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2xm2DT-avcU7" + }, + "source": [ + "## Challenge\n", + "#### 15) Ordering problems are recurrent in the history of programming. Over time, several algorithms have been developed to fulfill this function. The simplest of these algorithms is the [**Bubble Sort**](https://en.wikipedia.org/wiki/Bubble_sort), which is based on comparisons of elements two by two in a loop of passes through the elements. Your mission, if you decide to accept it, will be to input six whole numbers ramdonly ordered. Then implement the **Bubble Sort** principle to order these six numbers **using only loops and conditionals**. \n", + "#### At the end, print the six numbers in ascending order on a single line separated by spaces. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yxQqXUW4vcU8", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4b984142-2096-4fef-b062-e8f22b7e1fad" + }, + "source": [ + "str_inp = input(\"Enter six numbers seperated by spaces:\\n\")\n", + "nums= [int(x) for x in str_inp.split()]\n", + "\n", + "count = len(nums)\n", + "\n", + "for outer in range(count - 1):\n", + " for i in range(count - outer - 1):\n", + " if nums[i] > nums[i + 1]:\n", + " nums[i],nums[i + 1] = nums[i + 1],nums[i]\n", + "res = str(nums)\n", + "res = res.replace(\",\", \" \")\n", + "print(\"Numbers sorted in ascending order:\\n\" + str(res))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter six numbers seperated by spaces:\n", + "4 -76 3 0 -333 2\n", + "Numbers sorted in ascending order:\n", + "[-333 -76 0 2 3 4]\n" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/Saske_Assignment_2.ipynb b/Saske_Assignment_2.ipynb new file mode 100644 index 00000000..27fef374 --- /dev/null +++ b/Saske_Assignment_2.ipynb @@ -0,0 +1,636 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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": "Saske_Assignment_2.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Zo3tk_Hx8tpJ" + }, + "source": [ + "## Second Assignment" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WPG_8rz28tpM" + }, + "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", + "metadata": { + "id": "WtpWU45i8tpO", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f983792f-9bf2-4c62-c3f7-458a2a7a2258" + }, + "source": [ + "def even_squared():\n", + " print(\"Enter an integer x:\")\n", + " N = int(input())\n", + " n_list = []\n", + "\n", + " for i in range (1, N, 1):\n", + " if (i % 2) == 0:\n", + " n_list.append(i*i)\n", + " if (N % 2) == 0:\n", + " n_list.append(N*N)\n", + " print(\"The even squares from 1 to x are:\")\n", + " print(n_list)\n", + "\n", + "even_squared()\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter an integer x:\n", + "8\n", + "The even squares from 1 to x are:\n", + "[4, 16, 36, 64]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kqtGo8RP8tpP" + }, + "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", + "metadata": { + "id": "lJnT-8be8tpQ", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "673e239e-7703-48d3-bffd-d5b990804869" + }, + "source": [ + "print(\"Enter a number of integers:\")\n", + "ev = []\n", + "od = []\n", + "num = int(input())\n", + "\n", + "while num != -1:\n", + " num = int(input())\n", + " if (num % 2) == 0:\n", + " ev.append(num)\n", + " else:\n", + " od.append(num)\n", + "print(\"The even integers are \" + str(ev))\n", + "print(\"The odd integers are \" + str(od))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number of integers:\n", + "44\n", + "23\n", + "4\n", + "7\n", + "-23\n", + "5\n", + "2\n", + "-1\n", + "The even integers are [4, 2]\n", + "The odd integers are [23, 7, -23, 5, -1]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "K7LRFtmt8tpQ" + }, + "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", + "metadata": { + "id": "ylBcS6uJ8tpR", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "16e8f650-11ea-4ae3-a6ff-caebe0b5884f" + }, + "source": [ + "def even_account():\n", + " print(\"Enter a number of integers:\")\n", + " evac = [int(x) for x in input().split()]\n", + " count = 0\n", + "\n", + " for i in evac: \n", + " if (i % 2) == 0:\n", + " count = count + 1\n", + " \n", + " print(\"The count of existing even elements is: \")\n", + " return count\n", + "\n", + "even_account()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number of integers:\n", + "4 5 32 5 3 \n", + "The count of existing even elements is: \n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "execution_count": 49 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lKdF5LpQ8tpS" + }, + "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", + "metadata": { + "id": "uHNAO-3K8tpS", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "1bf1de73-a540-431f-f56c-9ff02516ce1b" + }, + "source": [ + "def squared_list():\n", + " print(\"Enter a number of integers:\")\n", + " li = [int(x) for x in input().split()]\n", + " sq_li = []\n", + "\n", + " for i in li:\n", + " sq_i = [i**2]\n", + " sq_li.append(sq_i)\n", + "\n", + " print(\"The squares of these elements are:\")\n", + " return sq_li\n", + "\n", + "squared_list()\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number of integers:\n", + "4 23 4\n", + "The squares of these elements are:\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[16], [529], [16]]" + ] + }, + "metadata": {}, + "execution_count": 51 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gDnqEw288tpT" + }, + "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", + "metadata": { + "id": "W8ecboay8tpU", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bafe5995-52fc-40d2-b782-4a97ca550ad6" + }, + "source": [ + "def descending():\n", + " print(\"Enter two lists consisting of integers seperated by spaces:\")\n", + " des1 = [int(x) for x in input().split()]\n", + " des2 = [int(x) for x in input().split()]\n", + " des_join = des1 + des2\n", + " des_join.sort(reverse=True)\n", + " print(\"Here are all integers in descending order:\")\n", + " return des_join\n", + "\n", + "descending()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter two lists consisting of integers seperated by spaces:\n", + "22 8 2 3 -43 3\n", + "43 -43 22 6 7 77\n", + "Here are all integers in descending order:\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[77, 43, 22, 22, 8, 7, 6, 3, 3, 2, -43, -43]" + ] + }, + "metadata": {}, + "execution_count": 55 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UeTIQ1Nt8tpU" + }, + "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", + "metadata": { + "id": "FFZxfnYD8tpV", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4629efe4-f2a8-4f61-bf9b-c614f830273a" + }, + "source": [ + "def adding():\n", + " print(\"Enter a list of integers:\")\n", + " ints = [int(x) for x in input().split()]\n", + " A = [11, 17, 23]\n", + "\n", + " intsa = A + ints\n", + " print(\"Here are list A and the integers joined together:\")\n", + " return intsa\n", + "\n", + "adding()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a list of integers:\n", + "2 4 298 3 2\n", + "Here are list A and the integers joined together:\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[11, 17, 23, 2, 4, 298, 3, 2]" + ] + }, + "metadata": {}, + "execution_count": 57 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rfT_6Lgq8tpW" + }, + "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", + "metadata": { + "id": "LI2HGtm18tpX", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4a74701b-b383-4aea-9fd2-11204e1ed881" + }, + "source": [ + "def intersection():\n", + " print(\"Enter two lists consisting of integers seperated by spaces:\")\n", + " C = [int(x) for x in input().split()]\n", + " D = [int(x) for x in input().split()]\n", + "\n", + " E = [value for value in C if value in D]\n", + " print(\"Here is a list of the intersecting values:\")\n", + " return E\n", + "\n", + "intersection()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter two lists consisting of integers seperated by spaces:\n", + "1 2 3 4\n", + "4 5 6\n", + "Here is a list of the intersecting values:\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[4]" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S6P-P9U38tpX" + }, + "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", + "metadata": { + "id": "O-K1JqI-8tpY", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "1c41f344-d952-4da6-ec19-1b247ad2a3ac" + }, + "source": [ + "def union():\n", + " print(\"Enter two lists consisting of integers seperated by spaces:\")\n", + " E = [int(x) for x in input().split()]\n", + " F = [int(x) for x in input().split()]\n", + "\n", + " EF = sorted(E + F)\n", + " EF_nodupl = []\n", + " [EF_nodupl.append(x) for x in EF if x not in EF_nodupl]\n", + " print(\"Here is a combined list without duplicates:\")\n", + " return EF_nodupl\n", + "\n", + "union()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter two lists consisting of integers seperated by spaces:\n", + "4 3 -33 5 6 4\n", + "5 43 -332 -33 56\n", + "Here is a combined list without duplicates:\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[-332, -33, 3, 4, 5, 6, 43, 56]" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OZCrbep28tpZ" + }, + "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", + "metadata": { + "id": "rp8csrQi8tpZ", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 745 + }, + "outputId": "d202fa3d-4264-49bf-8919-10a5495b8d33" + }, + "source": [ + "def intersection2 ():\n", + " print(\"Enter two lists consisting of integers seperated by spaces:\")\n", + " C = [int(x) for x in input().split()]\n", + " D = [int(x) for x in input().split()]\n", + "\n", + " E = [value for value in C if value in D]\n", + " print(\"Here is a list of the intersecting values:\")\n", + " print(str(E))\n", + " while 1 == 1:\n", + " print(\"Enter another list:\")\n", + " G = [int(x) for x in input().split()]\n", + " E = [value for value in E if value in G]\n", + " print(str(E))\n", + "\n", + "intersection2()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter two lists consisting of integers seperated by spaces:\n", + "1 2 3 4 5 6 7\n", + "3 4 52 2 4 1 \n", + "Here is a list of the intersecting values:\n", + "[1, 2, 3, 4]\n", + "Enter another list:\n", + "4 5 3 2 6 1 3\n", + "[1, 2, 3, 4]\n", + "Enter another list:\n", + "2 3 5 7\n", + "[2, 3]\n", + "Enter another list:\n" + ] + }, + { + "output_type": "error", + "ename": "KeyboardInterrupt", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 728\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 729\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 730\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 624\u001b[0m \"\"\"\n\u001b[0;32m--> 625\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 626\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mintersection2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mintersection2\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter another list:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mG\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mE\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mE\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mG\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 704\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 705\u001b[0m )\n\u001b[1;32m 706\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 732\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 735\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 736\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AQGqzkeW8tpZ" + }, + "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", + "metadata": { + "id": "SQsmlh1C8tpa", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d3d98e76-5534-4aa8-8076-93e0ffd4f903" + }, + "source": [ + "def matrix():\n", + " matrix_A = [[17, 3, 99], [28, 7, 98], [8, 4, 0]]\n", + " matrix_B = [[15, 4, 98], [25, 9, 94], [25, 6, 99]]\n", + "\n", + " AxB = [[0,0,0], [0,0,0], [0,0,0]]\n", + "\n", + " for i in range(len(matrix_A)):\n", + " for j in range(len(matrix_B[0])):\n", + " for k in range(len(matrix_B)):\n", + " AxB[i][j] += matrix_A[i][k] * matrix_B[k][j]\n", + " return AxB\n", + "\n", + "matrix()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[2805, 689, 11749], [3045, 763, 13104], [220, 68, 1160]]" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + } + ] +} \ No newline at end of file