diff --git a/.ipynb_checkpoints/lab-python-lambda-map-reduce-filter-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-lambda-map-reduce-filter-checkpoint.ipynb new file mode 100644 index 0000000..042658a --- /dev/null +++ b/.ipynb_checkpoints/lab-python-lambda-map-reduce-filter-checkpoint.ipynb @@ -0,0 +1,471 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Lambda Functions, Map, Reduce, Filter" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: The objective of this lab is to help students gain familiarity with using anonymous lambda functions and map, reduce, and filter methods in Python." + ] + }, + { + "cell_type": "markdown", + "id": "0120f101-3f9a-444c-84b9-a10a990a5f95", + "metadata": {}, + "source": [ + "Lambda functions, map, reduce, and filter are all related to functional programming in Python. \n", + "\n", + "**Lambda functions** are anonymous functions in Python, which means that they do not need to be defined with a name. They are typically used for short, one-line functions that are not going to be used elsewhere in the code. \n", + "\n", + "Lambda functions can take any number of arguments, but they can only contain a single expression. They are often used in combination with other built-in functions like map, reduce, and filter to perform operations on lists or other iterables." + ] + }, + { + "cell_type": "markdown", + "id": "7afb4d9e-63a9-4326-87f1-a0915769721c", + "metadata": {}, + "source": [ + "**Map** is a function that applies a given function to every element of a list, returning a new list with the transformed values. It's a useful way to apply the same operation to every element of a list, without having to write a for loop.\n", + "\n", + "**Reduce** is a function that applies a given function to the first two elements of a list, then applies the same function to the result and the next element, and so on, until it has reduced the list to a single value. It's useful for performing cumulative operations like summing or multiplying a list of numbers.\n", + "\n", + "**Filter** is a function that takes a function and a list, and returns a new list containing only the elements of the original list for which the function returns True. It's a useful way to selectively extract elements from a list based on a certain condition." + ] + }, + { + "cell_type": "markdown", + "id": "053ee1bc-59fa-4cf8-9191-916b6cdc5811", + "metadata": {}, + "source": [ + "All of these concepts can be used to make your code more concise and expressive, especially when working with lists or other iterables. They are also useful in data manipulation tasks, such as cleaning and transforming data in a pandas DataFrame. By practicing with these concepts in the lab, you will gain a better understanding of how to use them in a variety of programming tasks." + ] + }, + { + "cell_type": "markdown", + "id": "65df3710-f236-4818-9a4f-43bfd53cad7c", + "metadata": {}, + "source": [ + "## Challenge 1" + ] + }, + { + "cell_type": "markdown", + "id": "0e704740-6ac4-49d0-b25b-960cf8511a04", + "metadata": {}, + "source": [ + "In this Challenge we will use the following data, which is a list of bank transactions:" + ] + }, + { + "cell_type": "markdown", + "id": "ae7d88f0-a8e7-4f74-98d3-802306bdcf07", + "metadata": {}, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "16796011-a618-499a-a57f-4ca61d0a77e7", + "metadata": {}, + "source": [ + "Create a new list called credits that includes all of the debit transactions from the list transactions.\n", + "\n", + "Use the filter() function to create a new list called debits." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "08463071-9351-4d49-8d29-4fcb817fb177", + "metadata": {}, + "outputs": [], + "source": [ + "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": 7, + "id": "0781335d-39cf-403d-b86a-ca908a09fe55", + "metadata": {}, + "outputs": [], + "source": [ + "debits = []" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3488780e-a81e-4d19-b2c7-69e73a7d13af", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit')]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "for item in transactions:\n", + " if item[1] == \"debit\":\n", + " debits.append(item)\n", + "debits" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5b3af567-a05f-4250-987b-3640c48b47ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit')]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(lambda x: x[1] == 'debit', transactions))" + ] + }, + { + "cell_type": "markdown", + "id": "37ca34af-7f87-44f2-a45d-1a1e2ef8c194", + "metadata": {}, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "546c5aa2-6f16-4285-9c47-0f788b23552c", + "metadata": {}, + "source": [ + "Create a new list that includes all of the debit transactions from the list transactions, sorted in descending order by amount." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "f3d2d768-8f39-4587-9627-5e985dc10d6d", + "metadata": {}, + "outputs": [], + "source": [ + "sorted_list = sorted(transactions, reverse = True )" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "3e6efcf9-0b5d-4c45-83a7-560e705e3848", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(5000, 'credit'),\n", + " (2500, 'credit'),\n", + " (1500, 'credit'),\n", + " (1000, 'credit'),\n", + " (850, 'credit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit'),\n", + " (-1200, 'debit')]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted_list" + ] + }, + { + "cell_type": "markdown", + "id": "bee57a6e-19a3-4708-9b70-3b41a215353f", + "metadata": {}, + "source": [ + "## Challenge 2: Interest Calculation" + ] + }, + { + "cell_type": "markdown", + "id": "4e7c9f00-7c48-4c39-9de1-14db7fc468eb", + "metadata": {}, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "3500df4c-91bd-4f8f-9ffe-4d6a51460bc6", + "metadata": {}, + "source": [ + "Write Python code to take a list of bank account balances, and returns a new list containing the balance after one year of interest has been added. Use the map function to apply this function to the list of bank accounts, and take an interest rate of 0.05." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", + "metadata": {}, + "outputs": [], + "source": [ + "# create list of bank account balances\n", + "balances = [100, 50, -25, 1000, -10]" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "2f253b7e-5300-4819-b38f-9fc090554f51", + "metadata": {}, + "outputs": [], + "source": [ + "new_balances = []\n", + "for item in balances:\n", + " new_value = item * 1.05\n", + " new_balances.append(new_value)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "f5743033-6578-4062-a2e9-04f741a27cdc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_balances" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "744b0d04-36f9-4ebf-a5be-54af3c733d30", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(lambda item: item * 1.05, balances))" + ] + }, + { + "cell_type": "markdown", + "id": "6a515b75-8ccb-4ee3-9dc0-f36d72ac66a0", + "metadata": {}, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "7022779a-7611-428b-8f7d-138e923a63b8", + "metadata": {}, + "source": [ + "Write Python code to take a list of bank account dictionaries, each containing the account balance and interest rate, and returns a new list of dictionaries containing the balance after one year of interest has been added. Use the map function to apply this function to the list of bank accounts." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", + "metadata": {}, + "outputs": [], + "source": [ + "accounts = [\n", + " {'balance': 1000, 'interest_rate': 0.02},\n", + " {'balance': 2000, 'interest_rate': 0.01},\n", + " {'balance': 500, 'interest_rate': 0.03},\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "ef2a6243-f798-440d-a7d0-d8cdd8f991b6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'balance': 1000, 'interest rate': 0.02, 'updated': 1020.0}\n", + "{'balance': 2000, 'interest rate': 0.01, 'updated': 2020.0}\n", + "{'balance': 500, 'interest rate': 0.03, 'updated': 515.0}\n" + ] + } + ], + "source": [ + "for items in range(len(accounts)):\n", + " news = {\"balance\" : accounts[items][\"balance\"], \"interest rate\": accounts[items]['interest_rate'], \"updated\": accounts[items][\"balance\"] + accounts[items][\"balance\"] * accounts[items][\"interest_rate\"]}\n", + " print(news)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "0906a9b0-d567-4786-96f2-5755611b885e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'balance': 1000, 'interest rate': 0.02, 'updated': 1020.0},\n", + " {'balance': 2000, 'interest rate': 0.01, 'updated': 2020.0},\n", + " {'balance': 500, 'interest rate': 0.03, 'updated': 515.0}]" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(lambda item: {\"balance\" : item[\"balance\"], \"interest rate\": item['interest_rate'], \"updated\": item[\"balance\"] + item[\"balance\"] * item[\"interest_rate\"]}, accounts))" + ] + }, + { + "cell_type": "markdown", + "id": "69788c08-90c5-4c15-be3d-55ac4a963fe2", + "metadata": {}, + "source": [ + "## Challenge 3: Balance Reduction" + ] + }, + { + "cell_type": "markdown", + "id": "81536227-379c-461f-b564-6695957bd0c1", + "metadata": {}, + "source": [ + "### Exercise 1" + ] + }, + { + "cell_type": "markdown", + "id": "1006852a-42fc-4aae-bc0a-c771a94f6b2d", + "metadata": {}, + "source": [ + "Write Python code to take the new list of bank account balances (balances list after applying an interest_rate of 0.05, result of Challenge 1 Exercise 1), and print the total amount of negative balances. Use filter and reduce function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6284dbd3-e117-411e-8087-352be6deaed4", + "metadata": {}, + "outputs": [], + "source": [ + "from functools import reduce\n", + "\n", + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "3926594f-3204-4de3-a6bf-bd87069a82cc", + "metadata": {}, + "source": [ + "### Exercise 2" + ] + }, + { + "cell_type": "markdown", + "id": "6149139b-43db-473b-a10e-8bff9a38dcbf", + "metadata": {}, + "source": [ + "Write a Python function called calculate_balance that takes a bank account dictionary as an argument and returns the remaining balance after subtracting all the withdrawals.\n", + "\n", + "Then, use the map function and the calculate_balance function to apply it to the list accounts. This should give you a list of remaining balances after all the withdrawals have been subtracted." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da2264b5-298e-4b45-99df-852b94e90d15", + "metadata": {}, + "outputs": [], + "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" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-lambda-map-reduce-filter.ipynb b/lab-python-lambda-map-reduce-filter.ipynb index 96c9781..042658a 100644 --- a/lab-python-lambda-map-reduce-filter.ipynb +++ b/lab-python-lambda-map-reduce-filter.ipynb @@ -64,16 +64,6 @@ "In this Challenge we will use the following data, which is a list of bank transactions:" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "08463071-9351-4d49-8d29-4fcb817fb177", - "metadata": {}, - "outputs": [], - "source": [ - "transactions = [(-1200, 'debit'), (2500, 'credit'), (-100, 'debit'), (850, 'credit'), (-250, 'debit'), (1500, 'credit'), (-300, 'debit'), (5000, 'credit'), (-850, 'debit'), (1000, 'credit')]" - ] - }, { "cell_type": "markdown", "id": "ae7d88f0-a8e7-4f74-98d3-802306bdcf07", @@ -94,12 +84,75 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, + "id": "08463071-9351-4d49-8d29-4fcb817fb177", + "metadata": {}, + "outputs": [], + "source": [ + "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": 7, "id": "0781335d-39cf-403d-b86a-ca908a09fe55", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "debits = []" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3488780e-a81e-4d19-b2c7-69e73a7d13af", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit')]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "for item in transactions:\n", + " if item[1] == \"debit\":\n", + " debits.append(item)\n", + "debits" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5b3af567-a05f-4250-987b-3640c48b47ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(-1200, 'debit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit')]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(lambda x: x[1] == 'debit', transactions))" ] }, { @@ -115,21 +168,47 @@ "id": "546c5aa2-6f16-4285-9c47-0f788b23552c", "metadata": {}, "source": [ - "Create a new list that includes all of the debit transactions from the list transactions, sorted in descending order by amount.\n", - "\n", - "- Use the previously created debits list.\n", - "- Define a lambda function called sort_descending that takes two tuples and returns True if the transaction amount of the first tuple is greater than the transaction amount of the second tuple.\n", - "- Use the sorted() function with sort_descending and debits to create a new list." + "Create a new list that includes all of the debit transactions from the list transactions, sorted in descending order by amount." ] }, { "cell_type": "code", - "execution_count": null, - "id": "25073469-7258-4fc6-b0a0-ef8ea57688fe", + "execution_count": 25, + "id": "f3d2d768-8f39-4587-9627-5e985dc10d6d", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "sorted_list = sorted(transactions, reverse = True )" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "3e6efcf9-0b5d-4c45-83a7-560e705e3848", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(5000, 'credit'),\n", + " (2500, 'credit'),\n", + " (1500, 'credit'),\n", + " (1000, 'credit'),\n", + " (850, 'credit'),\n", + " (-100, 'debit'),\n", + " (-250, 'debit'),\n", + " (-300, 'debit'),\n", + " (-850, 'debit'),\n", + " (-1200, 'debit')]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted_list" ] }, { @@ -158,7 +237,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "e1de9d03-f029-4e2e-9733-ae92e3de7527", "metadata": {}, "outputs": [], @@ -169,12 +248,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "id": "2f253b7e-5300-4819-b38f-9fc090554f51", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "new_balances = []\n", + "for item in balances:\n", + " new_value = item * 1.05\n", + " new_balances.append(new_value)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "f5743033-6578-4062-a2e9-04f741a27cdc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_balances" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "744b0d04-36f9-4ebf-a5be-54af3c733d30", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[105.0, 52.5, -26.25, 1050.0, -10.5]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(lambda item: item * 1.05, balances))" ] }, { @@ -195,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "id": "69e24c3b-385e-44d6-a8ed-705a3f58e696", "metadata": {}, "outputs": [], @@ -209,12 +333,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, + "id": "ef2a6243-f798-440d-a7d0-d8cdd8f991b6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'balance': 1000, 'interest rate': 0.02, 'updated': 1020.0}\n", + "{'balance': 2000, 'interest rate': 0.01, 'updated': 2020.0}\n", + "{'balance': 500, 'interest rate': 0.03, 'updated': 515.0}\n" + ] + } + ], + "source": [ + "for items in range(len(accounts)):\n", + " news = {\"balance\" : accounts[items][\"balance\"], \"interest rate\": accounts[items]['interest_rate'], \"updated\": accounts[items][\"balance\"] + accounts[items][\"balance\"] * accounts[items][\"interest_rate\"]}\n", + " print(news)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, "id": "0906a9b0-d567-4786-96f2-5755611b885e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[{'balance': 1000, 'interest rate': 0.02, 'updated': 1020.0},\n", + " {'balance': 2000, 'interest rate': 0.01, 'updated': 2020.0},\n", + " {'balance': 500, 'interest rate': 0.03, 'updated': 515.0}]" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here\n" + "list(map(lambda item: {\"balance\" : item[\"balance\"], \"interest rate\": item['interest_rate'], \"updated\": item[\"balance\"] + item[\"balance\"] * item[\"interest_rate\"]}, accounts))" ] }, { @@ -304,7 +463,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,