diff --git a/.ipynb_checkpoints/lab-python-lambda-map-reduce-filter-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-lambda-map-reduce-filter-checkpoint.ipynb new file mode 100644 index 0000000..5ec99b5 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-lambda-map-reduce-filter-checkpoint.ipynb @@ -0,0 +1,467 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Lambda Functions, Map, Reduce, Filter" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: The objective of this lab is to help students gain familiarity with using anonymous lambda functions and map, reduce, and filter methods in Python." + ] + }, + { + "cell_type": "markdown", + "id": "0120f101-3f9a-444c-84b9-a10a990a5f95", + "metadata": {}, + "source": [ + "Lambda functions, map, reduce, and filter are all related to functional programming in Python. \n", + "\n", + "**Lambda functions** are anonymous functions in Python, which means that they do not need to be defined with a name. They are typically used for short, one-line functions that are not going to be used elsewhere in the code. \n", + "\n", + "Lambda functions can take any number of arguments, but they can only contain a single expression. They are often used in combination with other built-in functions like map, reduce, and filter to perform operations on lists or other iterables." + ] + }, + { + "cell_type": "markdown", + "id": "7afb4d9e-63a9-4326-87f1-a0915769721c", + "metadata": {}, + "source": [ + "**Map** is a function that applies a given function to every element of a list, returning a new list with the transformed values. It's a useful way to apply the same operation to every element of a list, without having to write a for loop.\n", + "\n", + "**Reduce** is a function that applies a given function to the first two elements of a list, then applies the same function to the result and the next element, and so on, until it has reduced the list to a single value. It's useful for performing cumulative operations like summing or multiplying a list of numbers.\n", + "\n", + "**Filter** is a function that takes a function and a list, and returns a new list containing only the elements of the original list for which the function returns True. It's a useful way to selectively extract elements from a list based on a certain condition." + ] + }, + { + "cell_type": "markdown", + "id": "053ee1bc-59fa-4cf8-9191-916b6cdc5811", + "metadata": {}, + "source": [ + "All of these concepts can be used to make your code more concise and expressive, especially when working with lists or other iterables. They are also useful in data manipulation tasks, such as cleaning and transforming data in a pandas DataFrame. By practicing with these concepts in the lab, you will gain a better understanding of how to use them in a variety of programming tasks." + ] + }, + { + "cell_type": "markdown", + "id": "65df3710-f236-4818-9a4f-43bfd53cad7c", + "metadata": {}, + "source": [ + "## Challenge 1" + ] + }, + { + "cell_type": "markdown", + "id": "0e704740-6ac4-49d0-b25b-960cf8511a04", + "metadata": {}, + "source": [ + "In this Challenge we will use the following data, which is a list of bank transactions:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "08463071-9351-4d49-8d29-4fcb817fb177", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1️ Absolute Values of Transactions: [100, 50, 200, 150, 50, 25, 400, 300, 125]\n", + "2️ Withdrawals Only: [-50, -150, -25, -300]\n", + "3️ Deposits Only: [100, 200, 50, 400, 125]\n", + "4️ Total Balance: 350\n", + "5️ Formatted Transactions: ['$100.00', '$-50.00', '$200.00', '$-150.00', '$50.00', '$-25.00', '$400.00', '$-300.00', '$125.00']\n" + ] + } + ], + "source": [ + "from functools import reduce\n", + "\n", + "# Sample list of bank transactions\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n", + "\n", + "# 1. Use map() + lambda to get absolute values of all transactions\n", + "absolute_transactions = list(map(lambda x: abs(x), transactions))\n", + "print(\"1️ Absolute Values of Transactions:\", absolute_transactions)\n", + "\n", + "# 2. Use filter() + lambda to get only withdrawals (negative values)\n", + "withdrawals = list(filter(lambda x: x < 0, transactions))\n", + "print(\"2️ Withdrawals Only:\", withdrawals)\n", + "\n", + "# 3. Use filter() + lambda to get only deposits (positive values)\n", + "deposits = list(filter(lambda x: x > 0, transactions))\n", + "print(\"3️ Deposits Only:\", deposits)\n", + "\n", + "# 4. Use reduce() + lambda to calculate total balance\n", + "total_balance = reduce(lambda acc, x: acc + x, transactions)\n", + "print(\"4️ Total Balance:\", total_balance)\n", + "\n", + "# 5. Bonus: Format each transaction as a currency string\n", + "formatted = list(map(lambda x: f\"${x:.2f}\", transactions))\n", + "print(\"5️ Formatted Transactions:\", formatted)\n" + ] + }, + { + "cell_type": "markdown", + "id": "ae7d88f0-a8e7-4f74-98d3-802306bdcf07", + "metadata": {}, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "16796011-a618-499a-a57f-4ca61d0a77e7", + "metadata": {}, + "source": [ + "Create a new list called credits that includes all of the debit transactions from the list transactions.\n", + "\n", + "Use the filter() function to create a new list called debits." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "0781335d-39cf-403d-b86a-ca908a09fe55", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Credits (Deposits): [100, 200, 50, 400, 125]\n", + "Debits (Withdrawals): [-50, -150, -25, -300]\n" + ] + } + ], + "source": [ + "# transactions list\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n", + "\n", + "# Credits\n", + "credits = list(filter(lambda x: x > 0, transactions))\n", + "print(\"Credits (Deposits):\", credits)\n", + "\n", + "# Debits\n", + "debits = list(filter(lambda x: x < 0, transactions))\n", + "print(\"Debits (Withdrawals):\", debits)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "37ca34af-7f87-44f2-a45d-1a1e2ef8c194", + "metadata": {}, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "546c5aa2-6f16-4285-9c47-0f788b23552c", + "metadata": {}, + "source": [ + "Create a new list that includes all of the debit transactions from the list transactions, sorted in descending order by amount.\n", + "\n", + "- Use the previously created debits list.\n", + "- Define a lambda function called sort_descending that takes two tuples and returns True if the transaction amount of the first tuple is greater than the transaction amount of the second tuple.\n", + "- Use the sorted() function with sort_descending and debits to create a new list." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Debits sorted by descending amount: [-300, -150, -50, -25]\n" + ] + } + ], + "source": [ + "\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n", + "debits = list(filter(lambda x: x < 0, transactions))\n", + "\n", + "\n", + "sorted_debits = sorted(debits, key=lambda x: abs(x), reverse=True)\n", + "\n", + "print(\"Debits sorted by descending amount:\", sorted_debits)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "bee57a6e-19a3-4708-9b70-3b41a215353f", + "metadata": {}, + "source": [ + "## Challenge 2: Interest Calculation" + ] + }, + { + "cell_type": "markdown", + "id": "4e7c9f00-7c48-4c39-9de1-14db7fc468eb", + "metadata": {}, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "3500df4c-91bd-4f8f-9ffe-4d6a51460bc6", + "metadata": {}, + "source": [ + "Write Python code to take a list of bank account balances, and returns a new list containing the balance after one year of interest has been added. Use the map function to apply this function to the list of bank accounts, and take an interest rate of 0.05." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Balances after 1 year: [1050.0, 1575.0, 2100.0, 525.0, 787.5]\n" + ] + } + ], + "source": [ + "\n", + "balances = [1000, 1500, 2000, 500, 750]\n", + "\n", + "\n", + "interest_rate = 0.05\n", + "\n", + "\n", + "updated_balances = list(map(lambda x: x * (1 + interest_rate), balances))\n", + "\n", + "print(\"Updated Balances after 1 year:\", updated_balances)\n" + ] + }, + { + "cell_type": "markdown", + "id": "6a515b75-8ccb-4ee3-9dc0-f36d72ac66a0", + "metadata": {}, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "7022779a-7611-428b-8f7d-138e923a63b8", + "metadata": {}, + "source": [ + "Write Python code to take a list of bank account dictionaries, each containing the account balance and interest rate, and returns a new list of dictionaries containing the balance after one year of interest has been added. Use the map function to apply this function to the list of bank accounts." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Account Balances:\n", + "{'balance': 1050.0, 'interest_rate': 0.05}\n", + "{'balance': 1560.0, 'interest_rate': 0.04}\n", + "{'balance': 2060.0, 'interest_rate': 0.03}\n", + "{'balance': 530.0, 'interest_rate': 0.06}\n" + ] + } + ], + "source": [ + "\n", + "accounts = [\n", + " {'balance': 1000, 'interest_rate': 0.05},\n", + " {'balance': 1500, 'interest_rate': 0.04},\n", + " {'balance': 2000, 'interest_rate': 0.03},\n", + " {'balance': 500, 'interest_rate': 0.06},\n", + "]\n", + "\n", + "\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", + "\n", + "print(\"Updated Account Balances:\")\n", + "for acc in updated_accounts:\n", + " print(acc)\n" + ] + }, + { + "cell_type": "markdown", + "id": "69788c08-90c5-4c15-be3d-55ac4a963fe2", + "metadata": {}, + "source": [ + "## Challenge 3: Balance Reduction" + ] + }, + { + "cell_type": "markdown", + "id": "81536227-379c-461f-b564-6695957bd0c1", + "metadata": {}, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "1006852a-42fc-4aae-bc0a-c771a94f6b2d", + "metadata": {}, + "source": [ + "Write Python code to take the new list of bank account balances (balances list after applying an interest_rate of 0.05, result of Challenge 1 Exercise 1), and print the total amount of negative balances. Use filter and reduce function." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "8e3441ef-b2a4-409d-8e44-b62d7b1de271", + "metadata": {}, + "outputs": [], + "source": [ + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c6f477b2-4052-425d-8d2a-b6bd5b18521f", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "balances = list(map(lambda x: x * 1.05, transactions))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6284dbd3-e117-411e-8087-352be6deaed4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Total Negative Balances: -551.25\n" + ] + } + ], + "source": [ + "\n", + "\n", + "negative_balances = list(filter(lambda x: x < 0, balances))\n", + "\n", + "\n", + "total_negative = reduce(lambda acc, x: acc + x, negative_balances, 0)\n", + "\n", + "print(\" Total Negative Balances:\", total_negative)\n" + ] + }, + { + "cell_type": "markdown", + "id": "3926594f-3204-4de3-a6bf-bd87069a82cc", + "metadata": {}, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "6149139b-43db-473b-a10e-8bff9a38dcbf", + "metadata": {}, + "source": [ + "Write a Python function called calculate_balance that takes a bank account dictionary as an argument and returns the remaining balance after subtracting all the withdrawals.\n", + "\n", + "Then, use the map function and the calculate_balance function to apply it to the list accounts. This should give you a list of remaining balances after all the withdrawals have been subtracted." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "da2264b5-298e-4b45-99df-852b94e90d15", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining Balances After Withdrawals: [850, 1150, 1700, 500]\n" + ] + } + ], + "source": [ + "from functools import reduce\n", + "\n", + "\n", + "accounts = [\n", + " {'balance': 1000, 'withdrawals': [100, 50]},\n", + " {'balance': 1500, 'withdrawals': [200, 100, 50]},\n", + " {'balance': 2000, 'withdrawals': [300]},\n", + " {'balance': 500, 'withdrawals': []},\n", + "]\n", + "\n", + "def calculate_balance(account):\n", + " total_withdrawals = sum(account['withdrawals'])\n", + " return account['balance'] - total_withdrawals\n", + "\n", + "\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "\n", + "\n", + "print(\"Remaining Balances After Withdrawals:\", remaining_balances)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c52ec7c9-efb0-4ccb-842d-42acd1cfae6b", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..5ec99b5 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -66,12 +66,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "08463071-9351-4d49-8d29-4fcb817fb177", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1️ Absolute Values of Transactions: [100, 50, 200, 150, 50, 25, 400, 300, 125]\n", + "2️ Withdrawals Only: [-50, -150, -25, -300]\n", + "3️ Deposits Only: [100, 200, 50, 400, 125]\n", + "4️ Total Balance: 350\n", + "5️ Formatted Transactions: ['$100.00', '$-50.00', '$200.00', '$-150.00', '$50.00', '$-25.00', '$400.00', '$-300.00', '$125.00']\n" + ] + } + ], "source": [ - "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]" + "from functools import reduce\n", + "\n", + "# Sample list of bank transactions\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n", + "\n", + "# 1. Use map() + lambda to get absolute values of all transactions\n", + "absolute_transactions = list(map(lambda x: abs(x), transactions))\n", + "print(\"1️ Absolute Values of Transactions:\", absolute_transactions)\n", + "\n", + "# 2. Use filter() + lambda to get only withdrawals (negative values)\n", + "withdrawals = list(filter(lambda x: x < 0, transactions))\n", + "print(\"2️ Withdrawals Only:\", withdrawals)\n", + "\n", + "# 3. Use filter() + lambda to get only deposits (positive values)\n", + "deposits = list(filter(lambda x: x > 0, transactions))\n", + "print(\"3️ Deposits Only:\", deposits)\n", + "\n", + "# 4. Use reduce() + lambda to calculate total balance\n", + "total_balance = reduce(lambda acc, x: acc + x, transactions)\n", + "print(\"4️ Total Balance:\", total_balance)\n", + "\n", + "# 5. Bonus: Format each transaction as a currency string\n", + "formatted = list(map(lambda x: f\"${x:.2f}\", transactions))\n", + "print(\"5️ Formatted Transactions:\", formatted)\n" ] }, { @@ -94,12 +129,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Credits (Deposits): [100, 200, 50, 400, 125]\n", + "Debits (Withdrawals): [-50, -150, -25, -300]\n" + ] + } + ], "source": [ - "# your code goes here" + "# transactions list\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n", + "\n", + "# Credits\n", + "credits = list(filter(lambda x: x > 0, transactions))\n", + "print(\"Credits (Deposits):\", credits)\n", + "\n", + "# Debits\n", + "debits = list(filter(lambda x: x < 0, transactions))\n", + "print(\"Debits (Withdrawals):\", debits)\n", + "\n" ] }, { @@ -124,12 +178,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Debits sorted by descending amount: [-300, -150, -50, -25]\n" + ] + } + ], "source": [ - "# your code goes here" + "\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n", + "debits = list(filter(lambda x: x < 0, transactions))\n", + "\n", + "\n", + "sorted_debits = sorted(debits, key=lambda x: abs(x), reverse=True)\n", + "\n", + "print(\"Debits sorted by descending amount:\", sorted_debits)\n", + "\n" ] }, { @@ -158,23 +228,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Balances after 1 year: [1050.0, 1575.0, 2100.0, 525.0, 787.5]\n" + ] + } + ], "source": [ - "# create list of bank account balances\n", - "balances = [100, 50, -25, 1000, -10]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2f253b7e-5300-4819-b38f-9fc090554f51", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" + "\n", + "balances = [1000, 1500, 2000, 500, 750]\n", + "\n", + "\n", + "interest_rate = 0.05\n", + "\n", + "\n", + "updated_balances = list(map(lambda x: x * (1 + interest_rate), balances))\n", + "\n", + "print(\"Updated Balances after 1 year:\", updated_balances)\n" ] }, { @@ -195,26 +271,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Account Balances:\n", + "{'balance': 1050.0, 'interest_rate': 0.05}\n", + "{'balance': 1560.0, 'interest_rate': 0.04}\n", + "{'balance': 2060.0, 'interest_rate': 0.03}\n", + "{'balance': 530.0, 'interest_rate': 0.06}\n" + ] + } + ], "source": [ + "\n", "accounts = [\n", - " {'balance': 1000, 'interest_rate': 0.02},\n", - " {'balance': 2000, 'interest_rate': 0.01},\n", - " {'balance': 500, 'interest_rate': 0.03},\n", - "]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0906a9b0-d567-4786-96f2-5755611b885e", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" + " {'balance': 1000, 'interest_rate': 0.05},\n", + " {'balance': 1500, 'interest_rate': 0.04},\n", + " {'balance': 2000, 'interest_rate': 0.03},\n", + " {'balance': 500, 'interest_rate': 0.06},\n", + "]\n", + "\n", + "\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", + "\n", + "print(\"Updated Account Balances:\")\n", + "for acc in updated_accounts:\n", + " print(acc)\n" ] }, { @@ -243,14 +334,48 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "6284dbd3-e117-411e-8087-352be6deaed4", + "execution_count": 10, + "id": "8e3441ef-b2a4-409d-8e44-b62d7b1de271", "metadata": {}, "outputs": [], "source": [ - "from functools import reduce\n", + "transactions = [100, -50, 200, -150, 50, -25, 400, -300, 125]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c6f477b2-4052-425d-8d2a-b6bd5b18521f", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "balances = list(map(lambda x: x * 1.05, transactions))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6284dbd3-e117-411e-8087-352be6deaed4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Total Negative Balances: -551.25\n" + ] + } + ], + "source": [ + "\n", + "\n", + "negative_balances = list(filter(lambda x: x < 0, balances))\n", + "\n", + "\n", + "total_negative = reduce(lambda acc, x: acc + x, negative_balances, 0)\n", "\n", - "# your code goes here" + "print(\" Total Negative Balances:\", total_negative)\n" ] }, { @@ -273,19 +398,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "da2264b5-298e-4b45-99df-852b94e90d15", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Remaining Balances After Withdrawals: [850, 1150, 1700, 500]\n" + ] + } + ], "source": [ + "from functools import reduce\n", + "\n", + "\n", "accounts = [\n", - " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", - " {'balance': 2000, 'withdrawals': [300, 100]},\n", - " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", + " {'balance': 1000, 'withdrawals': [100, 50]},\n", + " {'balance': 1500, 'withdrawals': [200, 100, 50]},\n", + " {'balance': 2000, 'withdrawals': [300]},\n", + " {'balance': 500, 'withdrawals': []},\n", "]\n", "\n", - "# your code goes here\n" + "def calculate_balance(account):\n", + " total_withdrawals = sum(account['withdrawals'])\n", + " return account['balance'] - total_withdrawals\n", + "\n", + "\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "\n", + "\n", + "print(\"Remaining Balances After Withdrawals:\", remaining_balances)\n", + "\n", + "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c52ec7c9-efb0-4ccb-842d-42acd1cfae6b", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -304,7 +459,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.2" } }, "nbformat": 4,