From f35ac737d5ef767ea690cf0ba18cecbb62253a0f Mon Sep 17 00:00:00 2001 From: Wunkifi Date: Fri, 9 May 2025 17:16:25 +0200 Subject: [PATCH] Lab solved by Santi Semprun --- lab-python-lambda-map-reduce-filter.ipynb | 108 +++++++++++++++++----- 1 file changed, 85 insertions(+), 23 deletions(-) diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..cb40a08 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "08463071-9351-4d49-8d29-4fcb817fb177", "metadata": {}, "outputs": [], @@ -94,12 +94,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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" + "credits = list(filter(lambda item: item[1] == 'debit', transactions))\n", + "print(credits)" ] }, { @@ -124,12 +133,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(-1200, 'debit'), (-850, 'debit'), (-300, 'debit'), (-250, 'debit'), (-100, 'debit')]\n" + ] + } + ], "source": [ - "# your code goes here" + "\n", + "\n", + "sorted_list = sorted(credits, key = lambda item:item[0])\n", + "print(sorted_list)\n" ] }, { @@ -158,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, "outputs": [], @@ -169,12 +189,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[95.0, 47.5, -23.75, 950.0, -9.5]\n" + ] + } + ], "source": [ - "# your code goes here" + "balances_after_rates = list(map(lambda x: x - x*0.05, balances))\n", + "print(balances_after_rates)" ] }, { @@ -195,7 +224,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, "outputs": [], @@ -209,12 +238,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[980.0, 1980.0, 485.0]\n" + ] + } + ], "source": [ - "# your code goes here\n" + "updated_accounts = list(map(lambda x: x['balance'] - x['balance']*x['interest_rate'], accounts ))\n", + "print(updated_accounts)" ] }, { @@ -243,14 +281,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-33.25\n" + ] + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "negative_balances = list(filter(lambda x:x < 0, balances_after_rates))\n", + "sum_negative_balances = reduce(lambda x,y:x+y, negative_balances)\n", + "print(sum_negative_balances)" ] }, { @@ -273,10 +321,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "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", @@ -284,13 +340,19 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# your code goes here\n", + "def calculate_balance(accounts_list):\n", + " for account in accounts_list:\n", + " account['balance'] = sum(account['withdrawals'])\n", + " print(account)\n", + "new_accounts = list(map(lambda account:account['balance']-sum(account['withdrawals']),accounts))\n", + "print(new_accounts)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -304,7 +366,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,