prefixAgain java challenge

gudii9
gudii9 used Ask the Experts™
on
Hi,
I am working on below challenge
http://codingbat.com/prob/p136417
I tried my code as below
public boolean prefixAgain(String str, int n) {

 String strNew = str.substring(0, n - 1);
 int len = str.length();
 String strLaterHalf = str.substring(n, len);
 if (strLaterHalf.contains(strNew)) {

  return true;
 } else {
  return false;
 }

}

Open in new window

I am getting below result
Expected      Run            
prefixAgain("abXYabc", 1) → true      true      OK         
prefixAgain("abXYabc", 2) → true      true      OK         
prefixAgain("abXYabc", 3) → false      true      X         
prefixAgain("xyzxyxyxy", 2) → true      true      OK         
prefixAgain("xyzxyxyxy", 3) → false      true      X         
prefixAgain("Hi12345Hi6789Hi10", 1) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 2) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 3) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 4) → false      true      X         
prefixAgain("a", 1) → false      true      X         
prefixAgain("aa", 1) → true      true      OK         
prefixAgain("ab", 1) → false      true      X         
other tests
X         

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
return str.substring(1).contains(str.substring(0,n));

Open in new window

Here is your code, reworked, btw :

public boolean prefixAgain(String str, int n) {
  String strNew = str.substring(0, n);
 int len = str.length();
 String strLaterHalf = str.substring(1, len);
 if (strLaterHalf.contains(strNew)) {

  return true;
 } else {
  return false;
 }
}

Open in new window

ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
String strLaterHalf = str.substring(n, len);
and
 String strLaterHalf = str.substring(1, len);
would interpret the problem specification differently on
prefixAgain("aaa", 2)
That does not seem to be one of the "other tests", so the ambiguity remains unresolved.
My preference would be for return str.substring(1).contains(str.substring(0,n));
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

My preference would be for return str.substring(1).contains(str.substring(0,n));

Which is what I put in my first comment.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Presumably that's why you changed n to 1 in gudii9's code, so that it would be consistent with yours, even though that change was not necessary in order to pass the tests.
Well I felt that using the 1 would help in seeing the fundamental mechanics of moving that pointer - rather than bringing n into it again, as it tends to "overload" n's use case, whereas if gudii asks "why is there a 1 there", then potential ambiguity can be avoided.

I am also unsure whether the question really intends to avoid overlapping or not, so 1 seemed better.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
We seem to share the intuition that substring(1) is somehow a more fundamental interpretation of the problem statement than substring(n).
Yet we see that other intuitions are possible, and we lack the evidence to say that one is correct and one is incorrect.
Other challenges on the codingbat site seem to have similar ambiguities, and the tests similarly seem to fail to resolve the ambiguity, which may suggest the lack of resolution is deliberate.
which may suggest the lack of resolution is deliberate.

Pleased you qualified that statement with the word "may". Because I would have thought that the very worst place for demonstrating ambiguity, is in a teaching/learning environment. This, imho, is even worse when there are no hints or visible caveats telling the user that they should be cautious in drawing conclusions about the overall definitiveness of obtaining a "passed all tests" result.

This weakness has shown itself up several times in these challenges, and to me leaves the door wide-open to the criticism that "a little knowledge is a dangerous thing". I wouldn't want these people to churn out programmers for coding my driverless car or fbw aircraft, nor any number of other criticial procedures where a proper understanding of the task was essential, rather than just the quick-and-dirty fingers of trigger-happy hackers. Perhaps CB has a retinue of lawyers ready behind the scenes somewhere.
awking00Information Technology Specialist
Commented:
I think the choice of str.substring(1,len) [or just str.substring(1)] as being the "better" choice is based on CodingBat's description of "Does that prefix string appear somewhere else in the string?" as opposed to "... appear later in the string?" Of course that is still a matter of interpretation and they don't give an example that demonstrates their intent.

Author

Commented:
public boolean prefixAgain(String str, int n) {

String strNew=str.substring(0,n);
int len=str.length();
String strLaterHalf=str.substring(n, len);
if( strLaterHalf.contains(strNew)){

return true;
}

else{
return false;
}
  
}

