From e5396cf4df89cd13fc8aac62bc92af3be8df5236 Mon Sep 17 00:00:00 2001 From: Bullionwuerfel Date: Thu, 9 Jan 2025 15:16:05 +0100 Subject: [PATCH] Update lab-python-lambda-map-reduce-filter.ipynb --- lab-python-lambda-map-reduce-filter.ipynb | 788 +++++++++++++--------- 1 file changed, 478 insertions(+), 310 deletions(-) diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..b2e339a 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -1,312 +1,480 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", - "metadata": {}, - "source": [ - "# Lab | Lambda Functions, Map, Reduce, Filter" - ] + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": { + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e" + }, + "source": [ + "# Lab | Lambda Functions, Map, Reduce, Filter" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": { + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6" + }, + "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": { + "id": "0120f101-3f9a-444c-84b9-a10a990a5f95" + }, + "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": { + "id": "7afb4d9e-63a9-4326-87f1-a0915769721c" + }, + "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": { + "id": "053ee1bc-59fa-4cf8-9191-916b6cdc5811" + }, + "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": { + "id": "65df3710-f236-4818-9a4f-43bfd53cad7c" + }, + "source": [ + "## Challenge 1" + ] + }, + { + "cell_type": "markdown", + "id": "0e704740-6ac4-49d0-b25b-960cf8511a04", + "metadata": { + "id": "0e704740-6ac4-49d0-b25b-960cf8511a04" + }, + "source": [ + "In this Challenge we will use the following data, which is a list of bank transactions:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "08463071-9351-4d49-8d29-4fcb817fb177", + "metadata": { + "id": "08463071-9351-4d49-8d29-4fcb817fb177" + }, + "outputs": [], + "source": [ + "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]" + ] + }, + { + "cell_type": "markdown", + "id": "ae7d88f0-a8e7-4f74-98d3-802306bdcf07", + "metadata": { + "id": "ae7d88f0-a8e7-4f74-98d3-802306bdcf07" + }, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "16796011-a618-499a-a57f-4ca61d0a77e7", + "metadata": { + "id": "16796011-a618-499a-a57f-4ca61d0a77e7" + }, + "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", + "source": [], + "metadata": { + "id": "8C2fFRpPO1MC" + }, + "id": "8C2fFRpPO1MC", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "0781335d-39cf-403d-b86a-ca908a09fe55", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0781335d-39cf-403d-b86a-ca908a09fe55", + "outputId": "92cf6214-f760-4b82-aa2a-4bcc95a6d4f8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n" + ] + } + ], + "source": [ + "debits = list(filter((lambda x : x[1]=='debit'), transactions))\n", + "\n", + "print(debits)" + ] + }, + { + "cell_type": "markdown", + "id": "37ca34af-7f87-44f2-a45d-1a1e2ef8c194", + "metadata": { + "id": "37ca34af-7f87-44f2-a45d-1a1e2ef8c194" + }, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "546c5aa2-6f16-4285-9c47-0f788b23552c", + "metadata": { + "id": "546c5aa2-6f16-4285-9c47-0f788b23552c" + }, + "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": 48, + "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", + "outputId": "70c3fd7e-8ec9-4110-a0e4-8596a4ace1fb" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n" + ] + } + ], + "source": [ + "sortdebits= list(sorted(list(debits), key = lambda x:x[0], reverse=True))\n", + "\n", + "print(sortdebits)" + ] + }, + { + "cell_type": "markdown", + "id": "bee57a6e-19a3-4708-9b70-3b41a215353f", + "metadata": { + "id": "bee57a6e-19a3-4708-9b70-3b41a215353f" + }, + "source": [ + "## Challenge 2: Interest Calculation" + ] + }, + { + "cell_type": "markdown", + "id": "4e7c9f00-7c48-4c39-9de1-14db7fc468eb", + "metadata": { + "id": "4e7c9f00-7c48-4c39-9de1-14db7fc468eb" + }, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "3500df4c-91bd-4f8f-9ffe-4d6a51460bc6", + "metadata": { + "id": "3500df4c-91bd-4f8f-9ffe-4d6a51460bc6" + }, + "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": 66, + "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", + "outputId": "54746a73-43bb-42ba-c28a-2972b8115f21" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]\n" + ] + } + ], + "source": [ + "# create list of bank account balances\n", + "balances = [100, 50, -25, 1000, -10]\n", + "\n", + "yearbalance=list(map(lambda x:x*1.05,balances))\n", + "print(yearbalance)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f253b7e-5300-4819-b38f-9fc090554f51", + "metadata": { + "id": "2f253b7e-5300-4819-b38f-9fc090554f51" + }, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "6a515b75-8ccb-4ee3-9dc0-f36d72ac66a0", + "metadata": { + "id": "6a515b75-8ccb-4ee3-9dc0-f36d72ac66a0" + }, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "7022779a-7611-428b-8f7d-138e923a63b8", + "metadata": { + "id": "7022779a-7611-428b-8f7d-138e923a63b8" + }, + "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": 63, + "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", + "metadata": { + "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696" + }, + "outputs": [], + "source": [ + "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": 65, + "id": "0906a9b0-d567-4786-96f2-5755611b885e", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0906a9b0-d567-4786-96f2-5755611b885e", + "outputId": "89622ce8-597c-49d1-acf1-7562260caea0" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[{'balance': 1020.0, 'interest_rate': 0.02}, {'balance': 2020.0, 'interest_rate': 0.01}, {'balance': 515.0, 'interest_rate': 0.03}]\n" + ] + } + ], + "source": [ + "yearaccounts = list(map(lambda x:{'balance':x['balance']*(1+x['interest_rate']), 'interest_rate': x['interest_rate']},accounts))\n", + "\n", + "print(yearaccounts)" + ] + }, + { + "cell_type": "markdown", + "id": "69788c08-90c5-4c15-be3d-55ac4a963fe2", + "metadata": { + "id": "69788c08-90c5-4c15-be3d-55ac4a963fe2" + }, + "source": [ + "## Challenge 3: Balance Reduction" + ] + }, + { + "cell_type": "markdown", + "id": "81536227-379c-461f-b564-6695957bd0c1", + "metadata": { + "id": "81536227-379c-461f-b564-6695957bd0c1" + }, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "1006852a-42fc-4aae-bc0a-c771a94f6b2d", + "metadata": { + "id": "1006852a-42fc-4aae-bc0a-c771a94f6b2d" + }, + "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": 83, + "id": "6284dbd3-e117-411e-8087-352be6deaed4", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6284dbd3-e117-411e-8087-352be6deaed4", + "outputId": "6fb06945-f2b4-44ba-bbc4-5f314b0734fb" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "-36.75\n" + ] + } + ], + "source": [ + "from functools import reduce\n", + "\n", + "negamount=reduce(lambda x,y: x+y , filter(lambda x:x<0,yearbalance))\n", + "print(negamount)\n" + ] + }, + { + "cell_type": "markdown", + "id": "3926594f-3204-4de3-a6bf-bd87069a82cc", + "metadata": { + "id": "3926594f-3204-4de3-a6bf-bd87069a82cc" + }, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "6149139b-43db-473b-a10e-8bff9a38dcbf", + "metadata": { + "id": "6149139b-43db-473b-a10e-8bff9a38dcbf" + }, + "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": 95, + "id": "da2264b5-298e-4b45-99df-852b94e90d15", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "da2264b5-298e-4b45-99df-852b94e90d15", + "outputId": "18019f33-2f38-4abd-c6b0-054d6ad2b703" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[{'balance': 650, 'withdrawals': []}, {'balance': 1600, 'withdrawals': []}, {'balance': 275, 'withdrawals': []}]\n" + ] + } + ], + "source": [ + "accounts = [\n", + " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", + " {'balance': 2000, 'withdrawals': [300, 100]},\n", + " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", + "]\n", + "\n", + "\n", + "newacc=list(map(lambda x :{ 'balance':(x['balance']-reduce(lambda a,b:a+b,x['withdrawals'])),'withdrawals':list()},accounts))\n", + "\n", + "print(newacc)\n" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "UN79wLV8ZvF7" + }, + "id": "UN79wLV8ZvF7", + "execution_count": null, + "outputs": [] + } + ], + "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.9.13" + }, + "colab": { + "provenance": [] + } }, - { - "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": null, - "id": "08463071-9351-4d49-8d29-4fcb817fb177", - "metadata": {}, - "outputs": [], - "source": [ - "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]" - ] - }, - { - "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": null, - "id": "0781335d-39cf-403d-b86a-ca908a09fe55", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, - { - "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": null, - "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, - { - "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": null, - "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", - "metadata": {}, - "outputs": [], - "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" - ] - }, - { - "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": null, - "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", - "metadata": {}, - "outputs": [], - "source": [ - "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" - ] - }, - { - "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": null, - "id": "6284dbd3-e117-411e-8087-352be6deaed4", - "metadata": {}, - "outputs": [], - "source": [ - "from functools import reduce\n", - "\n", - "# your code goes here" - ] - }, - { - "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": null, - "id": "da2264b5-298e-4b45-99df-852b94e90d15", - "metadata": {}, - "outputs": [], - "source": [ - "accounts = [\n", - " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", - " {'balance': 2000, 'withdrawals': [300, 100]},\n", - " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", - "]\n", - "\n", - "# your code goes here\n" - ] - } - ], - "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.9.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file