Solved

twoCahr challenge

Posted on 2014-12-04
20
101 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
 
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
3 Use Cases for Connected Systems

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 44
servlet  URL Rewriting 1 25
Way to decrease size of apk file 9 42
Eclipse with gitlab 1 10
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now