• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 119
  • Last Modified:

twoCahr challenge

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
gudii9
Asked:
gudii9
  • 11
  • 6
  • 3
2 Solutions
 
ozoCommented:
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
 
gudii9Author Commented:
but the lenght and index are dynamic right? I cannot hard code to 5 or 3 either. please advise
0
 
Zsolt PribuszCommented:
You need to change line 2
from:
if(index<=str.length()/2&&index>=0)

to:

if(index < str.length()-1 && index>=0)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ozoCommented:
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
 
gudii9Author Commented:
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
 
gudii9Author Commented:
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
 
gudii9Author Commented:

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
 
gudii9Author Commented:
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
 
ozoCommented:
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
 
gudii9Author Commented:
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
 
gudii9Author Commented:
Does my code looks fine. Any improvements i can make?
please advise
0
 
Zsolt PribuszCommented:
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
 
gudii9Author Commented:
"IF" you could exchange the evaluations.

How to do it. please advise
0
 
ozoCommented:
What part of "exchange the evaluations" is unclear?
0
 
gudii9Author Commented:
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
 
ozoCommented:
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
 
gudii9Author Commented:
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
 
ozoCommented:
I believe that is what Zsolt Pribusz was suggesting.
0
 
Zsolt PribuszCommented:
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
 
gudii9Author Commented:
got it. thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now