Assignment/Algorithm.png
Assignment/Assignment 2 notebook.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Important Note For guidance on how to write in markdown, please consult [this documentation](https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two categories of items added to the budget planner:\n",
"1. Needs are items and services that are essential for the survival and cannot be avoided.\n",
"2. Wants are items and services that are nice to have, but that one can live without them.\n",
"\n",
"Hence, needs will always be considered as a part of the what to have and can never be skipped. But, wants contains list of items or services like watching movie, pizza, etc. which can be avoided to achieve the desired savings."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1 Step-By-Step Algorithm\n",
"Use the cell below to demonstrate your understanding of the problem. Make sure to explain in detail how your algorithm works including the inputs, the outputs and the processes used.\n",
"\n",
"**Note: Make sure that you explain which data structure you chose and justify your choice**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The algorithm to implement a budget planner requires following inputs from the user:\n",
"
\n",
"- Monthly budget amount
\n",
"- Amount of money to be saved.
\n",
"- Total number of items that are considered as needs.
\n",
"- Total number of items that are considered as wants.
\n",
"- For each item of the list (needs and wants) following 3 fields are required:
\n",
"\n",
"- Item/Service name
\n",
"- Price/Cost associated with it.
\n",
"- It's priority on a scale of 1(lowest) to 10(highest)
\n",
"
\n",
"
\n",
"\n",
"As each item will be stored in a sequential manner, i've used list data structure to store the items in the respective lists. \n",
"\n",
"Each item in the list will store 3 fields associated with it. Hence, for this i've used tuple data structure.\n",
"\n",
"#### After the user has entered all the required information, the algorithm works as follows:\n",
"
\n",
"- Sort the two lists which contain details about items which are needed and wanted in descending order by priority and ascending order by it's price.
\n",
"- Initially set balance to the monthlyBudget.
\n",
"- Keep adding the items of the needsList to whatToBuy list and keep decrementing the balance.
\n",
"- After all the items from the needs list have been added to what to buy, check if balance is greater than the amount of money to be saved. If so, then add the items from the wants list and keep reducing the balance with the price of the item. Else add the item to list of what not to buy list.
\n",
"- Once both the lists have been processed, display the list of items which user can buy (whatToBuy) and which the user can skip buying to achieve desired savings (whatNotToBuy).
\n",
"- If at some point, while adding the items from the needsList, if the value of balance becomes negative, then it means that the budget for the month is too low and even the essentails cannot be purchased.
\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 Flowchart Diagram\n",
"Use the cell below to upload the flowchart that shows how the algorithm works (the visual representation of your algorithm).\n",
"\n",
"\n",
"**NOTE:** Let \"my_image.png\" be an image which is in the same directory as your jupyter notebook. If you would like to include that image on this notebook, then you can simply write in a code cell:\n",
"\n",
"```from IPython.display import Image\n",
"Image(\"my_image.png\")```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# to upload your image, replace \"my_image.png\" with the name of your image\n",
"from IPython.display import Image\n",
"Image(\"Algorithm.png\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3 Explanation\n",
" Use the cell below to include a clear explanation of your algorithm. In your explanation, explain how this process is an **algorithm** and make sure to add details about how your flowchart demonstrates the different steps of the algorithm. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The algorithm process each item of the needs list of needs and adds them to the list.\n",
"It then processes each item from the list of wants and adds them to the list. Once, all the items have been added, ensure that if at any point the balance becomes equal to the savings needed, then add the remaining items to the list of **whatNotToBuy**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Implementation\n",
"In this section, you will implement in Python the algorithm that you described above."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.1 Libraries and modules\n",
"Use the cell below to import all the libraries or modules that your Python program requires."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As i've used only the builtins, no additional library imports are required."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.2 User Input\n",
"Use the cell below to write the code that takes the user's input. Your code should allow the user to enter at least the following:\n",
"- The monthly budget\n",
"- The amount of money they want to save\n",
"- The items that they might buy during the month (needs and wants) and:\n",
" - The price of each item\n",
" - A priority value for each item (from 1 to 10, with 10 being higher priority)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Budget stores the monthly budget.\n",
"budget = int(input(\"Enter your monthly budget : $\"))\n",
"\n",
"# It contains the amount of money the user wishes to save.\n",
"savingsNeeded = int(input(\"Enter the amount of money to be saved : $\"))\n",
"\n",
"# It stores the count of total number of items the user believes are essential\n",
"needs = int(\n",
" input(\"Enter the number of items to be added to the list of needs. Example: \\\n",
" \\n Education, \\n Food, \\n Medicine, \\n Transportation etc.\\n ---> \"))\n",
"\n",
"# This will store the total number of items the user believes can be skipped to achieve desired savings.\n",
"wants = int(\n",
" input(\"Enter the number of items to be added to the list of wants. Example: \\\n",
" \\n Trips, \\n Deserts, \\n Parties: \\n---> \"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def addItems(listSize):\n",
" '''\n",
" Adds tuples to the list of items and returns the list.\n",
" This is a commen method used to store items for both needsList and wantsList.\n",
" '''\n",
" listOfItem = []\n",
" for i in range(0, listSize):\n",
" name = input(\"Enter the name of the item : \")\n",
" price = float(input(\"Enter the amount : $\"))\n",
" priority = int(input(\n",
" \"Enter the priority of this item on a scale of 1(lowest) to 10(highest) : \"))\n",
" # append the new item to the list.\n",
" listOfItem.append((name, price, priority))\n",
" return listOfItem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.3 Sorting Functions\n",
"Use the cell below to define all the functions that will take the user's input from the cell above and then process it to make the monthly budget plan for the user. The output should contain information on what the user should buy and what not to buy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The lists will store the respective lists.\n",
"needsList = []\n",
"wantsList = []\n",
"\n",
"# Add items to the needsList\n",
"print(\"Enter the items to be added to the list of needs. \")\n",
"needsList = addItems(needs)\n",
"\n",
"# Add items to the wantsList\n",
"print()\n",
"print(\"Enter the items to be added to the list of wants. \")\n",
"wantsList = addItems(wants)\n",
"\n",
"# sort the list of needs in decreasing order of priority and increasing order of price.\n",
"needsList.sort(key=lambda x: (x[1], -1*x[2]))\n",
"\n",
"# sort the list of wants in decreasing order of priority and increasing order of price.\n",
"wantsList.sort(key=lambda x: (x[1], -1*x[2]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def budgetPlannerAlgorithm(budget, savingsNeeded, needsList, wantsList):\n",
" balance = budget\n",
" needs = len(needsList)\n",
" wants = len(wantsList)\n",
" whatToBuy = []\n",
" whatNotToBuy = []\n",
" i = 0\n",
" # add all the items in the needsList to the whatToBuy and update the remaining balance.\n",
" while(i < needs):\n",
" whatToBuy.append(needsList[i])\n",
" balance -= needsList[i][1]\n",
" i += 1\n",
" \n",
" # If after adding the needs to whatToBuy, the balance is still greater than savingsNeeded,\n",
" # add them to whatToBuy until balance is greater than savingsNeeded.\n",
" # If the balance is already less than savings needed, add all items to whatNotToBuy list.\n",
" if(balance > savingsNeeded):\n",
" i = 0\n",
" # Add items from wants to the list based on condition.\n",
" while(i < wants):\n",
" # only add item if after adding it, the balance will be still greater than or equal to savings needed\n",
" if(balance - wantsList[i][1] >= savingsNeeded):\n",
" whatToBuy.append(wantsList[i])\n",
" balance -= wantsList[i][1]\n",
" else:\n",
" whatNotToBuy.append(wantsList[i])\n",
" i += 1\n",
" else:\n",
" whatNotToBuy += wantsList\n",
" \n",
" # If balance...