• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get the first, last and middle elements of an Array and return the biggest.  RSS feed

 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an Array challenge from CodinBat.  None of the arrays are < 3.  I have tried a lot of variations but I'm not sure if its my "middle" variable or my if statments. I am getting very weird ansers.   Some right, some wrong.

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? 
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you run that code on coding bat?
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 sh‍ould probably be static, but coding bat doesn't allow static methods, so miss out the keyword static.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code runs fine, but the output is wrong (for most of them).  I'll try and find another way to get the middle element.  I tried writing it myself which didn't work, I then googled it and that was the one it gave me.  I'll try find a better one. 

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;
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The challenge says that the length of the array will always be odd. It is important.

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?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I've been playing about with NetBeans and tried this code:



which prints out exactly what I want on NetBeans but only half the answers are right on CodingBat.  So I need to try and fix something else in the code!  I'll re-write it and see where I get.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to call Math.floor.

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.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU!  I just realised the stupid mistake I was making.  I was only asking for the length of the array / 2 and not the element in the array.  I fixed it with this:



Funny how taking a step back has moved me forward!!  Cheers guys!
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:The challenge says that the length of the array will always be odd. It is important. . . .
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.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know CodingBat let you make new methods, so I created just one to handle the last part of the code.  Everything look ok?

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return Math.max(Math.max(i, j), k);
I don't like the name isBigger, which should have boolean return type. Try largestOfThree.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:return Math.max(Math.max(i, j), k);

Cheater.    
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's cheating about that? It follows Bloch's recommendation always to use the ready‑made API methods
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bod MacNeil wrote:. . . I always wondered if there was a built in function to check more than 2 ints.  . . .
Not as far as I know. In Java8 you can create an IntStream and call its max() method. I suggest you try that, and find out whether coding bat uses Java8
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try with an IntStream:-You can read about IntStreams here; the of method is a factory method taking any number of ints (I do not know whether you can pass an int[], but you can use Arrays#stream() with an int[] instead). Both those techniques return an IntStream reference and in this instance you simply need to call its max() method. That doesn't return an int as one might think, but an OptionalInt reference, to prevent problems if max() is called on a 0‑element stream.. There are various ways to get the value out of that optional; I shall try orElse which returns its argument (here I am using the most negative permissible value for an int) if the optional is empty and there is no risk of an Exception.

Coding Bat didn't like the use of a Stream at all; it must be stuck in Java7 mode.
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slightly more general (type inference), and available in java 7, is to use the method Collections.max,
for instance:

Collections.max(Arrays.asList("aap", "noot", "mies", "wim"));
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried PS's suggestion:-Worked nicely on coding bat The Collections#max() method doesn't seem to have a since field. Maybe it was in the oldest versions of that class.

But why are they stuck on Java7?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If not want to use inbuilt function then I think this one will also work. According to question It just wants largest so numbers equals to not considered.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:. . . numbers equals to not considered. . . .]
If two numbers are equal, you can use either as the larger. I think that is implicit in all max functions.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell Oh I see, this how they decide ( the result is the argument closer to the value of Integer.MAX_VALUE ).
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can generalise that to any size of array, but this method sorts the array every time it is called:-
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!