diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..b3ac9be 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -8,6 +8,14 @@ "# Lab | Functions" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa9944c1", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "0c581062-8967-4d93-b06e-62833222f930", @@ -43,11 +51,433 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "9aebe77d", + "metadata": {}, + "outputs": [], + "source": [ + "##EJERCICIO 1\n", + "\n", + "products = ['hat', 'skirt', 'shoes','t-shirt', 'car', 'motorcycle', 'socks']\n", + "\n", + "def initialize_inventory(products):\n", + "\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " quantity = int(input(f\"¿Cuántas unidades de {product} hay? \"))\n", + "\n", + " inventory[product] = quantity\n", + "\n", + " return inventory\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "339d1077", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "e0722459", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat': 7,\n", + " 'skirt': 7,\n", + " 'shoes': 7,\n", + " 't-shirt': 7,\n", + " 'car': 7,\n", + " 'motorcycle': 7,\n", + " 'socks': 7}" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "5abba42e", + "metadata": {}, + "outputs": [], + "source": [ + "#EJERCICIO 2\n", + "\n", + "def get_customer_orders():\n", + "\n", + " customer_orders = set()\n", + "\n", + " seguir_comprando = 'yes'\n", + "\n", + " while seguir_comprando == 'yes':\n", + "\n", + " product = input('INTRODUCE QUE QUIERES COMPRAR')\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " seguir_comprando = input('Quieres seguir comprando')\n", + "\n", + "\n", + " return customer_orders\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "4ac0e6de", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "0197a998", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'car', 't-shirt'}" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "67b157a5", + "metadata": {}, + "outputs": [], + "source": [ + "##for p in customer_orders:\n", + "\n", + "## inventory[p] = inventory[p] - 1\n", + "\n", + "#EJERCICIO 3\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + "\n", + " for new_inventory in customer_orders:\n", + "\n", + " inventory[new_inventory] = inventory[new_inventory] - 1\n", + "\n", + " return inventory\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "6dd325c2", + "metadata": {}, + "outputs": [], + "source": [ + "new_inventory = update_inventory(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "9a841fe8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat': 7,\n", + " 'skirt': 7,\n", + " 'shoes': 7,\n", + " 't-shirt': 5,\n", + " 'car': 4,\n", + " 'motorcycle': 7,\n", + " 'socks': 7}" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "7d0f9c0b", + "metadata": {}, + "outputs": [], + "source": [ + "#EJERCICIO 4\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_products_order = len(customer_orders)\n", + "\n", + " unique_products = (customer_orders)\n", + "\n", + " #number_unique = len(set(unique_products))\n", + "\n", + " percentage_unique = total_products_order / len(products) * 100\n", + "\n", + " return total_products_order , percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "8d36fe3e", + "metadata": {}, + "outputs": [], + "source": [ + "percentage_unique = calculate_order_statistics(set(customer_orders), products)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "93e475ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 28.57142857142857)" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "f1fd8536", + "metadata": {}, + "outputs": [], + "source": [ + "#EJERCICIO 5\n", + "\n", + "def print_order_statistics(order_statistics):\n", + "\n", + " print(\"Total de productos pedidos\", order_statistics[0])\n", + "\n", + " print(\"Porcentaje\", order_statistics[1])\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "c95a4195", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total de productos pedidos 7\n", + "Porcentaje 57.14285714285714\n" + ] + } + ], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "cc618757", + "metadata": {}, + "outputs": [], + "source": [ + "#EJERCICIO 6\n", + "\n", + "def print_updated_inventory(inventory):\n", + "\n", + " print(\"Inventario es:\", new_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "8c1bd59b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventario es: {'hat': 5, 'skirt': 5, 'shoes': 3}\n" + ] + } + ], + "source": [ + "new_inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "print_updated_inventory(new_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd003e3e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "daf170ce", + "metadata": {}, + "outputs": [], + "source": [ + "#ESTE CODIGO ES PARA VER EL UN ORDEN CLARO DEL CODIGO, realmente se \n", + "#ponen las funciones al principio.\n", + "\n", + "inventory = {}\n", + "\n", + "def initialize_inventory(products):\n", + " \n", + " products = []\n", + "\n", + " for product in products:\n", + " quantity = int(input(f\"¿Cuántas unidades de {product} hay? \"))\n", + "\n", + " inventory[product] = quantity\n", + "\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + "\n", + " customer_orders = set()\n", + "\n", + " seguir_comprando = 'yes'\n", + "\n", + " while seguir_comprando == 'yes':\n", + "\n", + " product = input('INTRODUCE')\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " seguir_comprando = input('Quieres seguir comprando')\n", + "\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "def update_inventory(customer_order, inventory):\n", + "\n", + " for p in customer_order:\n", + "\n", + " inventory[p] = inventory[p] - 1\n", + "\n", + " return inventory\n", + "\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_products= len(customer_orders)\n", + "\n", + " unique_products = set(customer_orders)\n", + "\n", + " number_unique = len(unique_products)\n", + "\n", + " percentage_unique = number_unique / total_products *100\n", + "\n", + " return total_products , percentage_unique\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "41c85b18", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'shoes'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 7\u001b[39m\n\u001b[32m 3\u001b[39m inventory = initialize_inventory( product)\n\u001b[32m 4\u001b[39m \n\u001b[32m 5\u001b[39m customer_order = get_customer_orders()\n\u001b[32m 6\u001b[39m \n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m update_inventory(customer_order, inventory)\n\u001b[32m 8\u001b[39m \n\u001b[32m 9\u001b[39m calculate_order_statistics(customer_orders, products)\n\u001b[32m 10\u001b[39m \n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[18]\u001b[39m\u001b[32m, line 39\u001b[39m, in \u001b[36mupdate_inventory\u001b[39m\u001b[34m(customer_order, inventory)\u001b[39m\n\u001b[32m 35\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m update_inventory(customer_order, inventory):\n\u001b[32m 36\u001b[39m \n\u001b[32m 37\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;28;01min\u001b[39;00m customer_order:\n\u001b[32m 38\u001b[39m \n\u001b[32m---> \u001b[39m\u001b[32m39\u001b[39m inventory[p] = inventory[p] - \u001b[32m1\u001b[39m\n\u001b[32m 40\u001b[39m \n\u001b[32m 41\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m inventory\n", + "\u001b[31mKeyError\u001b[39m: 'shoes'" + ] + } + ], + "source": [ + "product =[]\n", + "\n", + "inventory = initialize_inventory( product)\n", + "\n", + "customer_order = get_customer_orders()\n", + "\n", + "update_inventory(customer_order, inventory)\n", + "\n", + "calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(new_inventory)\n", + "\n", + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +491,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,