Solved

sameFirstLast

Posted on 2015-02-12
8
49 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

706 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

17 Experts available now in Live!

Get 1:1 Help Now