Skip to content

Instantly share code, notes, and snippets.

@Simonblancodas
Created May 26, 2020 16:46
Show Gist options
  • Save Simonblancodas/e6903e2d4dbdf936d25c3f410d995025 to your computer and use it in GitHub Desktop.
Save Simonblancodas/e6903e2d4dbdf936d25c3f410d995025 to your computer and use it in GitHub Desktop.
A set of three programming training problems for beginners
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": "<h1>Problem Set 1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "This problem set will introduce you to using control flow in Python and formulating a computational solution to a problem. It will also give you a chance to explore bisection search. This problem set has three problems."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2>Part A: House Hunting"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "You have graduated from MIT and now have a great job! You move to the San Francisco Bay Area and decide that you want to start saving to buy a house. As housing prices are very high in the Bay Area, you realize you are going to have to save for several years before you can afford to make the down payment on a house. In Part A, we are going to determine how long it will take you to save enough money to make the down payment given the assumptions\n\nAll the assumptions are translated into the variables defined below"
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": "#The Inputs\n#Cost of your dream home\ntotal_cost = 500000\n#Annual Salary\nannual_salary=120000\n#Annual return rate of invested savings\nr=.04\n#Percentage of monthly salary saved\nportion_saved=0.05\n#portion of the cost needed for a down payment\nportion_down_payment=.25\n\n#addinional variables\n#Savings I start with\ncurrent_savings=0\n#Monthly salary\nmonth_salary=annual_salary/12\n\n#Converts variables to floats\ntotal_costf=float(total_cost)\ncurrent_savingsf = float(current_savings)\nportion_down_paymentf=float(portion_down_payment)\nportion_down_paymentf=float(portion_down_payment)\nrf=float(r)\nportion_savedf=float(portion_saved)\nannual_salaryf=float(annual_salary)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Write a program to calculate how many months it will take you to save up enough money for a down payment. You will want your main variables to be floats, so you should cast user inputs to floats. 1\nYour program should ask the user to enter the following variables:\n1. The starting annual salary (annual_salary)\n2. The portion of salary to be saved (portion_saved)\n3. The cost of your dream home (total_cost)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Hints"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "To help you get started, here is a rough outline of the stages you should probably follow in writing your\ncode:\n\n\u25cf Retrieve user input. Look at input() if you need help with getting user input. For this problem set,\nyou can assume that users will enter valid input (e.g. they won\u2019t enter a string when you expect\nan int)\n\n\u25cf Initialize some state variables. You should decide what information you need. Be careful about\nvalues that represent annual amounts and those that represent monthly amounts."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Try different inputs and see how long it takes to save for a down payment. Please make your\nprogram print results in the format shown in the test cases below."
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Number of months to save: 183\nYears to save: 15\nCurrent savings are: 125784.80816751943\n"
}
],
"source": "#This code calculates the number of months needed to save enough for the down Payment\nnumber_of_months=0\ndown_payment=total_costf*portion_down_payment\nwhile current_savingsf <= down_payment:\n month_savings=(current_savingsf*rf/12)+month_salary*portion_savedf\n current_savings = current_savingsf+month_savings\n number_of_months = number_of_months+1\n current_savingsf = float(current_savings)\nprint('Number of months to save: ',number_of_months)\nprint('Years to save: ',round(number_of_months/12))\nprint('Current savings are: ', current_savingsf)\n#current savings is reset to 0 so we can run the program is reset at the end of every run\ncurrent_savingsf=0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Test Case 1\n\n1. Enter your annual salary: 120000\n2. Enter the percent of your salary to save, as a decimal: .10\n3. Enter the cost of your dream home: 1000000\n4. Number of months: 183\n\nTest Case 2\n\n1. Enter your annual salary: 80000\n2. Enter the percent of your salary to save, as a decimal: .15\n3. Enter the cost of your dream home: 500000\n4. Number of months: 105"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2>Part B: Saving, with a raise"
},
{
"cell_type": "markdown",
"metadata": {},
"source": " In Part A, we unrealistically assumed that your salary didn\u2019t change. But you are an MIT graduate, and\nclearly you are going to be worth more to your company over time! So we are going to build on your\nsolution to Part A by factoring in a raise every six months."
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": "#New inputs:\n#The percentage of raise that will be received every ''m'' months\nsemi_annual_raise = 0.05\n#Number of months between raises\nm=6\n\n#The Inputs\n#Cost of your dream home\ntotal_cost = 1500000\n#Annual Salary\nannual_salary=75000\n#Annual return rate of invested savings\nr=.04\n#Percentage of monthly salary saved\nportion_saved=0.05\n#Portion of the cost needed for a down payment\nportion_down_payment=.25\n\n#Addinional variables\n#Savings I start with\ncurrent_savings=0\n#Monthly salary\nmonth_salary=annual_salaryf/12\n#Initial month indicator\nnumber_of_months=0\n\n#Converts variables to floats\ntotal_costf=float(total_cost)\ncurrent_savingsf = float(current_savings)\nportion_down_paymentf=float(portion_down_payment)\nportion_down_paymentf=float(portion_down_payment)\nrf=float(r)\nportion_savedf=float(portion_saved)\nannual_salaryf=float(annual_salary)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Write a program to calculate how many months it will take you save up enough money for a down\npayment. LIke before, assume that your investments earn a return of r = 0.04 (or 4%) and the\nrequired down payment percentage is 0.25 (or 25%). Have the user enter the following variables:\n1. The starting annual salary (annual_salary)\n2. The percentage of salary to be saved (portion_saved) \n3. The cost of your dream home (total_cost) \n4. The semiannual salary raise (semi_annual_raise)"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Number of months to save: 261\nYears to save: 22\nCurrent savings are: 378291.88049863366\n"
}
],
"source": "#This code calculates the number of months needed to save enough for the down Payment now that a salary raise is received every certain amount of months\n\ncurrent_salary = month_salary\ndown_payment=total_costf*portion_down_payment\n\nwhile current_savingsf <= down_payment:\n annual=current_salary*12\n month_savings=(current_savingsf*rf/12)+current_salary*portion_savedf\n current_savings = current_savingsf+month_savings\n number_of_months = number_of_months+1\n current_savingsf = float(current_savings)\n if (number_of_months%m)==0 and (number_of_months%3)==0:\n current_salary = current_salary+current_salary*semi_annual_raise\nprint('Number of months to save: ',number_of_months)\nprint('Years to save: ',round(number_of_months/12))\nprint('Current savings are: ', current_savingsf)\n#current savings is reset to 0 so we can run the program is reset at the end of every run\ncurrent_savingsf=0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Test Case 1\n1. Enter your starting annual salary: 120000 \n2. Enter the percent of your salary to save, as a decimal: . 05 \n3. Enter the cost of your dream home: 500000 \n4. Enter the semiannual raise, as a decimal: .03 \n- Output: Number of months: 142\n\nTest Case 2 \n1. Enter your starting annual salary: 80000 \n2. Enter the percent of your salary to save, as a decimal: . 1 \n3. Enter the cost of your dream home: 800000 \n4. Enter the semiannual raise, as a decimal: .03 \n- Output: Number of months: 159 \n\nTest Case 3\n1. Enter your starting annual salary: 75000 \n2. Enter the percent of your salary to save, as a decimal: . 05 \n3. Enter the cost of your dream home: 1500000 \n4. Enter the semiannual raise, as a decimal: .05 \n- Output: Number of months: 261"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2>Part C: Finding the right amount to save away"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "In Part B, you had a chance to explore how both the percentage of your salary that you save each month and your annual raise affect how long it takes you to save for a down payment. This is nice, but suppose you want to set a particular goal, e.g. to be able to afford the down payment in three years. How much should you save each month to achieve this? In this problem, you are going to write a program to answer that question. To simplify things, assume: 3\n\n1. Your semiannual raise is .07 (7%) \n2. Your investments have an annual return of 0.04 (4%) \n3. The down payment is 0.25 (25%) of the cost of the house \n4. The cost of the house that you are saving for is $1M.\n\nYou are now going to try to find the best rate of savings to achieve a down payment on a USD 1M house in 36 months. Since hitting this exactly is a challenge, we simply want your savings to be within USD 100 of the required down payment."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "write a program to calculate the best savings rate, as a function of your starting salary. You should use bisection search to help you do this efficiently. You should keep track of the number of steps it takes your bisections search to finish. You should be able to reuse some of the code you wrote for part B in this problem. \n\nBecause we are searching for a value that is in principle a float, we are going to limit ourselves to two decimals of accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal \u2013 but we are not going to worry about the difference between 7.041% and 7.039%). This means we can search for an integer between 0 and 10000 (using integer division), and then convert it to a decimal percentage (using float division) to use when we are calculating the current_savings after 36 months. By using this range, there are only a finite number of numbers that we are searching over, as opposed to the infinite number of decimals between 0 and 1. This range will help prevent infinite loops. The reason we use 0 to 10000 is to account for two additional decimal places in the range 0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>The code below finds the salary savings rate needed with an statick rate of return over investment of the savings"
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "need to save a portion higher than 99.99%\nNumber of months to save: 36\nCurrent savings are: 37777.403828738585\nCurrent salary is: 1250.6086265408333\nReturn rate is: 0.04\nPortion saved is: 0.9999\nSteps in bisection: 14\n"
}
],
"source": "#Inputs\ntotal_cost = 1000000\n#variables for salary\nannual_salary=10000\nmonth_salary=annual_salary/12\nportion_saved=0.05\na = 0\nb = 10000\nportion_range = range(a,b,1)\nsemi_annual_raise = 0.07\n\ntarget_months=36\n#variables for return rate\nr=.04\n\ncurrent_savings=0\n\n#Number of months between raises\nm=6\nportion_down_payment=.25\ndown_payment = total_cost*portion_down_payment\ncounter = 0\n\nwhile current_savings < (down_payment-100) or current_savings > (down_payment+100):\n loop_months = 0\n current_savings=0\n current_salary=month_salary\n for i in range(target_months):\n current_savings = current_savings+current_savings*(r/12)+current_salary*portion_saved\n loop_months=loop_months + 1\n if (loop_months%m)==0:\n current_salary = current_salary+current_salary*semi_annual_raise\n \n portion_savedi=int(portion_saved*10000)\n if current_savings < (down_payment-100):\n a = portion_savedi\n portion_range = range(a,b,1)\n portion_savedi = portion_savedi+(len(portion_range)//2)\n \n if current_savings > (down_payment+100):\n b = portion_savedi\n portion_range = range(a,b,1)\n portion_savedi = portion_savedi-(len(portion_range)//2)\n \n if portion_savedi >= 9999:\n portion_saved = float(portion_savedi/10000)\n counter = counter+1\n print(\"need to save a portion higher than 99.99%\")\n break\n #resets r to float \n portion_saved = float(portion_savedi/10000)\n #loop conter\n counter = counter+1\nprint('Number of months to save: ',loop_months) \nprint('Current savings are: ',current_savings)\nprint('Current salary is: ',current_salary)\nprint('Return rate is: ',r)\nprint('Portion saved is: ',portion_saved)\nprint('Steps in bisection: ', counter)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>The code below finds the return rate needed with an statick savings rate of the salary"
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "need a return rate higher than 99.99%\nNumber of months to save: 36\nCurrent savings are: 138203.897873971\nCurrent salary is: 18759.129398112498\nReturn rate is: 0.9999\n"
}
],
"source": "\ntotal_cost = 1000000\n#variables for salary\nannual_salary=150000\nmonth_salary=annual_salary/12\nportion_saved=0.05\nsemi_annual_raise = 0.07\n\ntarget_months=36\n#variables for return rate\nr=.12\na = 0\nb = 10000\nr_range = range(a,b,1)\n\ncurrent_savings=0\n\n#Number of months between raises\nm=6\nportion_down_payment=.25\ndown_payment = total_cost*portion_down_payment\ncounter = 0\n\nwhile current_savings < (down_payment-100) or current_savings > (down_payment+100):\n loop_months = 0\n current_savings=0\n current_salary=month_salary\n for i in range(target_months):\n current_savings = current_savings+current_savings*(r/12)+current_salary*portion_saved\n loop_months=loop_months + 1\n if (loop_months%m)==0:\n current_salary = current_salary+current_salary*semi_annual_raise\n ri=int(r*10000)\n if current_savings < (down_payment-100):\n a = ri\n r_range = range(a,b,1)\n ri = ri+(len(r_range)//2)\n \n if current_savings > (down_payment+100):\n b = ri\n r_range = range(a,b,1)\n ri = ri-(len(r_range)//2)\n \n if ri >= 9999:\n r = float(ri/10000)\n counter = counter+1\n print(\"need a return rate higher than 99.99%\")\n break\n #resets r to float \n r = float(ri/10000)\n #loop conter\n counter = counter+1\nprint('Number of months to save: ',loop_months) \nprint('Current savings are: ',current_savings)\nprint('Current salary is: ',current_salary)\nprint('Return rate is: ',r)\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Test Case 1 \n1. Enter the starting salary: 150000 \n2. Best savings rate: 0.4411 \n3. Steps in bisection search: 12\n\nTest Case 2 \n1. Enter the starting salary: 300000 \n2. Best savings rate: 0.2206 \n3. Steps in bisection search: 9 \n\nTest Case 3 \n1. Enter the starting salary: 10000 \n2. It is not possible to pay the down payment in three years."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<hr>\n<h2>The last exercise!</h2>\n<p>Congratulations, you have completed your first Problem set in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work......\n \n I think it's funny that they always put it after the last exercise"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.6",
"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.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment