Solved

sameFirstLast

Posted on 2015-02-12
8
53 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
  • 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 26

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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now