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
175 changes: 143 additions & 32 deletions lab-python-lambda-map-reduce-filter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"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"
"transactions = [\n",
" (-1200, 'debit'),\n",
" (2500, 'credit'),\n",
" (-100, 'debit'),\n",
" (850, 'credit'),\n",
" (-250, 'debit'),\n",
" (1500, 'credit'),\n",
" (-300, 'debit'),\n",
" (5000, 'credit'),\n",
" (-850, 'debit'),\n",
" (1000, 'credit')\n",
"]\n",
"\n",
"debits = list(filter(lambda transaction: transaction[1] == 'debit', transactions))\n",
"\n",
"print(debits)"
]
},
{
Expand All @@ -124,12 +147,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"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"
"sort_descending = lambda transaction: transaction[0]\n",
"\n",
"debits_descending = sorted(\n",
" debits,\n",
" key=sort_descending,\n",
" reverse=True\n",
")\n",
"\n",
"print(debits_descending)"
]
},
{
Expand Down Expand Up @@ -158,23 +197,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"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": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[105.0, 52.5, -26.25, 1050.0, -10.5]\n"
]
}
],
"source": [
"# your code goes here"
"# Original bank account balances\n",
"balances = [100, 50, -25, 1000, -10]\n",
"\n",
"# Interest rate of 5%\n",
"interest_rate = 0.05\n",
"\n",
"# Apply one year of interest to every balance\n",
"new_balances = list(\n",
" map(lambda balance: balance * (1 + interest_rate), balances)\n",
")\n",
"\n",
"print(new_balances)"
]
},
{
Expand Down Expand Up @@ -209,12 +256,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"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",
"updated_accounts = list(\n",
" map(\n",
" lambda account: {\n",
" 'balance': account['balance'] * (1 + account['interest_rate']),\n",
" 'interest_rate': account['interest_rate']\n",
" },\n",
" accounts\n",
" )\n",
")\n",
"\n",
"print(updated_accounts)\n"
]
},
{
Expand Down Expand Up @@ -243,14 +314,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "6284dbd3-e117-411e-8087-352be6deaed4",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Negative balances: [-26.25, -10.5]\n",
"Total negative balance: -36.75\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here"
"# Balances after adding 5% interest\n",
"new_balances = [105.0, 52.5, -26.25, 1050.0, -10.5]\n",
"\n",
"# Keep only negative balances\n",
"negative_balances = list(\n",
" filter(lambda balance: balance < 0, new_balances)\n",
")\n",
"\n",
"# Add all negative balances together\n",
"total_negative_balance = reduce(\n",
" lambda total, balance: total + balance,\n",
" negative_balances,\n",
" 0\n",
")\n",
"\n",
"print(\"Negative balances:\", negative_balances)\n",
"print(\"Total negative balance:\", total_negative_balance)"
]
},
{
Expand All @@ -273,26 +369,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"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",
" {'balance': 2000, 'withdrawals': [300, 100]},\n",
" {'balance': 500, 'withdrawals': [50, 100, 75]},\n",
"]\n",
"\n",
"# your code goes here\n"
"def calculate_balance(account):\n",
" total_withdrawals = sum(account['withdrawals'])\n",
" remaining_balance = account['balance'] - total_withdrawals\n",
" return remaining_balance\n",
"\n",
"remaining_balances = list(map(calculate_balance, accounts))\n",
"\n",
"print(remaining_balances)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:anaconda3] *",
"language": "python",
"name": "python3"
"name": "conda-env-anaconda3-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -304,7 +415,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down