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
125 changes: 103 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": 1,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -94,12 +94,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 63,
"id": "0781335d-39cf-403d-b86a-ca908a09fe55",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(2500, 'credit'), (850, 'credit'), (1500, 'credit'), (5000, 'credit'), (1000, 'credit')] [(-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 x: x[1] == 'credit', transactions))\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"print(credits, debits)"
]
},
{
Expand All @@ -124,12 +135,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 64,
"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": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"sort_descending = lambda x, y: True if x[0] > y[0] else False \n",
"# I defined sort_descending as requested but it can't be used with sorted (as is defined) given that sorted only send 1 argument (the current elemet) to function 'key' and not two as expected in sort_descending.\n",
"debits_ex2 = sorted(debits, key=lambda x: x[0], reverse=True) \n",
"\n",
"debits_ex2"
]
},
{
Expand Down Expand Up @@ -158,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 45,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +200,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 53,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[105.0, 52.5, -26.25, 1050.0, -10.5]"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"balance_1y_ex1 = list(map(lambda x: x * 1.05, balances))\n",
"balance_1y_ex1"
]
},
{
Expand All @@ -195,7 +239,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 49,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +253,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 51,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[1020.0, 2020.0, 515.0]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here\n"
"# your code goes here\n",
"balance_1y_ex2 = list(map(lambda x: x[\"balance\"] * (1+x[\"interest_rate\"]), accounts))\n",
"balance_1y_ex2"
]
},
{
Expand Down Expand Up @@ -246,11 +303,21 @@
"execution_count": null,
"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"
"# your code goes here\n",
"negative_balances = list(filter(lambda x: x<0, balance_1y_ex1))\n",
"print(reduce(lambda x, y: x + y, negative_balances))\n"
]
},
{
Expand All @@ -273,24 +340,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 59,
"id": "da2264b5-298e-4b45-99df-852b94e90d15",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[650, 1600, 275]"
]
},
"execution_count": 59,
"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"
"# your code goes here\n",
"calculate_balance = lambda x: x[\"balance\"] - sum(x[\"withdrawals\"])\n",
"remaining_balance = list(map(calculate_balance, accounts))\n",
"remaining_balance"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +385,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.12"
}
},
"nbformat": 4,
Expand Down