//

Open in new window


i see mistake i did is added -1 unnecessarily. above passing all tests

Author

Commented:
public class PrefixAgainEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Values is-->"+prefixAgain("abXYabc", 3));
	}

	
	public static boolean prefixAgain(String str, int n) {

		String strNew=str.substring(0,n);
		System.out.println("strNew is-->"+strNew);
		int len=str.length();
		String strLaterHalf=str.substring(n, len);
		System.out.println("strLaterHalf is-->"+strLaterHalf);
		if( strLaterHalf.contains(strNew)){

		return true;
		}

		else{
		return false;
		}
		  
		}

}

Open in new window


above give below correctr result
strNew is-->abX
strLaterHalf is-->Yabc
Values is-->false

Author

Commented:
public class PrefixAgainEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Values is-->"+prefixAgain("aaa", 2));
	}

	
	public static boolean prefixAgain(String str, int n) {

		String strNew=str.substring(0,n);
		System.out.println("strNew is-->"+strNew);
		int len=str.length();
		String strLaterHalf=str.substring(n, len);
		System.out.println("strLaterHalf is-->"+strLaterHalf);
		if( strLaterHalf.contains(strNew)){

		return true;
		}

		else{
		return false;
		}
		  
		}

}

Open in new window


above passed the test of  prefixAgain("aaa", 2));
      }


My preference would be for return str.substring(1).contains(str.substring(0,n));



I wonder why we choose 1 in above solution why not 2 or 3?? just to be safer side that n is always there 1 or more??
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
return str.substring(2).contains(str.substring(0,n)); is clearly wrong in this case:
Expected      Run
prefixAgain("aa", 1) → true      false

Author

Commented:
package simple.servlet;
public class PrefixAgainEx {

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

	
	public static boolean prefixAgain(String str, int n) {

		String strNew=str.substring(0,n);
		System.out.println("strNew is-->"+strNew);
		int len=str.length();
		String strLaterHalf=str.substring(n, len);
		System.out.println("strLaterHalf is-->"+strLaterHalf);
		if( strLaterHalf.contains(strNew)){

		return true;
		}

		else{
		return false;
		}
		  
		}

}

Open in new window


i got result as true when i run same as expected
strNew is-->a
strLaterHalf is-->a
Values is-->true

Author

Commented:
public boolean prefixAgain(String str, int n) {

return str.substring(2).contains(str.substring(0,n));
}


above failed two test cases as you mentioned

Expected      Run            
prefixAgain("abXYabc", 1) → true      true      OK         
prefixAgain("abXYabc", 2) → true      true      OK         
prefixAgain("abXYabc", 3) → false      false      OK         
prefixAgain("xyzxyxyxy", 2) → true      true      OK         
prefixAgain("xyzxyxyxy", 3) → false      false      OK         
prefixAgain("Hi12345Hi6789Hi10", 1) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 2) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 3) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 4) → false      false      OK         
prefixAgain("a", 1) → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)      X         
prefixAgain("aa", 1) → true      false      X         
prefixAgain("ab", 1) → false      false      OK         
other tests
OK         

Author

Commented:
public boolean prefixAgain(String str, int n) {

return str.substring(1).contains(str.substring(0,n));
}

Open in new window


above passed all tests

Expected      Run            
prefixAgain("abXYabc", 1) → true      true      OK         
prefixAgain("abXYabc", 2) → true      true      OK         
prefixAgain("abXYabc", 3) → false      false      OK         
prefixAgain("xyzxyxyxy", 2) → true      true      OK         
prefixAgain("xyzxyxyxy", 3) → false      false      OK         
prefixAgain("Hi12345Hi6789Hi10", 1) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 2) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 3) → true      true      OK         
prefixAgain("Hi12345Hi6789Hi10", 4) → false      false      OK         
prefixAgain("a", 1) → false      false      OK         
prefixAgain("aa", 1) → true      true      OK         
prefixAgain("ab", 1) → false      false      OK         
other tests
OK

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial