?
Solved

twoCahr challenge

Posted on 2014-12-04
20
Medium Priority
?
110 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
[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
  • 11
  • 6
  • 3
20 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 1000 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 1000 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

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