diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..22d03c5 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": 2, "id": "08463071-9351-4d49-8d29-4fcb817fb177", "metadata": {}, "outputs": [], @@ -94,12 +94,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit')]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "credits = list(filter(lambda x: x[1]== \"credit\", transactions))\n", + "debit =list(filter(lambda x: x[1]== \"debit\", transactions))\n", + "debit" ] }, { @@ -124,12 +142,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (-850, 'debit'),\n", + " (-300, 'debit'),\n", + " (-250, 'debit'),\n", + " (-100, 'debit')]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "sort_descending = lambda t1: t1[0]\n", + "\n", + "sorted_debit = sorted(debit, key=sort_descending)\n", + "sorted_debit\n", + "# sort_descending = lambda t1, t2: t1[0] > t2[0]\n", + "\n", + "# sorted_debit = sorted(debit, key=sort_descending )" ] }, { @@ -158,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, "outputs": [], @@ -169,12 +209,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def calc_interest(balance):\n", + " balance += balance*0.05\n", + " return balance\n", + "\n", + "balances_with_interest = list(map(calc_interest, balances))\n", + "balances_with_interest" ] }, { @@ -195,7 +252,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, "outputs": [], @@ -209,12 +266,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1020.0, 2020.0, 515.0]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here\n" + "accounts = [\n", + " {'balance': 1000, 'interest_rate': 0.02},\n", + " {'balance': 2000, 'interest_rate': 0.01},\n", + " {'balance': 500, 'interest_rate': 0.03},\n", + "]\n", + "# your code goes here\n", + "def calc_interest(balance, interest_rate):\n", + " balance += balance * interest_rate\n", + " return balance\n", + "\n", + "new_balances = map(lambda x: calc_interest(x[\"balance\"], x[\"interest_rate\"]), accounts)\n", + "list(new_balances)" ] }, { @@ -243,14 +322,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "6284dbd3-e117-411e-8087-352be6deaed4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-26.25, -10.5]\n" + ] + }, + { + "data": { + "text/plain": [ + "-36.75" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from functools import reduce\n", "\n", - "# your code goes here" + "# your code goes here\n", + "print(list(filter(lambda x: x <0, balances_with_interest)))\n", + "\n", + "negative_balances = reduce(lambda x,y: x + y,\n", + " filter(lambda x: x <0, balances_with_interest)\n", + ")\n", + "\n", + "negative_balances" ] }, { @@ -273,10 +377,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "da2264b5-298e-4b45-99df-852b94e90d15", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[650, 1600, 275]" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "accounts = [\n", " {'balance': 1000, 'withdrawals': [100, 50, 200]},\n", @@ -284,13 +399,18 @@ " {'balance': 500, 'withdrawals': [50, 100, 75]},\n", "]\n", "\n", - "# your code goes here\n" + "# your code goes here\n", + "def calculate_balance(balance, widthdrawls):\n", + " return balance - sum(widthdrawls)\n", + "\n", + "account_balances = map(lambda x: calculate_balance(x[\"balance\"], x[\"withdrawals\"]), accounts)\n", + "list(account_balances)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" },