Solved

twoCahr challenge

Posted on 2014-12-04
20
98 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
Comment Utility
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
Comment Utility
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
Comment Utility
You need to change line 2
from:
if(index<=str.length()/2&&index>=0)

to:

if(index < str.length()-1 && index>=0)
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Author Comment

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

Expert Comment

by:Zsolt Pribusz
Comment Utility
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
Comment Utility
"IF" you could exchange the evaluations.

How to do it. please advise
0
 
LVL 84

Expert Comment

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

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I believe that is what Zsolt Pribusz was suggesting.
0
 
LVL 4

Expert Comment

by:Zsolt Pribusz
Comment Utility
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
Comment Utility
got it. thank you
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

771 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

14 Experts available now in Live!

Get 1:1 Help Now