here is the challenge:
Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.
maxTriple([1, 2, 3]) → 3
maxTriple([1, 5, 3]) → 5
maxTriple([5, 2, 3]) → 5
and my code, so far:
I've no idea where I'm going wrong. Maybe my if statements are a bit messy?
Where did you get your formula for middle from? That looks wrong.
It would also be wrong to use only one method; that code is begging to be subdivided in to multiple methods. They should probably be static, but coding bat doesn't allow static methods, so miss out the keyword static.
Yeah the challenge says they could be of length 1, but after running it I noticed 3 was the smallest. Kind of cheating but if I can get this to work first I'll add a conditional statement to deal with arrays < 3 in length. cheers;
So the middle element of 1-length array is the only element (index 0).
The middle element of 3-length array is element with index 1.
The middle element of 5-length array is element with index 2.
The middle element of 7-length array is element with index 3.
Do you see a pattern?
A quiz question. What is the result of this expression in Java?
You need to split your task into two parts.
1. Determine values of first, middle and last element in the array correctly.
2. If and only if you have completed step 1. work on selecting which of those values is the biggest.
It is only important because you want to be unambiguous about which the middle element is. You can find a middle element in an even‑numbered array, but there are two potential middle elements. If you use the / 2 method, you simply have to take the left middle element.
Paweł Baczyński wrote:The challenge says that the length of the array will always be odd. It is important. . . .
A couple of things:
1. The method name doesn't fit what the method does. Methods that start with "is" are usually boolean whereas this method returns an int. A good name should reflect its intent clearly and unambiguously. See this article: http://arlobelshee.com/good-naming-is-a-process-not-a-single-step/ for some great ideas about choosing good names.
2. Alternative implementation:
This code works exactly as yours except the case of numbers being equal is more explicitly defined in the code whereas your version it's implied. Not that it matters much in this case but in both versions, when there are two or more numbers that are the same, the latter number will be favored. So if first and middle are equal and both are greater than last, the middle number will be returned. If last is the greatest and is equal to first and/or middle, last will be returned.
You could also write that as:
Campbell Ritchie wrote:return Math.max(Math.max(i, j), k);
I don't like the name isBigger, which should have boolean return type. Try largestOfThree.
I always wondered if there was a built in function to check more than 2 ints. Never thought of just using the Math.max inside a Math.max!
The first thing people pck up on in my code is the names of everything. I'm going to take some time to read the article Junilu posted! cheers.
Coding Bat didn't like the use of a Stream at all; it must be stuck in Java7 mode.
Collections.max(Arrays.asList("aap", "noot", "mies", "wim"));
But why are they stuck on Java7?
If two numbers are equal, you can use either as the larger. I think that is implicit in all max functions.
Ganesh Patekar wrote:. . . numbers equals to not considered. . . .]
Campbell Ritchie wrote:If two numbers are equal, you can use either as the larger...
I still say it's a lot of extra work for specifically three values when a sort requires the same number of compares, and at most two swaps; and provides you with the answer for all three "value" methods in one fell swoop.
But I guess I'm just an old fart...