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
149 changes: 126 additions & 23 deletions lab-python-lambda-map-reduce-filter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "0781335d-39cf-403d-b86a-ca908a09fe55",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(2500, 'credit'),\n",
" (850, 'credit'),\n",
" (1500, 'credit'),\n",
" (5000, 'credit'),\n",
" (1000, 'credit')]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n",
"#Create a new list called credits that includes all of the debit transactions from the list transactions. Use the filter () function.\n",
"credits = list(filter(lambda x: x[1] == 'credit', transactions))\n",
"\n",
"credits"
]
},
{
Expand All @@ -124,12 +143,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(-100, 'debit'),\n",
" (-250, 'debit'),\n",
" (-300, 'debit'),\n",
" (-850, 'debit'),\n",
" (-1200, 'debit')]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# Create a list of debit transactions\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"\n",
"# Define a lambda function for sorting in descending order by amount\n",
"sort_descending = lambda x: x[0]\n",
"\n",
"# Sort the debits list in descending order by amount\n",
"debits_sorted = sorted(debits, key=sort_descending, reverse=True)\n",
"\n",
"debits_sorted\n",
"\n"
]
},
{
Expand Down Expand Up @@ -158,7 +202,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +213,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[105.0, 52.5, -26.25, 1050.0, -10.5]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# Calculate new balances after adding 5% interest\n",
"balances_with_interest = list(map(lambda x: x * 1.05, balances))\n",
"balances_with_interest\n"
]
},
{
Expand All @@ -195,7 +252,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +266,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"execution_count": 7,
"id": "2a3b2447",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[{'balance': 1020.0, 'interest_rate': 0.02},\n",
" {'balance': 2020.0, 'interest_rate': 0.01},\n",
" {'balance': 515.0, 'interest_rate': 0.03}]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here\n"
"# Calculate new balances after adding interest for each account\n",
"accounts_with_interest = list(\n",
" map(lambda acc: {'balance': acc['balance'] * (1 + acc['interest_rate']), 'interest_rate': acc['interest_rate']}, accounts)\n",
")\n",
"accounts_with_interest"
]
},
{
Expand Down Expand Up @@ -243,14 +317,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"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"
"# Filter negative balances\n",
"negative_balances = list(filter(lambda x: x < 0, balances_with_interest))\n",
"\n",
"# Sum the negative balances using reduce\n",
"total_negative = reduce(lambda a, b: a + b, negative_balances, 0)\n",
"\n",
"print(total_negative)"
]
},
{
Expand All @@ -273,24 +361,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "da2264b5-298e-4b45-99df-852b94e90d15",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[650, 1600, 275]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
" return account['balance'] - sum(account.get('withdrawals', []))\n",
"\n",
"remaining_balances = list(map(calculate_balance, accounts))\n",
"remaining_balances\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +407,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down