Solved

twoCahr challenge

Posted on 2014-12-04
20
104 Views
Last Modified: 2014-12-18
Hi,

I am trying below challenge
http://codingbat.com/prob/p144623
i wrote my code as below and failing one test case
public String twoChar(String str, int index) {
if(index<=str.length()/2&&index>=0)
  return str.substring(index,index+2);
  else return str.substring(0,2);
}

Open in new window


Expected	Run		
twoChar("java", 0) → "ja"	"ja"	OK	    
twoChar("java", 2) → "va"	"va"	OK	    
twoChar("java", 3) → "ja"	"ja"	OK	    
twoChar("java", 4) → "ja"	"ja"	OK	    
twoChar("java", -1) → "ja"	"ja"	OK	    
twoChar("Hello", 0) → "He"	"He"	OK	    
twoChar("Hello", 1) → "el"	"el"	OK	    
twoChar("Hello", 99) → "He"	"He"	OK	    
twoChar("Hello", 3) → "lo"	"He"	X	    
twoChar("Hello", 4) → "He"	"He"	OK	    
twoChar("Hello", 5) → "He"	"He"	OK	    
twoChar("Hello", -7) → "He"	"He"	OK	    
twoChar("Hello", 6) → "He"	"He"	OK	    
twoChar("Hello", -1) → "He"	"He"	OK	    
twoChar("yay", 0) → "ya"	"ya"	OK	    
other tests
X	    

Open in new window

how to improve and fix my code. Please advise
0
Comment
Question by:gudii9
  • 11
  • 6
  • 3
20 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40481177
When index==3 and str.length()==5, you are inappropriately returning str.substring(0,2) instead of the desired  str.substring(index,index+2)

I'd advise changing the test in your if condition
0
 
LVL 7

Author Comment

by:gudii9
ID: 40481360
but the lenght and index are dynamic right? I cannot hard code to 5 or 3 either. please advise
0
 
LVL 4

Accepted Solution

by:
Zsolt Pribusz earned 250 total points
ID: 40481969
You need to change line 2
from:
if(index<=str.length()/2&&index>=0)

to:

if(index < str.length()-1 && index>=0)
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 84

Expert Comment

by:ozo
ID: 40482212
If there is only one set of lenght and index values for which your condition makes the wrong choice, you could hard code that one exception, but a better solution would be to modify your condition.

What would make more sense than index<=str.length()/2 when you want to determine if the index is too big or too small to define a string length 2?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40492132
public String twoChar(String str, int index) {
if(index<=str.length()-1&&index>=0)
  return str.substring(index,index+2);
  else return str.substring(0,2);
}

Open in new window


i failed more test cases

xpected	Run		
twoChar("java", 0) → "ja"	"ja"	OK	    
twoChar("java", 2) → "va"	"va"	OK	    
twoChar("java", 3) → "ja"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 5 (line number:3)"	X	    
twoChar("java", 4) → "ja"	"ja"	OK	    
twoChar("java", -1) → "ja"	"ja"	OK	    
twoChar("Hello", 0) → "He"	"He"	OK	    
twoChar("Hello", 1) → "el"	"el"	OK	    
twoChar("Hello", 99) → "He"	"He"	OK	    
twoChar("Hello", 3) → "lo"	"lo"	OK	    
twoChar("Hello", 4) → "He"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 6 (line number:3)"	X	    
twoChar("Hello", 5) → "He"	"He"	OK	    
twoChar("Hello", -7) → "He"	"He"	OK	    
twoChar("Hello", 6) → "He"	"He"	OK	    
twoChar("Hello", -1) → "He"	"He"	OK	    
twoChar("yay", 0) → "ya"	"ya"	OK	    
other tests

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40492171
public String twoChar(String str, int index) {
if(index<=str.length()-2&&index>=0)
  return str.substring(index,index+2);
  else return str.substring(0,2);
}

Open in new window


above passed all tests
0
 
LVL 7

Author Comment

by:gudii9
ID: 40492177

from:
if(index<=str.length()/2&&index>=0)

to:

if(index < str.length()-2 && index>=0

i wonder why above fix worked where as my original code did not work
0
 
LVL 7

Author Comment

by:gudii9
ID: 40492181
public class Test36 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str=twoChar("aa",0);
		System.out.println("str is --->"+str);
	}

	public static String twoChar(String str, int index) {
		if(index<=str.length()-2&&index>=0)
		  return str.substring(index,index+2);
		  else return str.substring(0,2);
		}
}

Open in new window

wrote as java program
0
 
LVL 84

Expert Comment

by:ozo
ID: 40492846
Which of
index<=str.length()/2
or
index<=str.length()-2
better expresses the idea that there are two characters after index in str?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40494732
I think it is
index<=str.length()-2


public String twoChar(String str, int index) {
if(index<=str.length()-2&&index>=0)
  return str.substring(index,index+2);
  else return str.substring(0,2);
}

Open in new window

above also passed all testes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40502754
Does my code looks fine. Any improvements i can make?
please advise
0
 
LVL 4

Expert Comment

by:Zsolt Pribusz
ID: 40503243
This code is, ok.
Only one thing coming to my mind. In "IF" you could exchange the evaluations.
If the given index value is negative, then second evaluation will be not calculated unnecessarily. This is only small optimalization.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40503304
"IF" you could exchange the evaluations.

How to do it. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40503355
What part of "exchange the evaluations" is unclear?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40507266
I understood exchange the evaluations but not clear on how to do it in my code.
public String twoChar(String str, int index) {
if(index<=str.length()-2&&index>=0)
  return str.substring(index,index+2);
  else return str.substring(0,2);
}

Open in new window


please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40507472
The evaluations which Zsolt Pribusz suggest exchanging are
index<=str.length()-2
with
index>=0
Do you the reason for the suggestion and why it is only small optimalization?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40507552
public String twoChar(String str, int index) {
if(index>=0&&index<=str.length()-2)
  return str.substring(index,index+2);
  else return str.substring(0,2);
}

Open in new window


I see the point. I need to exchange as above right. Only when index>=0 then checking of index<=str.length()-2 happens?if index negative wont even check second condition after &&. Is my understanding correct.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40507569
I believe that is what Zsolt Pribusz was suggesting.
0
 
LVL 4

Expert Comment

by:Zsolt Pribusz
ID: 40507909
Yes, that was my suggestion.
It's a small thing, but it increases the efficiency of the code.
Paying attention to such things help writing effective program, even if it is complex.
The same is true if a problem are analyzed from multiple angles, and different approaches are used.
If there are several solutions to a problem, you can choose a solution based on efficiency, flexibility, portability as well.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40507990
got it. thank you
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

828 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