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
179 changes: 156 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": 7,
"id": "08463071-9351-4d49-8d29-4fcb817fb177",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -94,12 +94,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "0781335d-39cf-403d-b86a-ca908a09fe55",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1200, -100, -250, -300, -850]\n"
]
}
],
"source": [
"# your code goes here"
"debits_only = [amount for amount, t_type in transactions if t_type == 'debit']\n",
"lista_debitosdebits = filter (debits_only, transactions)\n",
"print (lista_debitos)\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "a07db8bc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n"
]
}
],
"source": [
"is_debit = lambda x: x[1] == 'debit'\n",
"debits = list(filter(is_debit, transactions))\n",
"print(debits)"
]
},
{
Expand All @@ -124,12 +157,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"id": "25073469-7258-4fc6-b0a0-ef8ea57688fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-100, -250, -300, -850, -1200]\n"
]
}
],
"source": [
"# your code goes here"
"lista_debitosdebits = [-1200, -100, -250, -300, -850]\n",
"\n",
"lista_debitosdebits_ordenada= sorted(lista_debitosdebits, reverse = True)\n",
"print (lista_debitosdebits_ordenada)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "04852f9f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit'), (-1200, 'debit')]\n"
]
}
],
"source": [
"debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
"debits_sorted = sorted(debits, key=lambda x: x[0], reverse=True)\n",
"print(debits_sorted)"
]
},
{
Expand Down Expand Up @@ -158,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"id": "e1de9d03-f029-4e2e-9733-ae92e3de7527",
"metadata": {},
"outputs": [],
Expand All @@ -169,12 +234,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"id": "2f253b7e-5300-4819-b38f-9fc090554f51",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[105.0, 52.5, -26.25, 1050.0, -10.5]\n"
]
}
],
"source": [
"# your code goes here"
"\n",
"def interes_anual (n):\n",
" return n * 1.05\n",
"\n",
"balances_año1 = map (interes_anual, balances)\n",
"print (list(balances_año1))\n"
]
},
{
Expand All @@ -195,7 +273,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "69e24c3b-385e-44d6-a8ed-705a3f58e696",
"metadata": {},
"outputs": [],
Expand All @@ -209,12 +287,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"id": "0906a9b0-d567-4786-96f2-5755611b885e",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1020.0, 2020.0, 515.0]\n"
]
}
],
"source": [
"def balances_nuevos(account):\n",
" return account['balance'] * (1 + account['interest_rate']) \n",
"balances_nuevos = map(balances_nuevos, accounts)\n",
"print(list(balances_nuevos))\n"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "335c34f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'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"
"def balances_actualizados (account):\n",
" return {'balance': account['balance'] * (1 + account['interest_rate']), 'interest_rate': account['interest_rate']}\n",
"balance_añox1 = list(map(balances_actualizados, accounts))\n",
"print(balance_añox1)"
]
},
{
Expand Down Expand Up @@ -243,14 +353,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 46,
"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"
"balances_año1 = map (interes_anual, balances)\n",
"balances_negativos = filter (lambda x: x < 0, balances_año1)\n",
"total_negativos = reduce (lambda x, y: x + y, balances_negativos)\n",
"print (total_negativos) \n"
]
},
{
Expand All @@ -273,24 +394,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 45,
"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"
"def calcular_balances_finales(account):\n",
" total_withdrawals = reduce(lambda x, y: x + y, account['withdrawals'])\n",
" return account['balance'] - total_withdrawals\n",
"balances_finales = map(calcular_balances_finales, accounts)\n",
"print(list(balances_finales))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -304,7 +437,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down