Solved

without 2 challenge

Posted on 2015-01-15
34
69 Views
Last Modified: 2015-02-07
Hi,

I am trying below challenge
http://codingbat.com/prob/p142247


Given a string, if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning, so "HelloHe" yields "lloHe". The substring may overlap with itself, so "Hi" yields "". Otherwise, return the original string unchanged.

without2("HelloHe") → "lloHe"
without2("HelloHi") → "HelloHi"
without2("Hi") → ""
'

i did not understand the challenge to proceed further. please advise.

thanks in advance.
0
Comment
Question by:gudii9
  • 18
  • 12
  • 4
34 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40552579
without2("HelloHe") → "lloHe"  "He" appears at both its beginning and end, so return return a string without the "He" at the beginning,
without2("HelloHi") → "HelloHi"    "He" at the beginning is not the same as the "Hi" at the end,  so return the original string unchanged.
without2("Hi") → ""     "Hi" appears at both its beginning and end, so return return a string without the "Hi" at the beginning.
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 150 total points
ID: 40553579
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40554206
public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(strLen-2);
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}

Open in new window


Expected	Run		
without2("HelloHe") → "lloHe"	"He"	X	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	"Hi"	X	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"xx"	X	    
without2("xx") → ""	"xx"	X	    
without2("x") → "x"	"x"	OK	    
without2("") → ""	""	OK	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests

Open in new window


i just typed as above and got below errors. please advise how to fix and improve my code
0
 
LVL 31

Expert Comment

by:awking00
ID: 40554297
>> please advise how to fix and improve my code <<
Use mine.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40554399
"if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning"
Your code instead returns the substring at the end

Also, your test is incorrect when strLen==2.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40554408
public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(2,strLen);
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}

Open in new window


does this looks good? I passed all tests.

I wonder how above code different from ID: 40553579 code. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40554437
ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2) and in not failing when str.length()<2
0
 
LVL 7

Author Comment

by:gudii9
ID: 40567183
ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2)

i do not see str.substring(2). I do find in old post  str.substring(0,2)

40554408 

public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(2,strLen);
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}





40553579 



public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

Open in new window


please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40567197
Thus it differs from http:#a40553579

str.substring(2)
is the same as
str.substring(2,strLen);
0
 
LVL 7

Author Comment

by:gudii9
ID: 40570664
not failing when str.length()<2
i wonder what you mean by
I have my code checking if length >2 righ as below
Please advise
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){

Open in new window


40554408 

public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(2,strLen);//same as substring(2) incorrect and not failing when str.length()<2
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}



40553579 



public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2) and in not failing when str.length()<2

i do not see str.substring(2). I do find in old post  str.substring(0,2) 
Thus it differs from http:#a40553579

str.substring(2)
is the same as
str.substring(2,strLen);

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40580666
ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2) and in not failing when str.length()<2

can you please elaborate on this comment?
I am not clear.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592023
40554408 

public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
[b]  strOut=str.substring(2,strLen);[/b]
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}

Open in new window


i see above one incorrectly using str.substring(2,strLen);
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592038
But it is passing all the tests though
Expected	Run		
without2("HelloHe") → "lloHe"	"lloHe"	OK	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	""	OK	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"x"	OK	    
without2("xx") → ""	""	OK	    
without2("x") → "x"	"x"	OK	    
without2("") → ""	""	OK	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests
OK

Open in new window


please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592044
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

Open in new window


infact above one failing many tests

Expected	Run		
without2("HelloHe") → "lloHe"	"He"	X	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	"Hi"	X	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"xx"	X	    
without2("xx") → ""	"xx"	X	    
without2("x") → "x"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests

Open in new window


may be i understand your comment in reverse way?
0
 
LVL 31

Expert Comment

by:awking00
ID: 40592106
As ozo pointed out, I incorrectly used str.substr(0,2), which would simply return the first two characters when what is needed if there is a match is everything beginning with the third character.
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }
  return str;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592299
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }
  return str;
}

Open in new window


it is failing few test cases. looks like cornere cases.

Expected	Run		
without2("HelloHe") → "lloHe"	"lloHe"	OK	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	""	OK	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"x"	OK	    
without2("xx") → ""	""	OK	    
without2("x") → "x"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests

Open in new window


how to handle them. please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40592307
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (str.length()>=2 && matches) {
    return str.substring(2);
  }else{
  return str;
  }
 // return str;
}

Open in new window


i wrote as above still failing some cases. please advise.

How many times we can write return statements in a method. Can i return one in if other in else other outside the if and also else as above? which infact did not work unless i comment one return.
please advise
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 84

Expert Comment

by:ozo
ID: 40592325
str.substring(0,2) does not make sense when str is "" or "x"
0
 
