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
292 changes: 271 additions & 21 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": 13,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -94,12 +94,59 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "0781335d-39cf-403d-b86a-ca908a09fe55",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Debits: [(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n",
"Credits: [(2500, 'credit'), (850, 'credit'), (1500, 'credit'), (5000, 'credit'), (1000, 'credit')]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"# Challenge 1. Exercise 1:\n",
"\n",
"# use the following data, which is a list of bank transactions:\n",
"# 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.\n",
"# Use the filter() function to create a new list called debits.\n",
"\n",
"transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n",
"\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"credits = list(filter(lambda x: x[1] == 'credit', transactions))\n",
"print(\"Debits:\", debits)\n",
"print(\"Credits:\", credits)\n"
]
},
{
"cell_type": "markdown",
"id": "3a20fc61",
"metadata": {},
"source": [
"#### **Note on the exercise interpretation**\n",
"\n",
"The exercise statement says: \n",
"> *\"Create a new list called credits that includes all of the debit transactions.\"*\n",
"\n",
"Taken literally, this would result in:\n",
"- a list called `credits` containing **debit** transactions, and \n",
"- another list called `debits` also containing **debit** transactions,\n",
"\n",
"which does not make logical sense given the context.\n",
"\n",
"For this reason, I assumed there is a typo in the statement and interpreted the task as:\n",
"- `credits` → transactions with type `\"credit\"`\n",
"- `debits` → transactions with type `\"debit\"`\n",
"\n",
"From my perspective, this interpretation is consistent with the variable names, the domain logic, and the intended learning objective of the lab. \n",
"I appreciate feedback if a different interpretation was intended.\n"
]
},
{
Expand Down Expand Up @@ -127,17 +174,83 @@
"execution_count": null,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorted Debits: [(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n"
]
}
],
"source": [
"# your code goes here\n",
"\n",
"# Challenge 1. Exercise 2:\n",
"\n",
"# Create a new list that includes all of the debit transactions from the list transactions, sorted in descending order by amount.\n",
"\n",
"# Use the previously created debits list.\n",
"transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n",
"\n",
"# Define a lambda function called sort_descending \n",
"# that takes two tuples and returns True if the transaction amount of the first tuple is greater than the transaction amount of the second tuple.\n",
"sort_descending = lambda x, y: x[0] > y[0]\n",
"\n",
"# Use the sorted() function with sort_descending and debits to create a new list.\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"sorted_debits = sorted(debits, key=lambda x: x[0], reverse=True)\n",
"print(\"Sorted Debits:\", sorted_debits)\n",
"\n",
"# expected output: [(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n",
"\n",
"# This solution uses key + reverse in sorted()."
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5497bccc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorted Debits: [(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"# Challenge 1. Exercise 2:\n",
"\n",
"# Create a new list that includes all of the debit transactions from the list transactions, sorted in descending order by amount.\n",
"\n",
"# Use the previously created debits list.\n",
"# Define a lambda function called sort_descending \n",
"# that takes two tuples and returns True if the transaction amount of the first tuple is greater than the transaction amount of the second tuple.\n",
"# Use the sorted() function with sort_descending and debits to create a new list.\n",
"\n",
"from functools import cmp_to_key\n",
"transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"sort_descending = lambda x, y: -1 if x[0] > y[0] else (1 if x[0] < y[0] else 0)\n",
"sorted_debits = sorted(debits, key=cmp_to_key(sort_descending))\n",
"print(\"Sorted Debits:\", sorted_debits)\n",
"\n",
"# expected output: [(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n",
"\n",
"# This solution uses comparator comp_to_key with sorted()."
]
},
{
"cell_type": "markdown",
"id": "bee57a6e-19a3-4708-9b70-3b41a215353f",
"metadata": {},
"source": [
"## Challenge 2: Interest Calculation"
"## Challenge 2: Interest Calculation "
]
},
{
Expand Down Expand Up @@ -169,12 +282,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Balances after interest: [105.0, 52.5, -26.25, 1050.0, -10.5]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"# Write Python code to take a list of bank account balances\n",
"# and returns a new list containing the balance after one year of interest has been added.\n",
"# Use the map function to apply this function to the list of bank accounts\n",
"# and take an interest rate of 0.05.\n",
"\n",
"balances = [100, 50, -25, 1000, -10]\n",
"\n",
"def add_interest(balance):\n",
" return balance + (balance * 0.05)\n",
"\n",
"interest_balances = list(map(add_interest, balances))\n",
"print(\"Balances after interest:\", interest_balances)\n"
]
},
{
Expand All @@ -195,7 +329,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +343,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accounts after interest: [{'balance': 1020.0, 'interest_rate': 0.02}, {'balance': 2020.0, 'interest_rate': 0.01}, {'balance': 515.0, 'interest_rate': 0.03}]\n"
]
}
],
"source": [
"# your code goes here\n"
"# your code goes here\n",
"\n",
"# Write Python code to take a list of bank account dictionaries,\n",
"# each containing the account balance and interest rate,\n",
"# and returns a new list of dictionaries containing the balance after one year of interest has been added.\n",
"# Use the map function to apply this function to the list of bank accounts.\n",
"\n",
"accounts = [\n",
" {'balance': 1000, 'interest_rate': 0.02},\n",
" {'balance': 2000, 'interest_rate': 0.01},\n",
" {'balance': 500, 'interest_rate': 0.03},\n",
"]\n",
"\n",
"def add_interest_account(account):\n",
" new_balance = account['balance'] + (account['balance'] * account['interest_rate'])\n",
" return {'balance': new_balance, 'interest_rate': account['interest_rate']}\n",
"interest_accounts = list(map(add_interest_account, accounts))\n",
"print(\"Accounts after interest:\", interest_accounts)\n"
]
},
{
Expand Down Expand Up @@ -246,11 +405,82 @@
"execution_count": null,
"id": "6284dbd3-e117-411e-8087-352be6deaed4",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Balances after interest: [105.0, 52.5, -26.25, 1050.0, -10.5]\n",
"Total negative balances: -36.75\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here\n",
"\n",
"# Write Python code to take the new list of bank account balances \n",
"# (balances list after applying an interest_rate of 0.05,\n",
"# result of Challenge 1 Exercise 1), new_balance = [105.0, 52.5, -26.25, 1050.0, -10.5]\n",
"# and print the total amount of negative balances. Use filter and reduce function.\n",
"\n",
"balances = [100, 50, -25, 1000, -10]\n",
"\n",
"def add_interest(balance):\n",
" return balance + (balance * 0.05)\n",
"\n",
"interest_balances = list(map(add_interest, balances))\n",
"print(\"Balances after interest:\", interest_balances)\n",
"\n",
"negative_balances = list(filter(lambda x: x < 0, interest_balances))\n",
"total_negative = reduce(lambda x, y: x + y, negative_balances)\n",
"print(\"Total negative balances:\", total_negative)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "935219e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total negative balances: -36.75\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here"
"# your code goes here\n",
"\n",
"# Write Python code to take the new list of bank account balances \n",
"# (balances list after applying an interest_rate of 0.05,\n",
"# result of Challenge 1 Exercise 1), # new_balance = [105.0, 52.5, -26.25, 1050.0, -10.5]\n",
"# and print the total amount of negative balances. Use filter and reduce function.\n",
"\n",
"balances = [100, 50, -25, 1000, -10]\n",
"\n",
"new_balance = [105.0, 52.5, -26.25, 1050.0, -10.5]\n",
"\n",
"negative_balances = list(filter(lambda x: x < 0, new_balance))\n",
"total_negative = reduce(lambda x, y: x + y, negative_balances)\n",
"print(\"Total negative balances:\", total_negative)"
]
},
{
"cell_type": "markdown",
"id": "848584f2",
"metadata": {},
"source": [
"**Note on exercise interpretation:**\n",
">\n",
"> While working on this exercise, I noticed a potential inconsistency in the exercise reference. The statement mentions Challenge 1, Exercise 1; however, based on the overall structure and logic of the lab, that exercise focuses on list creation rather than balance calculations.\n",
"For this reason, I assumed that the intended reference was the exercise involving balance transformations, and I proceeded by using the balances obtained in the corresponding balance-related exercise, applying the required operations (filter and reduce) as instructed.\n",
"This interpretation was made to ensure logical consistency across the lab. I am, of course, open to feedback in case a different interpretation was intended.\n"
]
},
{
Expand All @@ -273,24 +503,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "da2264b5-298e-4b45-99df-852b94e90d15",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remaining balances: [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",
"\n",
"# Write a Python function called calculate_balance that takes a bank account dictionary as an argument\n",
"# and returns the remaining balance after subtracting all the withdrawals.\n",
"# Then, use the map function and the calculate_balance function to apply it to the list accounts. \n",
"# This should give you a list of remaining balances after all the withdrawals have been subtracted.\n",
"\n",
"def calculate_balance(account):\n",
" total_withdrawals = sum(account['withdrawals'])\n",
" return account['balance'] - total_withdrawals\n",
"\n",
"remaining_balances = list(map(calculate_balance, accounts))\n",
"print(\"Remaining balances:\", remaining_balances)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +554,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down