diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..ad76c32 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,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "credits=list(filter(lambda n:n[1]=='credit',transactions))\n", + "debits=list(filter(lambda n:n[1]=='debit',transactions))" ] }, { @@ -124,12 +126,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 93, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(-1200, 'debit'), (-100, 'debit'), (-250, 'debit'), (-300, 'debit'), (-850, 'debit')]\n" + ] + }, + { + "data": { + "text/plain": [ + "[(-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit'),\n", + " (-1200, 'debit')]" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "print(debits)\n", + "sort_descending= lambda a,b:True if a[0]>b[0] else False\n", + "sort_descending= lambda a:True if a[0][0]>a[1][0] else False\n", + "# v=sort_descending((1,2),(3,4))\n", + "j=sorted(debits, key=lambda elem:elem[0], reverse=True)" ] }, { @@ -158,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, "outputs": [], @@ -169,12 +198,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "add_interest= lambda a:a+a*0.05\n", + "new_bals=list(map(add_interest,balances))" ] }, { @@ -195,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, "outputs": [], @@ -209,12 +240,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'balance': 1061.208, 'interest_rate': 0.02}, {'balance': 2060.602, 'interest_rate': 0.01}, {'balance': 546.3635, 'interest_rate': 0.03}]\n" + ] + } + ], "source": [ - "# your code goes here\n" + "# your code goes here\n", + "#old_dict={'balance': 1000, 'interest_rate': 0.02}\n", + "def update_bal(old_dict):\n", + " new_dict=old_dict\n", + " new_dict['balance']=old_dict['balance']*(1+old_dict['interest_rate'])\n", + " return new_dict\n", + "#print(list(map(update_bal,accounts)))" ] }, { @@ -243,13 +288,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "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", + "def filt_red(new_bals):\n", + " neg_bals=list(filter(lambda x:x<0, new_bals))\n", + " if neg_bals:\n", + " return reduce(lambda x,y:x+y,neg_bals)\n", + " else:\n", + " return 0\n", + " #print(reduce(lambda x,y:x+y, list(filter(lambda x:x<0, new_bals))))\n", + "print(filt_red(new_bals))\n", "# your code goes here" ] }, @@ -273,10 +333,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "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,8 +352,21 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# your code goes here\n", + "def calculate_balance(one_account):\n", + " #one_account['balance']=sum(one_account['withdrawals'])\n", + " return one_account['balance']-sum(one_account['withdrawals'])\n", + "resulting=list(map(calculate_balance,accounts))\n", + "print(resulting)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "106a9626-07ba-4d6f-86d7-cf8cef2db935", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -304,7 +385,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,