Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sameFirstLast

Posted on 2015-02-12
8
Medium Priority
?
64 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 28

Assisted Solution

by:dpearson
dpearson earned 668 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 664 total points
ID: 40606542
first == last is true when the operands are equal, and false when they are not equal
0
 
LVL 28

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 668 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

670 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