Solved

noX challenge

Posted on 2016-09-15
17
162 Views
Last Modified: 2016-09-20
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

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;
}

Open in new window




I am passing all tests

How to improve/modify my design, code and any other alternate approaches. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
17 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 41800015
The instructions say you have to do this challenge recursively.
What would happen if you modified your code to just call replace without all those if statements?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41800197
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41800267
>> 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.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 27

Expert Comment

by:rrz
ID: 41800348
@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.
0
 
LVL 27

Expert Comment

by:rrz
ID: 41801661
I'm sorry. I was wrong.   Your choice of the replace method is fine.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41802261
>> 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.
0
 
LVL 27

Expert Comment

by:rrz
ID: 41802342
@phoffric, I just meant that my previous suggestion was not helpful.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41804116
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));
     
   }
}

Open in new window


above passes all tests,

any improvement or alternate approaches?
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41804162
Your code is good. But,  is the following more readable?
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));
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 41804256
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.
0
 
LVL 27

Expert Comment

by:rrz
ID: 41804894
0
 
LVL 7

Author Comment

by:gudii9
ID: 41805448
not clear on above comments. are we saying not to use recursion if more characters to check?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41805468
@rrz,
I am unclear why you are identifying posts in other questions. Are you asking me to read them and verify them? Or what?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 41805469
@gudii9,
>> are we saying not to use recursion
This challenge forces us to use recursion. But you should be aware that each time you recurse, you are creating a function stack frame, which can eat up your allotted stack space if not careful.

If the string is 10 thousand chars long, then your current approach will make 10000 function calls - one for every character, and the stack will have 10000 frames. Now, if you knew, for example, that the undesired char was say 1/10th of 1%, then by using indexOf function to find the undesired char, you would only have to recurse  0.001 * 10000 = 10 calls (i.e., only 10 stack frames).

In fact, I once had to get rid of a spurious undesired char in a Word document, so if using recursion, then we don't want to recurse on every char.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28int%29
0
 
LVL 27

Expert Comment

by:rrz
ID: 41805553
@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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41807420
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));
}*/

Open in new window

above passes all
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

691 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