diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..de7d820 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -97,9 +97,27 @@ "execution_count": null, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Credits: [(2500, 'credit'), (850, 'credit'), (1500, 'credit'), (5000, 'credit'), (1000, 'credit')]\n", + "Debits: [(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'),\n", + " (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n", + "\n", + "# Filter out credits and debits using lambda functions\n", + "credits = list(filter(lambda x: x[1] == 'credit', transactions))\n", + "debits = list(filter(lambda x: x[1] == 'debit', transactions))\n", + "\n", + "print(\"Credits:\", credits)\n", + "print(\"Debits:\", debits)\n" ] }, { @@ -124,12 +142,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorted Debits (Descending): [(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# Sort the debits list in descending order based on the transaction amount\n", + "sort_descending = lambda x, y: x[0] > y[0]\n", + "sorted_debits = sorted(debits, key=lambda x: x[0], reverse=True)\n", + "\n", + "print(\"Sorted Debits (Descending):\", sorted_debits)\n" ] }, { @@ -169,12 +200,27 @@ }, { "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 1 Year Interest: [105.0, 52.5, -26.25, 1050.0, -10.5]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "balances = [100, 50, -25, 1000, -10]\n", + "\n", + "# Apply interest rate of 0.05 using the map function\n", + "interest_rate = 0.05\n", + "new_balances = list(map(lambda balance: balance * (1 + interest_rate), balances))\n", + "\n", + "print(\"Balances After 1 Year Interest:\", new_balances)\n" ] }, { @@ -209,12 +255,33 @@ }, { "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 After Interest: [{'balance': 1020.0, 'interest_rate': 0.02}, {'balance': 2020.0, 'interest_rate': 0.01}, {'balance': 515.0, 'interest_rate': 0.03}]\n" + ] + } + ], "source": [ - "# your code goes here\n" + "# 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", + "# Apply the interest rate for each account using map\n", + "updated_accounts = list(map(lambda account: {\n", + " 'balance': account['balance'] * (1 + account['interest_rate']),\n", + " 'interest_rate': account['interest_rate']\n", + "}, accounts))\n", + "\n", + "print(\"Updated Accounts After Interest:\", updated_accounts)\n" ] }, { @@ -243,14 +310,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Negative Balance: -36.75\n" + ] + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "# your code goes here\n", + "\n", + "# Filter out negative balances and then sum them using reduce\n", + "negative_balances = list(filter(lambda balance: balance < 0, new_balances))\n", + "\n", + "# Calculate the total negative balance\n", + "total_negative_balance = reduce(lambda x, y: x + y, negative_balances)\n", + "\n", + "print(\"Total Negative Balance:\", total_negative_balance)\n" ] }, { @@ -273,10 +356,18 @@ }, { "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 After Withdrawals: [650, 1600, 275]\n" + ] + } + ], "source": [ "accounts = [\n", " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", @@ -284,13 +375,23 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# your code goes here\n", + "# Function to calculate the remaining balance after withdrawals\n", + "def calculate_balance(account):\n", + " total_withdrawals = sum(account['withdrawals'])\n", + " remaining_balance = account['balance'] - total_withdrawals\n", + " return remaining_balance\n", + "\n", + "# Apply calculate_balance function to each account using map\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "\n", + "print(\"Remaining Balances After Withdrawals:\", remaining_balances)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -304,7 +405,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.1" } }, "nbformat": 4,