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
146 changes: 121 additions & 25 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": 2,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand All @@ -87,19 +87,36 @@
"id": "16796011-a618-499a-a57f-4ca61d0a77e7",
"metadata": {},
"source": [
"Create a new list called credits that includes all of the debit transactions from the list transactions.\n",
"Create a new list called debits that includes all of the debit transactions from the list transactions.\n",
"\n",
"Use the filter() function to create a new list called debits."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "0781335d-39cf-403d-b86a-ca908a09fe55",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(-1200, 'debit'),\n",
" (-100, 'debit'),\n",
" (-250, 'debit'),\n",
" (-300, 'debit'),\n",
" (-850, 'debit')]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"debits"
]
},
{
Expand All @@ -124,12 +141,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(-1200, 'debit'),\n",
" (-100, 'debit'),\n",
" (-250, 'debit'),\n",
" (-300, 'debit'),\n",
" (-850, 'debit')]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"extract = lambda x: x[1]\n",
"sorted_debits = sorted(debits, key=extract, reverse=True)\n",
"sorted_debits"
]
},
{
Expand Down Expand Up @@ -158,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +204,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[105.0, 52.5, -26.25, 1050.0, -10.5]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"with_interest = list(map(lambda x: x*1.05, balances))\n",
"with_interest"
]
},
{
Expand All @@ -195,7 +243,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +257,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'balance_after_1_year': 1020.0}, {'balance_after_1_year': 2020.0}, {'balance_after_1_year': 515.0}]\n"
]
}
],
"source": [
"# your code goes here\n"
"# Function to compute new balance after 1 year\n",
"def apply_interest(account):\n",
" new_balance = account[\"balance\"] * (1 + account[\"interest_rate\"])\n",
" return {\n",
" \"balance_after_1_year\": round(new_balance, 2)\n",
" }\n",
"# Use map to apply the function to all accounts\n",
"updated_accounts = list(map(apply_interest, accounts))\n",
"\n",
"# Output\n",
"print(updated_accounts)"
]
},
{
Expand Down Expand Up @@ -243,14 +309,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "6284dbd3-e117-411e-8087-352be6deaed4",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"-36.75"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here"
"neg_list = list(filter(lambda x: x < 0, with_interest))\n",
"reduce(lambda x,y: x+y, neg_list)"
]
},
{
Expand All @@ -273,26 +351,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"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"
"# Define the function to calculate remaining balance\n",
"def calculate_balance(account):\n",
" total_withdrawals = sum(account['withdrawals'])\n",
" remaining_balance = account['balance'] - total_withdrawals\n",
" return remaining_balance\n",
"\n",
"# Use map to apply the function to the list of accounts\n",
"remaining_balances = list(map(calculate_balance, accounts))\n",
"\n",
"# Output the result\n",
"print(remaining_balances)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -304,7 +400,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down