LVL 84

Expert Comment

by:ozo
ID: 40592328
You can write as many return statements in a method as you want, as long as all are reachable
0
 
LVL 7

Author Comment

by:gudii9
ID: 40593678
what is meaning of reachable. please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40593690
public String without2(String str) {

if (str.length()>=2){
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }else{
  return str;
  }
  
  
  }
  return str;
}

Open in new window


i passed all tests. I wonder how can i improve my code
Expected	Run		
without2("HelloHe") → "lloHe"	"lloHe"	OK	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	""	OK	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"x"	OK	    
without2("xx") → ""	""	OK	    
without2("x") → "x"	"x"	OK	    
without2("") → ""	""	OK	    
without2("Fruits") → "Fruits"

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40593697
0
 
LVL 7

Author Comment

by:gudii9
ID: 40593705
public String without2(String str) {

if (str.length()>=2){
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }else{
  return str;
  }  
  
  }
 // return str;
}

Open in new window


if i comment outer return statement getting below error

Compile problems:


Error:	public String without2(String str) {
	              ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically 
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value; 
so a value is always returned.

see Example Code to help with compile problems

Open in new window


i wonder why it is error. I have anyway supplied return withn if and other return else right?

is it is giving compilation error since i have not handled case where string length less than 2?

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40593710
in this example is it is advisable to create String st;
then assign to st in if and else loop then return st rather than returning 3 times separately in if , else outside if, else loops.
please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40593715
Line 8 is unnecessary, since if you don't return there, the control flow path will reach the return str; in line 13

Without line 8, line 7 becomes unnecessary.

I might use a single && test with no matches temporary, but that's a stylistic preference that others may disagree on, not an improvement.

If you might contemplate other variations of that code that may do other things after line 8 besides  return str;, then the line can serve the use of reminding you which variation is in effect in this case.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40593813
in this example is it is advisable to create String st;
I would probably call that a stylistic preference, and leave the choice to the individual programer.

One consideration for such decisions could be that if one is writing several related functions, then if one can build them all with a similar structure, it might make it easier to see the relationships between the functions, and simpler to modify one function into another.
(or if one thinks of the function they are writing as an example of a class of similar functions, then the structure of the program may be a reflection of the more general class of possible programs, and one's choice can be a way to suggest what kinds programs one thinks of as similar, or what modifications one may be contemplating)

For example, although I would probably not choose to use regular expressions for this particular challenge, if I were doing
extrafront challenge
startMatch challenge
without x challenge
without x2 challenge
deFront challenge
without 2 challenge
all at the same time, I might consider using return str.replaceAll(pattern,replacement); for all of them.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40593860
public String without2(String str) {

if (str.length()>=2&&str.substring(0,2).equals(str.substring(str.length() - 2))){
//  boolean matches = str.substring(0,2).equals(str.substring(str.length() - //2));
 // if (matches) {
    return str.substring(2);
 // }
  
  }
  return str;
}

Open in new window


i passed all tests as above. I still wonder why i have two return statements above.
How is unreachable code related with return statements which i often get. That link did not explain that specific point. please advise
0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 40593879
You have two return statements to account for the cases when the if condition is true, and when the if condition is false.

The unreachable code link was meant to address your "what is meaning of reachable" question.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40594358
public String withoutX(String str) {

if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+rest+lastChar;


}



}

Open in new window


i wonder above complains saying (from other challenge question but put here also to make my method return concept 100% concrete


Compile problems:


Error:      public String withoutX(String str) {
                    ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

see Example Code to help with compile problems
0
 
LVL 84

Expert Comment

by:ozo
ID: 40594552
What would happen in http:#a40594358 in the case when str.length()<1 ?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40595052
What would happen in http:#a40594358 in the case when str.length()<1 ?
i am not handling case when str.length()<1.
Flow comes out of if loop and exits method i guess since no else block and no code between end of if block bracket }

and method end bracket }
0
 
LVL 84

Expert Comment

by:ozo
ID: 40595348
And does it return a result of type String when it exits?
0
 
LVL 31

Expert Comment

by:awking00
ID: 40595653
Let's analyze your code from ID: 40594358 assuming an empty String (i.e a string with a length of 0)
public String withoutX(String str) { ==> This method must return a String value

if(str.length()>=1){ ==> This equals false so none of the next 5 lines are "reached" (i.e. They don't do anything)
            
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+rest+lastChar;

}

==> So nothing is returned, but the method requires returning a String. Therefore you get a compile error.
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40595769
Oh I see  now clearly how I am missing returning string which method expects when length 0.Thanks again for patiently explaining the mistake I did
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

759 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

23 Experts available now in Live!

Get 1:1 Help Now