From 402217bb2de3eb16ffb0b2e406242ff6b826e126 Mon Sep 17 00:00:00 2001 From: PabloXberg Date: Tue, 24 Feb 2026 15:03:38 +0100 Subject: [PATCH] Extra Lab 8 - LAMBDA please repeat. --- lab-python-lambda-map-reduce-filter.ipynb | 164 +++++++++++++++++++--- 1 file changed, 145 insertions(+), 19 deletions(-) diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..1eb96dd 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -94,12 +94,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Debits found:\n", + "[(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "transactions = [\n", + " (-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), \n", + " (850, 'credit'), (-250, 'debit'), (1500, 'credit'), \n", + " (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')\n", + "]\n", + "\n", + "# Using filter() with a lambda function\n", + "# The lambda takes 'x' (a tuple) and checks if x[1] is 'debit'\n", + "debits = list(filter(lambda x: x[1] == 'debit', transactions))\n", + "\n", + "print(\"Debits found:\")\n", + "print(debits)" ] }, { @@ -127,9 +147,26 @@ "execution_count": null, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Debits sorted in descending order (closest to 0 first):\n", + "[(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "# 1. Define the lambda to extract the amount for sorting\n", + "# Note: In Python 3, sorted() uses 'key' to determine the sorting value.\n", + "sort_key = lambda x: x[0]\n", + "\n", + "# 2. Use sorted() with the key and reverse=True for descending order\n", + "sorted_debits = sorted(debits, key=sort_key, reverse=True)\n", + "\n", + "print(\"Debits sorted in descending order (closest to 0 first):\")\n", + "print(sorted_debits)" ] }, { @@ -169,12 +206,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Balances after one year of interest:\n", + "[105.0, 52.5, -26.25, 1050.0, -10.5]\n" + ] + } + ], "source": [ - "# your code goes here" + "# Initial list of bank account balances\n", + "balances = [100, 50, -25, 1000, -10]\n", + "\n", + "# Interest rate\n", + "interest_rate = 0.05\n", + "\n", + "# 1. Use map() with a lambda function\n", + "# The lambda takes 'b' (balance) and multiplies it by 1.05\n", + "new_balances = list(map(lambda b: b * (1 + interest_rate), balances))\n", + "\n", + "print(\"Balances after one year of interest:\")\n", + "print(new_balances)" ] }, { @@ -209,12 +266,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Accounts:\n", + "{'balance': 1020.0, 'interest_rate': 0.02}\n", + "{'balance': 2020.0, 'interest_rate': 0.01}\n", + "{'balance': 515.0, 'interest_rate': 0.03}\n" + ] + } + ], "source": [ - "# your code goes here\n" + "accounts = [\n", + " {'balance': 1000, 'interest_rate': 0.02},\n", + " {'balance': 2000, 'interest_rate': 0.01},\n", + " {'balance': 500, 'interest_rate': 0.03},\n", + "]\n", + "\n", + "# The lambda takes 'acc' (the dictionary)\n", + "# It returns a NEW dictionary with the updated balance\n", + "updated_accounts = list(map(lambda acc: {\n", + " 'balance': acc['balance'] * (1 + acc['interest_rate']),\n", + " 'interest_rate': acc['interest_rate']\n", + "}, accounts))\n", + "\n", + "print(\"Updated Accounts:\")\n", + "for account in updated_accounts:\n", + " print(account)" ] }, { @@ -243,14 +326,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Negative balances found: [-26.25, -10.5]\n", + "Total amount of debt: -36.75\n" + ] + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "# Result from Challenge 2 Exercise 1 (balances after 0.05 interest)\n", + "# balances was [100, 50, -25, 1000, -10]\n", + "# after 1.05 interest: [105.0, 52.5, -26.25, 1050.0, -10.5]\n", + "new_balances = [105.0, 52.5, -26.25, 1050.0, -10.5]\n", + "\n", + "# 1. Filter the negative balances\n", + "negative_balances = list(filter(lambda x: x < 0, new_balances))\n", + "\n", + "# 2. Reduce to find the total sum of negatives\n", + "# reduce(function, iterable, initializer)\n", + "total_negative_amount = reduce(lambda a, b: a + b, negative_balances, 0)\n", + "\n", + "print(f\"Negative balances found: {negative_balances}\")\n", + "print(f\"Total amount of debt: {total_negative_amount}\")" ] }, { @@ -273,10 +378,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "da2264b5-298e-4b45-99df-852b94e90d15", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining Balances:\n", + "[650, 1600, 275]\n" + ] + } + ], "source": [ "accounts = [\n", " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", @@ -284,13 +398,25 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# 1. Define the function for a single account\n", + "def calculate_balance(account):\n", + " \"\"\"\n", + " Subtracts the sum of all withdrawals from the initial balance.\n", + " \"\"\"\n", + " total_withdrawals = sum(account['withdrawals'])\n", + " return account['balance'] - total_withdrawals\n", + "\n", + "# 2. Use map to apply the function to the list of accounts\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "\n", + "print(\"Remaining Balances:\")\n", + "print(remaining_balances)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -304,7 +430,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,