python Homework 2 14 September 2018 This assignment provides more practice in ba
ID: 3755666 • Letter: P
Question
python
Homework 2
14 September 2018
This assignment provides more practice in basic calculations, and also introduces the use of a data structure to hold information used in solving the problem. The data structure will be initialized within the program source code, and consulted after the input arrives.
Since the problem is a little more complicated than the previous homework, every student should still follow the recommended schedule described in the previous assignment for getting particular phases accomplished ahead of time.
Overall Problem
You are playing the game of Monopoly and you decide you wish to construct houses on one of your property groups. The rules of the game require that the number of houses on the properties within each group may not differ by more than one.
You will be given an amount of money to spend, the cost per house, and the number of properties in the group. The goal is to determine how many houses will go on each.
To make the program simple, you may assume that you will not have enough money to build past four houses, so there needs to be no special cases for hotels.
Monopoly Property Groups
Here is a small table relating the colors of the monopoly property groups, the number of properties within the group, and the cost of the houses.
Sample Interfaces
Exact spacing and spelling is not required -- correct calculation is far more important.
Homework 3
28 September 2018
This is a continuation of the previous assignment, and introduces use of conditional decisions to solve some parts of the problem. It is hoped that the previous assignment was designed flexibly enough to allow for easy modification.
Problem Description
The previous assignment had a simplifying assumption that the amount of money would be limited, such that the builder could not afford more than four houses on any property. This assignment will lift that restriction.
There is nothing really special about building hotels in Monopoly. That purchase is simply equal to the price of five houses, and is built only in the same circumstances that would permit five houses (no other property may have fewer than four).
The inputs to the program will be essentially the same -- the color of a property group and the amount of money to be spent. But the following cases should be addressed:
if no building is affordable, display "You cannot afford even one house." instead of building 0 houses everywhere.
if one can afford to build 5 houses somewhere, announce that a hotel is being built instead of 5 houses.
if one can afford to build more than 5 houses somewhere, do not do so, but stop at the hotel
omit 0's in the output (i..e don't say 1 property has 0 houses, or 0 have 3)
Comparing Homework 2 to Homework 3
Extra Credit Option
It is quite possible that the user chooses a color that is not accepted. Write code that would allow the user to try again, until a suitable input is found.
It might be good to treat "blue" as a special case, since it is not so much wrong as it is simply ambiguous.
For best results, write a solution that does not involve exception handling (no ValueError or KeyError)
Note: This option would require some language elements from Chapters 3 and 4.
color size cost purple 2 50 light blue 3 50 maroon 3 100 orange 3 100 red 3 150 yellow 3 150 green 3 200 dark blue 2 200Explanation / Answer
MCP(Minimum Cost Path) problem */
public class GFG {
/* A utility function that returns
minimum of 3 integers */
static int min(int x, int y, int z)
{
if (x < y)
return (x < z) ? x : z;
else
return (y < z) ? y : z;
}
/* Returns cost of minimum cost path
from (0,0) to (m, n) in mat[R][C]*/
static int minCost(int cost[][], int m,
int n)
{
if (n < 0 || m < 0)
return Integer.MAX_VALUE;
else if (m == 0 && n == 0)
return cost[m][n];
else
return cost[m][n] +
min( minCost(cost, m-1, n-1),
minCost(cost, m-1, n),
minCost(cost, m, n-1) );
}
// Driver code
public static void main(String args[])
{
int cost[][] = { {1, 2, 3},
{4, 8, 2},
{1, 5, 3} };
System.out.print(minCost(cost, 2, 2));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.