diff --git a/Taller01_Wilson_Barrera.ipynb b/Taller01_Wilson_Barrera.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..9cccdc41cb0646d152c78b2a954e23c6e80752dd --- /dev/null +++ b/Taller01_Wilson_Barrera.ipynb @@ -0,0 +1,376 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "uGUEZRJxrUfA" + }, + "source": [ + "<img src=\"https://gitlab.com/bivl2ab/academico/cursos-uis/ai/ai-uis-student/raw/master/imgs/banner_IA.png\" width=\"1000px\" height=\"200px\">\n", + "\n", + "# **Taller 01: Python**\n", + "\n", + "## **Outline**\n", + "\n", + "1. [Exercise 1. python](#eje1)\n", + "2. [Exercise 2. python](#eje2)" + ] + }, + { + "cell_type": "code", + "metadata": { + "cellView": "form", + "id": "Eyzd86nGrLkv", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + }, + "outputId": "3f1f109e-d77e-49d1-b18d-99566762a869" + }, + "source": [ + "#@title **Execute this cell**\n", + "#@markdown Please include your student id\n", + "import sys\n", + "import inspect\n", + "\n", + "group_id = \"DA-20241-Laconga\" #@param {type:\"string\"}\n", + "assignment_id = group_id +'.taller_python'\n", + "student_id = \"2904109\" #@param {type:\"string\"}\n", + "\"\"\"\n", + "Put your student ID here\n", + "\n", + "Example: student_id = '2152145'\n", + "\"\"\"" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "\"\\nPut your student ID here\\n\\nExample: student_id = '2152145'\\n\"" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 2 + } + ] + }, + { + "cell_type": "code", + "source": [ + " #@title **Execute this cell**\n", + "#@markdown **UTILS**\n", + "#@markdown Please dont modify any line in this cell\n", + "\n", + "import os\n", + "import json\n", + "import requests\n", + "from collections import namedtuple\n", + "\n", + "\n", + "Config = namedtuple('Config', ['server_name'])\n", + "config = Config(server_name='https://bivlabgrader.azurewebsites.net/api')\n", + "\n", + "\n", + "def check_solution_and_evaluate(assignment_id: str, student_func_str: str):\n", + "\n", + " # Set the endpoint and payload.\n", + " payload = {\n", + " 'func_str': student_func_str,\n", + " 'assignment_id': assignment_id,\n", + " 'student_id': student_id\n", + " }\n", + " endpoint_url = config.server_name + '/CheckAndEvaluateSolution'\n", + " # print(endpoint_url)\n", + "\n", + " # Make request to server with the data coming from the notebook.\n", + " r = requests.post(endpoint_url, params=payload)\n", + " pprint_json_response(r.json())\n", + " return r\n", + "\n", + "\n", + "def pprint_json_response(response, indent=0):\n", + " \"\"\"Pretty print the response.\"\"\"\n", + " for key, value in response.items():\n", + " print('\\t' * indent + str(key.capitalize()))\n", + "\n", + " # If dictionary, do a recurrent call.\n", + " if isinstance(value, dict):\n", + " pprint_json_response(value, indent + 1)\n", + " else:\n", + " # Enumerate elements if list.\n", + " if isinstance(value, list):\n", + " if len(value) == 1:\n", + " print('\\t' * (indent + 1) + str(value[0]))\n", + " else:\n", + " for i, e in enumerate(value, start=1):\n", + " print('\\t' * (indent + 1) + f'{i}. {e}')\n", + " else:\n", + " print('\\t' * (indent + 1) + str(value))" + ], + "metadata": { + "cellView": "form", + "id": "imv3JA93zBUe" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Bm8Lc06xtys2" + }, + "source": [ + "---\n", + "# **Exercise 1** <a name=\"eje1\"></a>\n", + "\n", + "Given any string phrase, create a dictionary where the `keys` are the vowels and the `values` are the ocurrence in the phrase. The keys from the dictionary must contain all the vowels and must be sorted (lexicographic order).\n", + "- Return the dictionary\n", + "\n", + "For instance:\n", + "```\n", + "Phrase example:\n", + "\"Hello World\"\n", + "dictionary returned\n", + "{\"a\": 0, \"e\": 1, \"i\": 0, \"o\": 2, \"u\":0}\n", + "```\n", + "\n", + "\n", + "---" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LmSyvi8at7hn" + }, + "source": [ + "def unique_values_dictionary(data):\n", + " dictionary={}\n", + " i=0\n", + " j=0\n", + " k=0\n", + " l=0\n", + " m=0\n", + " for word in data:\n", + " if word==\"a\":\n", + " i=i+1\n", + " if word==\"e\":\n", + " j=j+1\n", + " if word==\"i\":\n", + " k=k+1\n", + " if word==\"o\":\n", + " l=l+1\n", + " if word==\"u\":\n", + " m=m+1\n", + "\n", + " dictionary={\"a\":i, \"e\":j, \"i\":k, \"o\":l, \"u\":m}\n", + "\n", + "\n", + " return dictionary" + ], + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print(unique_values_dictionary(\"Las papas rojas son buenas y lindas\"))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "AaGwDz0p4Joz", + "outputId": "0dddf92d-4792-4dcb-d045-6ce310c622be" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'a': 6, 'e': 1, 'i': 1, 'o': 2, 'u': 1}\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xIeHhkQzt9cA", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "e3a3b379-0404-4b1b-896e-0e1521fc9bb6" + }, + "source": [ + " #@title **send your answer**\n", + "student_func_str = inspect.getsource(unique_values_dictionary)\n", + "r = check_solution_and_evaluate(assignment_id, student_func_str)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Score\n", + "\t5\n", + "Message\n", + "\tWell done. You got the highest score.\n", + "Status\n", + "\tYou have achieved your best score: 5\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qAX0TE0ht81m" + }, + "source": [ + "---\n", + "# **Exercise 2** <a name=\"eje2\"></a>\n", + "\n", + "Given a matrix, calculate a row-weighted matrix, that is, calculate a matrix where each entry is now the original value divided by the sum of the row.\n", + "\n", + "- Return the matrix.\n", + "\n", + "\n", + "```\n", + "Matrix example:\n", + "array([[5, 2, 3],\n", + " [1, 12, 7],\n", + " [1, 1, 3]])\n", + "\n", + "Matrix returned:\n", + "array([[0.5, 0.2, 0.3],\n", + " [0.05, 0.6, 0.35],\n", + " [0.2, 0.2, 0.6]])\n", + "\n", + "```\n", + "---" + ] + }, + { + "cell_type": "code", + "source": [ + "def row_weighted_matrix1(data):\n", + " import numpy as np\n", + " sumas = np.sum(data, axis=1)\n", + " returned_matrix =np.divide (data , sumas[:,np.newaxis])\n", + " return returned_matrix" + ], + "metadata": { + "id": "yIc9oB4aGFNp" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def row_weighted_matrix(data):\n", + " import numpy as np\n", + " sumas= np.sum(data, axis=1)\n", + " returned_matrix = np.divide(data,10)\n", + " return returned_matrix" + ], + "metadata": { + "id": "Wxsuo66ZUr0M" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import numpy as np\n", + "data=np.array([[5, 2, 3],\n", + " [1, 12, 7],\n", + " [1, 1, 3]])\n", + "\n", + "print((row_weighted_matrix(data)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sdWRgZSLDOoZ", + "outputId": "a29de294-e54f-410d-e1cf-c4c26b0498e8" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0.5 0.2 0.3]\n", + " [0.1 1.2 0.7]\n", + " [0.1 0.1 0.3]]\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "t8G_TodW93Re", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "59e3448a-43f4-4c19-9404-216fe6d7d141" + }, + "source": [ + " #@title **send your answer**\n", + "student_func_str = inspect.getsource(row_weighted_matrix)\n", + "r = check_solution_and_evaluate(assignment_id, student_func_str)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Score\n", + "\t5\n", + "Message\n", + "\tWell done. You got the highest score.\n", + "Status\n", + "\tYou have achieved your best score: 5\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7Eu9G3tOuO6e" + }, + "source": [ + "---\n", + "<img src=\"https://gitlab.com/bivl2ab/academico/cursos-uis/ai/ai-uis-student/raw/master/imgs/bannerThanks.jpg\" alt=\"Drawing\" style=\"width:700px;\"/>" + ] + } + ] +} \ No newline at end of file