gudii9
asked on
noX challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p118230
Psedo code:
1. if array length is zero return ""
2. if array length is 1 and it is x return ""
3. if array length is 1 and it is not x return same str
4. else find location of x and replace with "" if greater than length
5. return replaced string
I am passing all tests
How to improve/modify my design, code and any other alternate approaches. please advise
I am working on below challenge
http://codingbat.com/prob/p118230
Psedo code:
1. if array length is zero return ""
2. if array length is 1 and it is x return ""
3. if array length is 1 and it is not x return same str
4. else find location of x and replace with "" if greater than length
5. return replaced string
public String noX(String str) {
if(str.length()==0){
return "";
}
if(str.length()==1&&"x".equals(str)){
return "";
}
if(str.length()==1&&!"x".equals(str)){
return str;
}
if(str.length()>1){
return str.replace("x","");
}
return null;
}
I am passing all tests
How to improve/modify my design, code and any other alternate approaches. please advise
No recursion is necessary for this challenge. You could just use
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-
>> No recursion is necessary for this challenge.
You are already stating what the author has observed.
But the challenge is to learn how to use recursion.
You are already stating what the author has observed.
But the challenge is to learn how to use recursion.
@phoffric, gudii9 has already gone through 11 recursion challenges last week. He will not learn anything by using recursion when it is not useful. He really needs to learn the API better.
I'm sorry. I was wrong. Your choice of the replace method is fine.
>> I'm sorry. I was wrong. Your choice of the replace method is fine.
Could you elaborate on the meaning of this?
Are you saying that the author's solution is fine by using the replace method?
I have the same question where you wrote this in two other questions from this author.
Could you elaborate on the meaning of this?
Are you saying that the author's solution is fine by using the replace method?
I have the same question where you wrote this in two other questions from this author.
@phoffric, I just meant that my previous suggestion was not helpful.
ASKER
public String noX(String str) {
// public String changeXY(String str) {
if (str.length() == 0){
return str;
}
else if (str.charAt(0) == 'x') {
return "" + noX(str.substring(1));
}
else{
return str.charAt(0) + noX(str.substring(1));
}
}
above passes all tests,
any improvement or alternate approaches?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In this question and in others, it looks like you all are recursively examining one char at a time. If the string is 10 thousand chars long, then the stack will have 10000 frames.
How about doing a find operation in noX?
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28int%29
Then, if there are only 1% of the undesired char, the stack will only have 100 frames.
How about doing a find operation in noX?
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28int%29
Then, if there are only 1% of the undesired char, the stack will only have 100 frames.
@phoffric, I have already demonstrated the use of indexOf at
https://www.experts-exchange.com/questions/28970071/changeXy-challenge.html?anchorAnswerId=41804107#a41804107
and
https://www.experts-exchange.com/questions/28968901/countHi-challenge.html?anchorAnswerId=41797015#a41797015
and
https://www.experts-exchange.com/questions/28968900/countX.html?anchorAnswerId=41796932#a41796932
https://www.experts-exchange.com/questions/28970071/changeXy-challenge.html?anchorAnswerId=41804107#a41804107
and
https://www.experts-exchange.com/questions/28968901/countHi-challenge.html?anchorAnswerId=41797015#a41797015
and
https://www.experts-exchange.com/questions/28968900/countX.html?anchorAnswerId=41796932#a41796932
ASKER
not clear on above comments. are we saying not to use recursion if more characters to check?
@rrz,
I am unclear why you are identifying posts in other questions. Are you asking me to read them and verify them? Or what?
I am unclear why you are identifying posts in other questions. Are you asking me to read them and verify them? Or what?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@phoffric and gudii9, I posted those three links to show examples of where I used indexOf in recent challenges. The same approach I used in those examples, can be used in this challenge.
ASKER
public String noX(String str) {
// public String changeXY(String str) {
if("".equals(str)){
return "";
}
else if (str.charAt(0) == 'x') {
return "" + noX(str.substring(1));
}
else{
return str.charAt(0) + noX(str.substring(1));
}
}
/* public String noX(String str) {
if ("".equals(str)){
return "";
}
if (str.charAt(0) == 'x') {
return noX(str.substring(1));
}
return str.charAt(0) + noX(str.substring(1));
}
A*/
/* public String changePi(String str) {
if (str.equals("") || str.length() < 2) return str;
if (str.charAt(0) == 'p' && str.charAt(1) == 'i')
return "3.14" + changePi(str.substring(2));
return str.charAt(0) + changePi(str.substring(1));
}*/
above passes all
What would happen if you modified your code to just call replace without all those if statements?