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
163 changes: 141 additions & 22 deletions lab-python-lambda-map-reduce-filter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@
"metadata": {},
"outputs": [],
"source": [
"transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]"
"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",
"]"
]
},
{
Expand All @@ -97,9 +108,25 @@
"execution_count": null,
"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"
"from numpy import negative\n",
"\n",
"\n",
"debit_transaction = list(filter(lambda key: key[0] < 0, transactions))\n",
"\n",
"# OR\n",
"\n",
"debit_transaction1 = list(filter(lambda key: key[1] == \"debit\", transactions))\n",
"print(debit_transaction)"
]
},
{
Expand Down Expand Up @@ -127,9 +154,43 @@
"execution_count": null,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(-1200, 'debit'),\n",
" (-850, 'debit'),\n",
" (-300, 'debit'),\n",
" (-250, 'debit'),\n",
" (-100, 'debit')]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"import functools\n",
"\n",
"# 2. Define the lambda function sort_descending (as requested)\n",
"# It returns True (1) if the first amount is greater than the second\n",
"sorted_descending = lambda t1, t2: t1[0] > t2[0]\n",
"\n",
"\n",
"# 3. Use sorted() with a helper to convert the boolean logic into a sorting key\n",
"# We use cmp_to_key because Python 3 sorted() expects a comparison key\n",
"def compare(t1, t2):\n",
" if sorted_descending(t1, t2):\n",
" return -1 # t1 is greater, so it comes \"first\" for descending order\n",
" return 1\n",
"\n",
"\n",
"sorted_debits = sorted(debit_transaction1, key=functools.cmp_to_key(compare))\n",
"\n",
"# BEST WAY\n",
"sorted_deb = sorted(debit_transaction1, key=lambda x: x[0], reverse=False)\n",
"sorted_deb"
]
},
{
Expand Down Expand Up @@ -158,7 +219,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -172,9 +233,22 @@
"execution_count": null,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[105.0, 52.5, -26.25, 1050.0, -10.5]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"interest = 0.05\n",
"new_balance = list(map(lambda b: b * (1 + interest), balances))\n",
"new_balance"
]
},
{
Expand All @@ -201,9 +275,9 @@
"outputs": [],
"source": [
"accounts = [\n",
" {'balance': 1000, 'interest_rate': 0.02},\n",
" {'balance': 2000, 'interest_rate': 0.01},\n",
" {'balance': 500, 'interest_rate': 0.03},\n",
" {\"balance\": 1000, \"interest_rate\": 0.02},\n",
" {\"balance\": 2000, \"interest_rate\": 0.01},\n",
" {\"balance\": 500, \"interest_rate\": 0.03},\n",
"]"
]
},
Expand All @@ -212,9 +286,24 @@
"execution_count": null,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[{'balance': 1020.0}, {'balance': 2020.0}, {'balance': 515.0}]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here\n"
"# your code goes here\n",
"new_balance = list(\n",
" map(lambda acc: {\"balance\": acc[\"balance\"] * (1 + acc[\"interest_rate\"])}, accounts)\n",
")\n",
"new_balance"
]
},
{
Expand Down Expand Up @@ -246,11 +335,25 @@
"execution_count": null,
"id": "6284dbd3-e117-411e-8087-352be6deaed4",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"-2700"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here"
"total = reduce(lambda a, b: a + b[0], sorted_deb, 0)\n",
"total\n",
"\n",
"# something else"
]
},
{
Expand All @@ -276,21 +379,37 @@
"execution_count": null,
"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",
" {\"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"
"\n",
"def calculate_balance(balance_dict):\n",
" return balance_dict[\"balance\"] - sum(balance_dict[\"withdrawals\"])\n",
"\n",
"\n",
"list(map(calculate_balance, accounts))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +423,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down