How to show results in text, not digits - max

I'm new to programming so forgive me if my question is vague.
I'm creating program that tells you which one of a triangle, circle or square has the largest area. I want the program to say "The triangle has the largest area", and not showing me the area in digits.
I know how to show which of the numbers was largest but how can I make the program say "the triangle" or "the square" has the largest area?

Related

Move points to nearest unoccupied grid position (different)

I have a set of X "data points" with x and y coordinates and I want to assign them to a MxN grid, such that a "grid point" is occupied once. To simplify the question I here state that the number of "data points" and the number of "grid points" is identical.
For me the acceptable criterion is when the sum of difference between all "data point" and the selected "grid point" to the square is minimized.
For sure I can do this using a brute force method but there is a factorial of X number of possibilities, i.e. app. about 5*10^8 possibilities when you have 12 "data points".
Is there an elegant algorithm to do it with minimum computational effort less than O(n!)?
Just to visualize the problem I show an example with 6 "grid points" (A to F) in blue and "data points" (1 to 6) in red.
It is just interesting to see, that "3" is nearest point to "B", but then "1" is very far from the next point. Intuitively the human eye assigns trivially 2->A 4->C 6->E 5->F but it assigns non-trivial 1->B and 3->D. And this is what I want to reach programmatically.
There is already a question with the same name, but there was no discussion about algorithm,
here.
Your question is a variant of the balanced assignment problem, or equivalently, the problem of finding a minimal matching in a balanced, weighted bipartite graph. Specifically, your problem is actually a bit more restricted, since the edge weights correspond to spacial distance, and certain properties (e.g. the triangle inequality theorem) hold.
Have a look at the Hungarian Algorithm for a polynomial-time solution.

Practicing interview questions and want to know how to approach this one aka algorithm

Saw this as an interview question would want to know the correct way to break this down that way I can understand the thought process and code it myself for practice. Also any advice on how to break down a problem.
"There is a n * n square board, there’s a new kind of game which is played on this board. there are pieces on the board which can only move horizontal and vertical any number of squares until it encounters an opponent piece, when it does so, it replaces the opponent piece at that position and the turn alternates. The input will contain a n * n matrix with 1,2 and 0 in the cells. ‘1’ denotes your pieces, ‘2’ denotes opponent pieces, and ‘0’ denotes free space. One of your pieces has fallen from the board and neither you nor your opponent remember its original position, so you collectively decide that you can place it anywhere you want to, so its upto you to place it at a position where you can maximize the number of opponent pieces you can cut. Output the x and y coordinates of such position. If multiple positions exists, output anyone."

Using list of scattered points in 2D tilemap, how would I find the center of those points?

Have a look at this image for example (just a random image of scattered points from image search):
(reference)
You'll see the locations with blue points. Let's say the blue represents what I'm looking for. But I want to find the coordinates where there is the most blue. Meaning the most dense or center of most points (in the picture, it would approximate [.5, .5]).
If I have an arrayList of each and every blue point (x,y coordinates), then how do I use those points to find the center/most dense area of those points?
There are several options, dependent on what precisely you need. The simplest would be the mean, the average of all points: You sum all points up and divide by their number. Getting the most dense area is complicated, because at first you have to come up with a definition of "dense". One option would be: For each point P, find the 7 nearest neighbors N_P1...N_P7. The point P where the 7th neighbor has the smallest distance |P-N_P7| is the point with the highest density around it and you pick P as center. You can replace that 7 with any number that works for you. You could even replace it with some parameter from your data set, say 1/3 of the total number of points.

Maximization algorithm for ball preferences

I'm trying to devise the most efficient algorithm for a problem, but I'm having some difficulty. If someone could lend a hand, either by proposing an algorithm or classifying the problem so I can do some further research, I would be very appreciative.
The problem is as follows:
There are n (an integer) number of distinct red balls, each of which has its own number, and m number of distinct green balls, each of which has its own corresponding number as well. For example, if n = 3, then there are three red balls named Red Ball 1, Red Ball 2 and Red Ball 3.
There are also two boxes in which the balls can be placed.
Before the balls are placed in the boxes however, x number of people make predictions as to which balls will be placed in which box (either box 1 or box 2). Each person gets one prediction and for each prediction they can guess one ball to be in each box. The only condition is that the ball they guess in box 1 cannot be the same color as the ball they guess to be in box 2. An example prediction would be: "I think that Red Ball 2 will be in box 1 and Green Ball 3 will be in box 2"
After everyone has made their predictions, the balls will be placed in the boxes the maximize the number of predictions that are correct.
The code I must write will be prompted with n, m, and x as well as the predictions and then be asked to return the maximum number of predictions that are correct.
Once again, I am looking for either algorithmic help or help to identify the type of problem this is. I currently have a recursive algorithm running on (n^2), but I need something a little more efficient.
Thanks for your help! Cheers, Mates!

Difference between 'Largest Rectangle in Histogram' and 'Container With Most Water'

http://www.leetcode.com/onlinejudge
I am not able to see the difference between these two questions.
To me, these two questions are same, but they are NOT.
Can someone give me some hints that explain why they are different.
Thank you
The "container of water" solution will allow the water to rise above intermediate positions. With the "largest rectangle" problem, the rectangle cannot rise above intermediate bars.
The "container of water" question is not as clearly described as the largest rectangle one, however I've been asked the water one in an interview.
The container of water one is basically asking for the area of the biggest "valley" in between the bars on a histogram. Looking at the histogram in the largest rectangle example, the answer would be "1" because there are two troughs made by the graph, a 1x1 trough on the left side, and a 1x1 trough on the right side. The max of these is of course, 1.
they are very similar. I understand the difference is that a "container of water" would be a left wall, right wall and the bottom (x-axis) (and some lines in between may be short), it is open on the top. while in the "largest rectangle" all the histograms bars that constitute the rectangle would have to reach the top area.

Resources