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.
Bod MacNeil wrote: None of the arrays are < 3.
Assuming you're referring to length, that's not what the wording of the "challenge" you posted says.
Winston
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
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 1length array is the only element (index 0).
The middle element of 3length array is element with index 1.
The middle element of 5length array is element with index 2.
The middle element of 7length array is element with index 3.
Do you see a pattern?
A quiz question. What is the result of this expression in Java?
3/2
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/goodnamingisaprocessnotasinglestep/ 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:
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
Campbell Ritchie wrote:return Math.max(Math.max(i, j), k);
Cheater.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
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.
Junilu Lacar wrote:You could also write that as:...
or indeed:However, yet another alternative is to sort the three values  which requires the same number of checks and at most 2 "swaps"  but then makes the three "value results" (greatest, smallest, middle) trivial.
Winston
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Coding Bat didn't like the use of a Stream at all; it must be stuck in Java7 mode.
for instance:
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...
Winston
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:You can generalise that to any size of array...
Very true. I hadn't thought of that. I've actually been working on a generic 'Range' Iterable as an exercise  kinda fun.
Winston
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
The knights of nee want a shrubbery. And a tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
