diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..fdd66b8 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -94,12 +94,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "cuadrado = lambda x: x**2\n", + "print(cuadrado(5)) # 25\n" ] }, { @@ -124,12 +134,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "numeros = [1, 2, 3, 4]\n", + "cuadrados = list(map(lambda x: x**2, numeros))\n", + "print(cuadrados) # [1, 4, 9, 16]\n" ] }, { @@ -169,12 +190,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "pares = list(filter(lambda x: x % 2 == 0, numeros))\n", + "print(pares) # [2, 4]\n" ] }, { @@ -209,12 +240,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], "source": [ - "# your code goes here\n" + "# your code goes here\n", + "from functools import reduce\n", + "suma = reduce(lambda a, b: a + b, numeros)\n", + "print(suma) # 10\n" ] }, { @@ -243,14 +285,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Balances negativos: [-26.25, -10.5]\n", + "Total de balances negativos: -36.75\n" + ] + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "# your code goes here\n", + "from functools import reduce\n", + "\n", + "# Ejemplo de balances después de aplicar interés\n", + "balances = [105.0, 52.5, -26.25, 1050.0, -10.5]\n", + "\n", + "# Filtrar los balances negativos\n", + "negative_balances = list(filter(lambda x: x < 0, balances))\n", + "\n", + "# Sumar el total de los negativos\n", + "total_negative = reduce(lambda a, b: a + b, negative_balances)\n", + "\n", + "print(\"Balances negativos:\", negative_balances)\n", + "print(\"Total de balances negativos:\", total_negative)\n" ] }, { @@ -273,10 +337,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "da2264b5-298e-4b45-99df-852b94e90d15", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Balances finales después de retiros: [650, 699, 275]\n" + ] + } + ], "source": [ "accounts = [\n", " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", @@ -284,13 +356,38 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# your code goes here\n", + "# Lista de cuentas con saldo inicial y retiros\n", + "accounts = [\n", + " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", + " {'balance': 2000, 'withdrawals': [300, 1001]},\n", + " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", + "]\n", + "\n", + "# Función que calcula el saldo final después de retiros\n", + "def calculate_balance(account):\n", + " remaining = account['balance'] - sum(account['withdrawals'])\n", + " return remaining\n", + "\n", + "# Usamos map para aplicar la función a todas las cuentas\n", + "final_balances = list(map(calculate_balance, accounts))\n", + "\n", + "print(\"Balances finales después de retiros:\", final_balances)\n", + "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7ec2c11a", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -304,7 +401,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,