Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 137 additions & 22 deletions lab-python-lambda-map-reduce-filter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -94,12 +94,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"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",
"credits = list(filter(lambda transaction: transaction[1] == 'credit', transactions))\n",
"debits = list(filter(lambda transaction: transaction[1] == 'debit', transactions))\n",
"print(\"Credits:\", credits)\n",
"print(\"Debits:\", debits)"
]
},
{
Expand All @@ -124,12 +137,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 41,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Debits sorted in descending order: [(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"sort_descending = lambda transaction: transaction[0]\n",
"debits_sorted_desc = sorted(debits, key=sort_descending, reverse=True)\n",
"print(\"Debits sorted in descending order:\", debits_sorted_desc)"
]
},
{
Expand Down Expand Up @@ -158,7 +182,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 45,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +193,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 46,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[105.0, 52.5, -25, 1050.0, -10]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"add_interest = lambda balance: balance + (balance * 0.05) if balance > 0 else balance\n",
"new_balances = list(map(add_interest, balances))\n",
"print(new_balances) "
]
},
{
Expand All @@ -195,7 +231,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 49,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +245,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 50,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'balance': 1020.0, 'interest_rate': 0.02}\n",
"{'balance': 2020.0, 'interest_rate': 0.01}\n",
"{'balance': 515.0, 'interest_rate': 0.03}\n"
]
}
],
"source": [
"# your code goes here\n"
"# your code goes here\n",
"\n",
"add_interest = lambda account: {\n",
" \"balance\": account[\"balance\"] + (account[\"balance\"] * account[\"interest_rate\"]) if account[\"balance\"] > 0 else account[\"balance\"],\n",
" \"interest_rate\": account[\"interest_rate\"]\n",
"}\n",
"\n",
"\n",
"new_accounts = list(map(add_interest, accounts))\n",
"\n",
"\n",
"for account in new_accounts:\n",
" print(account)"
]
},
{
Expand Down Expand Up @@ -243,14 +301,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 54,
"id": "6284dbd3-e117-411e-8087-352be6deaed4",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total negative balances: -35\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here"
"# your code goes here\n",
"# Assume this is the result after applying 5% interest rate (e.g., Challenge 1, Exercise 1)\n",
"balances_after_interest = [105.0, 52.5, -25, 1050.0, -10]\n",
"\n",
"# Use filter() to get all negative balances\n",
"negative_balances = list(filter(lambda balance: balance < 0, balances_after_interest))\n",
"\n",
"# Use reduce() to sum up all the negative balances\n",
"total_negative_balance = reduce(lambda x, y: x + y, negative_balances)\n",
"\n",
"# Print the total negative balance\n",
"print(\"Total negative balances:\", total_negative_balance)"
]
},
{
Expand All @@ -273,19 +350,57 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 57,
"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",
" {'balance': 2000, 'withdrawals': [300, 100]},\n",
" {'balance': 500, 'withdrawals': [50, 100, 75]},\n",
"]\n",
"\n",
"# your code goes here\n"
"# your code goes here\n",
"# Define the calculate_balance function\n",
"def calculate_balance(account):\n",
" \"\"\"\n",
" Calculate the remaining balance after subtracting all withdrawals.\n",
"\n",
" Parameters:\n",
" account (dict): A dictionary representing a bank account with keys 'balance' and 'withdrawals'.\n",
"\n",
" Returns:\n",
" float: The remaining balance after withdrawals.\n",
" \"\"\"\n",
" # Sum up all withdrawals and subtract from balance\n",
" total_withdrawals = sum(account.get('withdrawals', []))\n",
" remaining_balance = account['balance'] - total_withdrawals\n",
" return remaining_balance\n",
"\n",
"\n",
"# Use map to apply calculate_balance to each account\n",
"remaining_balances = list(map(calculate_balance, accounts))\n",
"\n",
"# Print the list of remaining balances\n",
"print(\"Remaining balances after withdrawals:\", remaining_balances)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "159bd98c-19de-44b8-bc9e-213a17574947",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -304,7 +419,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down