diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..7d1d698 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "08463071-9351-4d49-8d29-4fcb817fb177", "metadata": {}, "outputs": [], @@ -94,12 +94,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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", + "[(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "credits = [t for t in transactions if t[1] == 'debit']\n", + "print(credits)\n", + "\n", + "debits = list(filter(lambda x: x[1] == 'debit', transactions))\n", + "print(debits) \n" ] }, { @@ -124,12 +137,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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" + "# Define a lambda function to sort in descending order\n", + "sort_descending = lambda x, y: x[0] > y[0]\n", + "\n", + "# Use the sorted() function with sort_descending and debits\n", + "sorted_debits = sorted(debits, key=lambda x: x[0], reverse=True)\n", + "\n", + "print(sorted_debits)" ] }, { @@ -158,7 +185,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, "outputs": [], @@ -169,12 +196,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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" + "\n", + "# define a function to calculate the balance after one year of interest\n", + "def add_interest(balance):\n", + " interest_rate = 0.05\n", + " return balance + (balance * interest_rate)\n", + "\n", + "# use the map function to apply the add_interest function to the list of balances\n", + "new_balances = list(map(add_interest, balances))\n", + "\n", + "print(new_balances)" ] }, { @@ -195,7 +239,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, "outputs": [], @@ -209,12 +253,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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" + "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", + "# define a function to calculate the balance after one year of interest\n", + "def add_interest(account):\n", + " return {'balance': account['balance'] + (account['balance'] * account['interest_rate']), 'interest_rate': account['interest_rate']}\n", + "\n", + "# use the map function to apply the add_interest function to the list of accounts\n", + "new_accounts = list(map(add_interest, accounts))\n", + "\n", + "print(new_accounts)\n" ] }, { @@ -243,14 +308,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-33.25\n" + ] + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "\n", + "\n", + "# list of bank account balances after applying an interest_rate of 0.05\n", + "balances = [105.0, 52.5, -23.75, 1050.0, -9.5]\n", + "\n", + "# use filter to get the negative balances\n", + "negative_balances = list(filter(lambda x: x < 0, balances))\n", + "\n", + "# use reduce to calculate the total amount of negative balances\n", + "total_negative = reduce(lambda x, y: x + y, negative_balances)\n", + "\n", + "print(total_negative)" ] }, { @@ -273,10 +357,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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 +376,19 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "def calculate_balance(account):\n", + " return account['balance'] - sum(account['withdrawals'])\n", + "\n", + "remaining_balances = list(map(calculate_balance, accounts))\n", + "\n", + "print(remaining_balances)\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -304,7 +402,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,