diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..7ae9af0 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -94,12 +94,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "# Create a new list called debits that includes all of the debit transactions from the list transactions.\n", + "# Use the filter() function to create a new list called debits\n", + "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n", + "debits = list(filter(lambda x: x[1] == 'debit', transactions)) \n", + "print(debits)\n", + "\n" ] }, { @@ -124,12 +137,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "# Use the previously created debits list.\n", + "# Define a lambda function called sort_descending that takes two tuples and returns True \n", + "# 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.\n", + "\n", + "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n", + "debits = list(filter(lambda x: x[1] == 'debit', transactions))\n", + "sort_descending = lambda x: x[0]\n", + "sorted_debits = sorted(debits, key=sort_descending, reverse=True)\n", + "print(sorted_debits)" ] }, { @@ -169,12 +199,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]\n" + ] + } + ], "source": [ - "# your code goes here" + "# Write Python code to take a list of bank account balances, \n", + "# and returns a new list containing the balance after one year of interest has been added.\n", + "# Use the map function to apply this function to the list of bank accounts, and take an interest rate of 0.05.\n", + "balances = [100, 50, -25, 1000, -10]\n", + "interest_rate = 0.05\n", + "updated_balances = list(map(lambda x: x * (1 + interest_rate), balances))\n", + "print(updated_balances)" ] }, { @@ -209,12 +253,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'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" + "# Write Python code to take a list of bank account dictionaries, each containing the account balance and interest rate,\n", + "# and returns a new list of dictionaries containing the balance after one year of interest has been added. \n", + "# Use the map function to apply this function to the list of bank accounts.\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", + "updated_accounts = list(map(lambda account: {'balance': account['balance'] * (1 + account['interest_rate']), 'interest_rate': account['interest_rate']}, accounts))\n", + "print(updated_accounts)" ] }, { @@ -243,14 +304,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-36.75\n" + ] + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "# Write Python code to take the new list of bank account balances (balances list after applying an interest_rate of 0.05, \n", + "# result of Challenge 1 Exercise 1),\n", + "# and print the total amount of negative balances. Use filter and reduce function.\n", + "updated_accounts = [105.0, 52.5, -26.25, 1050.0, -10.5]\n", + "negative_balances = list(filter(lambda x: x < 0, updated_accounts))\n", + "total_negative_balance = reduce(lambda x, y: x + y, negative_balances)\n", + "print(total_negative_balance)\n" ] }, { @@ -273,10 +348,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "da2264b5-298e-4b45-99df-852b94e90d15", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[650, 1600, 275]\n" + ] + } + ], "source": [ "accounts = [\n", " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", @@ -284,13 +367,22 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# Write a Python function called calculate_balance that takes a bank account dictionary as an \n", + "# argument and returns the remaining balance after subtracting all the withdrawals.\n", + "#Then, use the map function and the calculate_balance function to apply it to the list accounts. \n", + "# This should give you a list of remaining balances after all the withdrawals have been subtracted.\n", + "\n", + "def calculate_balance(account):\n", + " return account['balance'] - sum(account['withdrawals'])\n", + "\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "print(remaining_balances) " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -304,7 +396,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,