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
108 changes: 85 additions & 23 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": 1,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -94,12 +94,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"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"
"credits = list(filter(lambda item: item[1] == 'debit', transactions))\n",
"print(credits)"
]
},
{
Expand All @@ -124,12 +133,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(-1200, 'debit'), (-850, 'debit'), (-300, 'debit'), (-250, 'debit'), (-100, 'debit')]\n"
]
}
],
"source": [
"# your code goes here"
"\n",
"\n",
"sorted_list = sorted(credits, key = lambda item:item[0])\n",
"print(sorted_list)\n"
]
},
{
Expand Down Expand Up @@ -158,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +189,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[95.0, 47.5, -23.75, 950.0, -9.5]\n"
]
}
],
"source": [
"# your code goes here"
"balances_after_rates = list(map(lambda x: x - x*0.05, balances))\n",
"print(balances_after_rates)"
]
},
{
Expand All @@ -195,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +238,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[980.0, 1980.0, 485.0]\n"
]
}
],
"source": [
"# your code goes here\n"
"updated_accounts = list(map(lambda x: x['balance'] - x['balance']*x['interest_rate'], accounts ))\n",
"print(updated_accounts)"
]
},
{
Expand Down Expand Up @@ -243,14 +281,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"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"
"negative_balances = list(filter(lambda x:x < 0, balances_after_rates))\n",
"sum_negative_balances = reduce(lambda x,y:x+y, negative_balances)\n",
"print(sum_negative_balances)"
]
},
{
Expand All @@ -273,24 +321,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 46,
"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"
"# your code goes here\n",
"def calculate_balance(accounts_list):\n",
" for account in accounts_list:\n",
" account['balance'] = sum(account['withdrawals'])\n",
" print(account)\n",
"new_accounts = list(map(lambda account:account['balance']-sum(account['withdrawals']),accounts))\n",
"print(new_accounts)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +366,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down