Solved

sameFirstLast

Posted on 2015-02-12
8
61 Views
Last Modified: 2015-02-18
hi,
I am trying below challenge
http://codingbat.com/prob/p118976

i am failing in one test case
public boolean sameFirstLast(int[] nums) {
int numsLen=nums.length;
int first=nums[0];
int last=nums[numsLen-1];
if(numsLen>=1&&first==last)
return true;
return false;
  
}

Open in new window


Expected	Run		
sameFirstLast({1, 2, 3}) → false	false	OK	    
sameFirstLast({1, 2, 3, 1}) → true	true	OK	    
sameFirstLast({1, 2, 1}) → true	true	OK	    
sameFirstLast({7}) → true	true	OK	    
sameFirstLast({}) → false	Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:3)	X	    
sameFirstLast({1, 2, 3, 4, 5, 1}) → true	true	OK	    
sameFirstLast({1, 2, 3, 4, 5, 13}) → false	false	OK	    
sameFirstLast({13, 2, 3, 4, 5, 13}) → true	true	OK	    
sameFirstLast({7, 7}) → true	true	OK	    
other tests

Open in new window


please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40606504
public boolean sameFirstLast(int[] nums) {

int numsLen=nums.length;

if(numsLen==0){

return false;
}
int first=nums[0];
int last=nums[numsLen-1];
if(numsLen>=1&&first==last)
return true;
return false;
  
}

Open in new window

i think i oredere edge case and passed all tests. I wonder how to improve my code. please advise
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 167 total points
ID: 40606507
I would say your code actually looks fine.

The only part that could be written more clearly is to replace these 3 lines:

...
if(numsLen>=1&&first==last)
return true;
return false;
...

With this one line:

return (first == last) ;

There's no need to check that "numsLen >= 1" because you already tested for length 0 at the top of the method - so the length here must be >= 1.

And rather than:
   if (first == last)
       return true ;
  return false ;

you can just return whether "first == last"
   return (first == last) ;

Make sense?

Whole program becomes:

public boolean sameFirstLast(int[] nums) {

int numsLen=nums.length;

if(numsLen==0){

return false;
}
int first=nums[0];
int last=nums[numsLen-1];
return (first == last) ;
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40606510
numsLen>=1&& is unneccessary
and at that point, you can just
return first==last;

or, using numsLen>=1&&  you can make the if(numsLen==0){ unnecessary
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 7

Author Comment

by:gudii9
ID: 40606523
And rather than:
   if (first == last)
       return true ;
  return false ;

you can just return whether "first == last"
   return (first == last) ;

Open in new window


instead of doing in 2 steps like checking and then returning we are doing in one step in return itself comparing so that i will automatically give true false in return statement right?
please advise
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 40606542
first == last is true when the operands are equal, and false when they are not equal
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40606710
instead of doing in 2 steps like checking and then returning we are doing in one step in return itself comparing so that i will automatically give true false in return statement right?

Yes that's right.  We're replacing 3 lines of code with 1 - which is good because it's simpler.  And we're also removing a conditional (the "if" test) so the code always executes the same lines of code - which is also good.

Rather than:
   if (first == last) {
      // Sometimes we run code in here
      return true ;
   }
   // Sometimes we run code here
   return false ;

We have now:
   // Always run this code
   return (first == last) ;

It's best to avoid branches and conditional statements in your code where possible.  It makes bugs less likely.

Doug
0
 
LVL 37

Accepted Solution

by:
zzynx earned 167 total points
ID: 40614484
If you write:
 if (first == last) {
       return true ;
 }
 return false ;

Open in new window


When runing this piece of code, your computer will evaluate the expression first == last.
The value of an expression is a boolean. That's a variable that can hold the value 'true' or 'false'.
When the result/value of that expression is true, it will return true.
When the result/value of that expression is false, it will return false.
So, in fact, it can just return the result/value of that expression.
You write that as:
return first == last;

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 40616024
Thanx 4 axxepting
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question