Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab Solved Juan Duran #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
158 changes: 135 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": 2,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -94,12 +94,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"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"
"# Use filter to create a list of debits\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"\n",
"# Use list comprehension to create a list of credits\n",
"credits = [transaction for transaction in transactions if transaction[1] == 'credit']\n",
"\n",
"print(\"Debits:\", debits)\n",
"print(\"Credits:\", credits)"
]
},
{
Expand All @@ -124,12 +140,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"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"
"# Use filter to create a list of debits\n",
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"\n",
"# Define the sort_descending lambda function\n",
"sort_descending = lambda x: x[0]\n",
"\n",
"# Use sorted() to create a new list sorted in descending order by amount\n",
"sorted_debits = sorted(debits, key=sort_descending, reverse=True)\n",
"\n",
"print(\"Sorted Debits:\", sorted_debits)"
]
},
{
Expand Down Expand Up @@ -158,7 +191,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +202,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Balances after one year of interest: [105.0, 52.5, -26.25, 1050.0, -10.5]\n"
]
}
],
"source": [
"# your code goes here"
"# Define a function to calculate the balance after one year of interest\n",
"def add_interest(balance: float, rate: float) -> float:\n",
" \"\"\"\n",
" Receives a balance and an interest rate, and returns the balance after one year of interest.\n",
" \"\"\"\n",
" return balance * (1 + rate)\n",
"\n",
"# Interest rate\n",
"interest_rate = 0.05\n",
"\n",
"# Use map to apply the add_interest function to the list of balances\n",
"new_balances = list(map(lambda balance: add_interest(balance, interest_rate), balances))\n",
"\n",
"print(\"Balances after one year of interest:\", new_balances)"
]
},
{
Expand All @@ -195,7 +249,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +263,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated accounts after one year of 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"
"# Define a function to calculate the balance after one year of interest\n",
"def add_interest(account: dict) -> dict:\n",
" \"\"\"\n",
" Receives a dictionary with 'balance' and 'interest_rate' keys,\n",
" and returns a new dictionary with the updated balance after one year of interest.\n",
" \"\"\"\n",
" new_balance = account['balance'] * (1 + account['interest_rate'])\n",
" return {'balance': new_balance, 'interest_rate': account['interest_rate']}\n",
"\n",
"# Use map to apply the add_interest function to the list of accounts\n",
"updated_accounts = list(map(add_interest, accounts))\n",
"\n",
"print(\"Updated accounts after one year of interest:\", updated_accounts)\n"
]
},
{
Expand Down Expand Up @@ -243,14 +317,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "6284dbd3-e117-411e-8087-352be6deaed4",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total amount of negative balances: -36.75\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"# your code goes here"
"# List of bank account balances after applying an interest rate of 0.05\n",
"balances = [105.0, 52.5, -26.25, 1050.0, -10.5]\n",
"\n",
"# Use filter to create a list of negative balances\n",
"negative_balances = list(filter(lambda balance: balance < 0, balances))\n",
"\n",
"# Use reduce to sum the negative balances\n",
"total_negative_balances = reduce(lambda acc, balance: acc + balance, negative_balances, 0)\n",
"\n",
"print(\"Total amount of negative balances:\", total_negative_balances)"
]
},
{
Expand All @@ -273,24 +364,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "da2264b5-298e-4b45-99df-852b94e90d15",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remaining balances after all withdrawals: [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 a function to calculate the remaining balance after withdrawals\n",
"def calculate_balance(account: dict) -> float:\n",
" \"\"\"\n",
" Receives a dictionary with 'balance' and 'withdrawals' keys,\n",
" and returns the remaining balance after subtracting all the withdrawals.\n",
" \"\"\"\n",
" total_withdrawals = sum(account['withdrawals'])\n",
" remaining_balance = account['balance'] - total_withdrawals\n",
" return remaining_balance\n",
"\n",
"# Use map to apply the calculate_balance function to the list of accounts\n",
"remaining_balances = list(map(calculate_balance, accounts))\n",
"\n",
"print(\"Remaining balances after all withdrawals:\", remaining_balances)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +416,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down