Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

repeatSeparator java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p109637
I tried my code as below
public String repeatSeparator(String word, String sep, int count) {
 StringBuilder sb = new StringBuilder();
 StringBuilder sb2 = sb.append(word);
 if (count > 0) {
  for (int i = 0; i < count - 1; i++) {

   sb2.append(sep + word);
  }
  return sb2.toString();
 } else {

  return word;
 }

}

Open in new window

I am getting below result
Expected      Run            
repeatSeparator("Word", "X", 3) → "WordXWordXWord"      "WordXWordXWord"      OK         
repeatSeparator("This", "And", 2) → "ThisAndThis"      "ThisAndThis"      OK         
repeatSeparator("This", "And", 1) → "This"      "This"      OK         
repeatSeparator("Hi", "-n-", 2) → "Hi-n-Hi"      "Hi-n-Hi"      OK         
repeatSeparator("AAA", "", 1) → "AAA"      "AAA"      OK         
repeatSeparator("AAA", "", 0) → ""      "AAA"      X         
repeatSeparator("A", "B", 5) → "ABABABABA"      "ABABABABA"      OK         
repeatSeparator("abc", "XX", 3) → "abcXXabcXXabc"      "abcXXabcXXabc"      OK         
repeatSeparator("abc", "XX", 2) → "abcXXabc"      "abcXXabc"      OK         
repeatSeparator("abc", "XX", 1) → "abc"      "abc"      OK         
repeatSeparator("XYZ", "a", 2) → "XYZaXYZ"      "XYZaXYZ"      OK         
other tests
OK         
Correct for more than half the tests

Your progress graph for this problem


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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
public String repeatSeparator(String word, String sep, int count) {
  StringBuilder sb = new StringBuilder(word.length()*count+Math.min(count-1,0)*sep.length());
  String sw=sep+word;
  for (int c=0;c<count;c++){
     sb.append(word);
     word=sw;
  }
  return sb.toString();
}
Lol, yes, I completely forgot the sep length for theStringBuilder constructor. But, nevermind, it doesn't need it anyway. ;)
Avatar of gudii9

ASKER

public String repeatSeparator(String word, String sep, int count) {
 StringBuilder sb = new StringBuilder();
 StringBuilder sb2 = sb.append(word);
 if (count > 0) {
  for (int i = 0; i < count - 1; i++) {

   sb2.append(sep + word);
  }
  return sb2.toString();
 } else {

  return word;
 }

}

Open in new window


how to fix my approach to resolve only 1 test that is failing?
Avatar of gudii9

ASKER

public String repeatSeparator(String word, String sep, int count) {
StringBuilder sb=new StringBuilder(word.length()*count);
StringBuilder sb2=sb.append(word);
if(count>0){
for(int i=0;i<count-1;i++){

sb2.append(sep+word);
}
return sb2.toString();
}
else if(count==1){
return word;
}
else{
word;
}
  
}


 // if(count==1){return word;}
  //StringBuilder sb = new StringBuilder(word.length()*count);
 // for (int c=0;c<count;c++){sb.append(word+sep);}
//  return sb.toString().substring(0,sb.toString().length()-sep.length());

Open in new window


above gives below error

Compile problems:


Error:      word;
      ^^^^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete Expression


see Example Code to help with compile problems

not sure why.

please advise
Avatar of gudii9

ASKER

public String repeatSeparator(String word, String sep, int count) {
 StringBuilder sb = new StringBuilder();
 StringBuilder sb2 = sb.append(word);
 if (count > 1) {
  for (int i = 0; i < count - 1; i++) {

   sb2.append(sep + word);
  }
  return sb2.toString();
 } else if(count == 1){

  return word;
 }


else{

  return "";
 }
}

Open in new window


i fixed as above and passing all tests
Avatar of gudii9

ASKER

public String repeatSeparator(String word, String sep, int count) {
StringBuilder sb=new StringBuilder(word.length()*count);
StringBuilder sb2=sb.append(word);
if(count>0){
for(int i=0;i<count-1;i++){

sb2.append(sep+word);
}
return sb2.toString();
}
else if(count==1){
return word;
}
else{
return "";
}
  
}


 // if(count==1){return word;}
  //StringBuilder sb = new StringBuilder(word.length()*count);
 // for (int c=0;c<count;c++){sb.append(word+sep);}
//  return sb.toString().substring(0,sb.toString().length()-sep.length());

                    

Open in new window


 forgot to put return before word which gave compilation errror which i fixed now
Avatar of gudii9

ASKER

public String repeatSeparator(String word, String sep, int count) {

  if( count>0 ){ return word+sep+repeatSeparator(word,sep,count-1); }
    if( count==1 ){ return word; }
  return "";
}

if i change order fails lot of tests.

how to find order.
[
more generic to specific or
rule is more specific to more generic
public String repeatSeparator(String word, String sep, int count) {
  if( count==1 ){ return word; }
  if( count>0 ){ return word+sep+repeatSeparator(word,sep,count-1); }
  
  return "";
}

Open in new window


seems rule more specific to more generic like specific guys need to be satisfied then generic people always happy
When count is 1, both count==1 and count>0 are true.
What do you think should happen in that case?
When both conditions are true, the return that happens first prevents the second return from happening, so order matters.

When only one of the conditions is true, then the order would not matter.
Can you think of a way to modify the conditions so that they are never both true?
Avatar of gudii9

ASKER

When count is 1, both count==1 and count>0 are true.
What do you think should happen in that case?
When both conditions are true, the return that happens first prevents the second return from happening, so order matters.

i agree.

i will remember this in my mind..never let normal course of flow obstruct second return from happening
I'm not sure how you decide what is "normal", but if you want the second return to happen, then you should not obstruct it from happening, when you don't want it to happen, then you should obstruct it from happening.

If the guard conditions were mutually exclusive, like if( count==1 ) and if( count>1 ) then you could choose to put them in any order without affecting which return happens.  
(the order could still affect which conditions were tested, which could matter to you if one of the test conditions was very complicated or had side effects)
On the other hand, if you fix the order of the returns, then you could have more freedom in choosing the guard conditions since the second guard condition would not need to decide a case that had already been caught by the first.
Avatar of gudii9

ASKER

If the guard conditions were mutually exclusive, like if( count==1 ) and if( count>1 ) then you could choose to put them in any order without affecting which return happens.  

i agree on this