diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..c4546e4 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": 3, "id": "08463071-9351-4d49-8d29-4fcb817fb177", "metadata": {}, "outputs": [], @@ -74,6 +74,36 @@ "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]" ] }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9c04ad39", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (2500, 'credit'),\n", + " (-100, 'debit'),\n", + " (850, 'credit'),\n", + " (-250, 'debit'),\n", + " (1500, 'credit'),\n", + " (-300, 'debit'),\n", + " (5000, 'credit'),\n", + " (-850, 'debit'),\n", + " (1000, 'credit')]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transactions" + ] + }, { "cell_type": "markdown", "id": "ae7d88f0-a8e7-4f74-98d3-802306bdcf07", @@ -94,12 +124,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "debit = lambda x: x[1] == 'debit'\n", + "\n", + "debit_transaction = filter(debit,transactions)\n", + "\n", + "debit_list = list(debit_transaction)" ] }, { @@ -124,12 +158,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit'),\n", + " (-1200, 'debit')]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "debit_list \n", + "\n", + "\n", + "sorted(debit_list,key= lambda t:t[0],reverse=True)" ] }, { @@ -158,7 +210,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, "outputs": [], @@ -169,12 +221,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "add_interest_func = lambda x: x*1.05\n", + "\n", + "balance_interest = list(map(add_interest_func,balances))\n", + "\n", + "balance_interest" ] }, { @@ -195,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, "outputs": [], @@ -209,12 +276,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[{'balance': 1000, 'interest_rate': 0.02, 'new_balance': 1020.0},\n", + " {'balance': 2000, 'interest_rate': 0.01, 'new_balance': 2020.0},\n", + " {'balance': 500, 'interest_rate': 0.03, 'new_balance': 515.0}]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here\n" + "accounts_func = lambda x: {**x,'new_balance':x['balance']*(1+x['interest_rate'])}\n", + "accounts_new = map(accounts_func,accounts)\n", + "\n", + "list(accounts_new)" ] }, { @@ -243,14 +326,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]\n" + ] + }, + { + "data": { + "text/plain": [ + "-36.75" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "print(balance_interest)\n", + "\n", + "\n", + "balance_interest_filtered = list(filter(lambda b: b<0 ,balance_interest))\n", + "\n", + "reduce(lambda x,y:x+y, balance_interest_filtered)\n" ] }, { @@ -273,24 +379,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 93, "id": "da2264b5-298e-4b45-99df-852b94e90d15", "metadata": {}, "outputs": [], "source": [ + "from functools import reduce\n", + "\n", "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" + "]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "id": "62d8b930", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_balance(account):\n", + " balance = list(account.values())[0]\n", + " withdrawal = reduce(lambda x,y: x+y,list(account.values())[1])\n", + " remaining = balance - withdrawal\n", + " return remaining\n" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "b4b80591", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[650, 1600, 275]" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(calculate_balance,accounts))" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "py310", "language": "python", "name": "python3" }, @@ -304,7 +445,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.18" } }, "nbformat": 4,