Solved

changePi Challenge

Posted on 2016-09-15
15
75 Views
Last Modified: 2016-09-20
Hi,

I am working on below challenge
http://codingbat.com/prob/p170924

Psedo code:

1. if array length is zero return ""
2. if array length is 1  return same str
3. else find location of pi and replace with 3.14 if greater than length
4. return replaced string

I wrote my code as below

public String changePi(String str) {
   if(str.length()==0){
    return "";
  }
  if(str.length()==1){
    return str;
  }
  if(str.length()==2&&"pi".equals(str)){
    return "3.14";
  }
   if(str.length()==1&&!"x".equals(str)){
    return str;
  }
   if(str.length()>1){
    return str.replace("pi","3.14");
  }
  
  
  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
  • 4
  • 3
  • 3
  • +1
15 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 41799990
>> Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14".

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: 41800191
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41800271
>> 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
 

Expert Comment

by:Filonowst
ID: 41800312
phoffric,
I respect the fact that you did not find my answer acceptable. I feel the best way to learn recursion is to step through an example. To try to explain this one without the code would be more difficult for both the explainer and the explainee. This is a fairly basic recursion question and there are many online tutorials as to how recursion works. So I guess my answer turns into: lookup some basic recursion tutorials.

OP,
The question specifically asks for a recursive solution which your code is not.
0
 

Expert Comment

by:Filonowst
ID: 41800313
Sorry for the confusion but I just changed my profile name from IT Manager.
0
 
LVL 27

Expert Comment

by:rrz
ID: 41800347
@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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:phoffric
ID: 41800369
@rrz,
>> He will not learn anything by using recursion when it is not useful.  He really needs to learn the API better.

Of course, learning the API is critical to becoming a successful professional. You are welcome to advise the author for this generality.

However, the author is choosing which challenges to participate in. If this question were on a college test, then he would get zero credit for the posted solution. It is up to the author to begin to realize why he got the right answer without using recursion (and that will be useful to him), and then proceed, if he wants, to come up with a recursive solution as required by the challenge.
0
 
LVL 27

Expert Comment

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

Author Comment

by:gudii9
ID: 41804074
public String changePi(String str) {

    if (str.length() == 0){ 
      return str;
      
    }
   else if (str.substring(0,2) == "pi") {
     return "3.14" + changePi(str.substring(2));
     
   }
   else{ 
     return str.substring(0,2) + changePi(str.substring(2));
     
   }
}
  
  
 

Open in new window

Expected      Run            
changePi("xpix") → "x3.14x"      "xpix"      X      
changePi("pipi") → "3.143.14"      "pipi"      X      
changePi("pip") → "3.14p"      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:7)      X      
changePi("pi") → "3.14"      "3.14"      OK      
changePi("hip") → "hip"      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:7)      X      
changePi("p") → "p"      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:7)      X      
changePi("x") → "x"      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:7)      X      
changePi("") → ""      ""      OK      
changePi("pixx") → "3.14xx"      "pixx"      X      
changePi("xyzzy") → "xyzzy"      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:7)      X      
other tests
X

wrote as above and failing some tests.

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 41804112
public String changePi(String str) {

    if (str.length()==0||str.length()<2){ 
      return str;
      
    }
   else if (str.substring(0,2) == "pi") {
     return "3.14" + changePi(str.substring(2));
     
   }
   else{ 
     return str.charAt(0) + changePi(str.substring(1));
     
   }
}
  
  
 
 

Open in new window


above passes more tests but still fails some. please advise
Expected      Run            
changePi("xpix") → "x3.14x"      "xpix"      X      
changePi("pipi") → "3.143.14"      "pipi"      X      
changePi("pip") → "3.14p"      "pip"      X      
changePi("pi") → "3.14"      "3.14"      OK      
changePi("hip") → "hip"      "hip"      OK      
changePi("p") → "p"      "p"      OK      
changePi("x") → "x"      "x"      OK      
changePi("") → ""      ""      OK      
changePi("pixx") → "3.14xx"      "pixx"      X      
changePi("xyzzy") → "xyzzy"      "xyzzy"      OK      
other tests
X      
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 41804125
You are almost there. You just forgot not to use == for comparing String content. Please use the String method.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41807401
public String changePi(String str) {

    if (str.length()==0||str.length()<2){ 
      return str;
      
    }
   else if (str.substring(0,2).equals("pi")) {
     return "3.14" + changePi(str.substring(2));
     
   }
   else{ 
     return str.charAt(0) + changePi(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

passed all
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

19 Experts available now in Live!

Get 1:1 Help Now