Solved

sameFirstLast

Posted on 2015-02-12
8
56 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 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
How our DevOps Teams Maximize Uptime

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

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 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