gudii9
asked on
extrafront challenge
Hi,
I am trying below challenge
http://codingbat.com/prob/p172063
I wrote as below and passing all tests.
i would like to know how can i improve my code. thanks in advance
I am trying below challenge
http://codingbat.com/prob/p172063
I wrote as below and passing all tests.
public String extraFront(String str) {
int strLen=str.length();
String strOutput="";
if(strLen>=2){
strOutput= str.substring(0,2)+str.substring(0,2)+str.substring(0,2);
return strOutput;
}
if(strLen<2){
strOutput= str+str+str;
return strOutput;
}
return strOutput;
}
i would like to know how can i improve my code. thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public String extraFront(String str) {
int idx = str.length() == 1 ? 1 : 2;
return StringUtils.repeat(str.substring(0, idx),3);
}
when i wrote like above
i am getting
Compile problems:
Error: return StringUtils.repeat(str.sub
^^^^^^^^^^^
StringUtils cannot be resolved
please advise
He's using a 3rd party library - you won't want to use StringUtils.
If you're testing this code just use:
String front = str.substring(0, idx) ;
return front + front + front ;
Doug
If you're testing this code just use:
String front = str.substring(0, idx) ;
return front + front + front ;
Doug
ASKER
int idx = str.length() == 1 ? 1 : 2;
what is meaning of above line
what is meaning of above line
ASKER
public String extraFront(String str) {
int idx = str.length() == 1 ? 1 : 2;
String st=str.substring(0, idx);
return str+str+str;
}
when i wrote as above failing test cases as below
Expected Run
extraFront("Hello") → "HeHeHe" "HelloHelloHello" X
extraFront("ab") → "ababab" "ababab" OK
extraFront("H") → "HHH" "HHH" OK
extraFront("") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:3)" X
extraFront("Candy") → "CaCaCa" "CandyCandyCandy" X
extraFront("Code") → "CoCoCo" "CodeCodeCode" X
other tests
X
Your progress graph for this problem
please advise
In in http:#a40554355, String st is never used for anything.
Also, the case of str.length()==0 handled improperly.
Also, the case of str.length()==0 handled improperly.
ASKER
public String extraFront(String str) {
int idx = str.length() == 1 ? 1 : 2;
String st=str.substring(0, idx);
return st+st+st;
}
i fixed by removing 'r' from str
now i fail one test case. please advise
Expected Run
extraFront("Hello") → "HeHeHe" "HeHeHe" OK
extraFront("ab") → "ababab" "ababab" OK
extraFront("H") → "HHH" "HHH" OK
extraFront("") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:3)" X
extraFront("Candy") → "CaCaCa" "CaCaCa" OK
extraFront("Code") → "CoCoCo" "CoCoCo" OK
other tests
OK
The code you posted isn't handling a string of length 0.
This will work:
However as I posted up at the top this is computing the same thing in a clearer and less error-prone way:
This will work:
public String extraFront(String str) {
int idx = str.length() == 1 ? 1 : 2;
if (str.length() == 0) idx = 0 ;
String st=str.substring(0, idx);
return st+st+st;
}
However as I posted up at the top this is computing the same thing in a clearer and less error-prone way:
public String extraFront(String str) {
int len = Math.min(str.length(),2) ;
String firstTwo = str.substring(0, len) ;
return firstTwo + firstTwo + firstTwo ;
}
I agree that my original submission did not account for a string lengthof 0 and shouldn't be used and dpearson's method of determining the len (i.e. index) to use is best. One modification that could be made is to return the repeated string in one line without the concatenation -
public String extraFront(String str) {
int len = Math.min(str.length(),2) ;
return new String(new char[3]).replace("\0",str. substring( 0, idx));
}
public String extraFront(String str) {
int len = Math.min(str.length(),2) ;
return new String(new char[3]).replace("\0",str.
}
ASKER
int idx = str.length() == 1 ? 1 : 2;
what is meaning of above line in plain english.
return new String(new char[3]).replace("\0",str.what is meaning of above line in plain english.substring( 0, idx));
please advise
ASKER
public String extraFront(String str) {
int idx = str.length() == 1 ? 1 : 2;
if (str.length() == 0) idx = 0 ;
String st=str.substring(0, idx);
return st+st+st;
}
by the way i passed all tests as above
int idx = str.length() == 1 ? 1 : 2;means
if( str.length() == 1 ){
idx = 1;
}else{
idx = 2;
}
By the way, although using str.replace is not the method I'd be primarily inclined to choose for this challenge,
and although this, as well as your other
startMatch challenge
without x challenge
without x2 challenge
deFront challenge
without 2 challenge
questions are all simple enough to be done with basic String manipulation operations of the kind you may be getting used to by now,
so that it could seem overkill to throw the weight of a regular expression replacement mechanism at problems that can be easily solved using the familiar tools you are trying to learn to master,
it may be of interest to note that all of those challenges can be solved with a one line program that looks like
return str.replaceAll(pattern,replacement);
and although this, as well as your other
startMatch challenge
without x challenge
without x2 challenge
deFront challenge
without 2 challenge
questions are all simple enough to be done with basic String manipulation operations of the kind you may be getting used to by now,
so that it could seem overkill to throw the weight of a regular expression replacement mechanism at problems that can be easily solved using the familiar tools you are trying to learn to master,
it may be of interest to note that all of those challenges can be solved with a one line program that looks like
return str.replaceAll(pattern,replacement);
ASKER
so when i see ? kind of regular expression then if and else loop should ring bell in mind. Also
return str.replaceAll(pattern,repcan you please elaborate on thislacement);
ASKER
so that it could seem overkill to throw the weight of a regular expression replacement mechanism at problems that can be easily solved using the familiar tools you are trying to learn to master,
i wonder which is the regular expression replacement mechanism.
In other post i got coment like below kind of format is not regular expression but short cut if else condition?
int idx = str.length() == 1 ? 1 : 2;
please advise
ASKER
replaceAll
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str). replaceAll (repl)
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.S tring) to suppress the special meaning of these characters, if desired.
Parameters:
regex - the regular expression to which this string is to be matched
replacement - the string to be substituted for each match
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern
i was reading aove API. I wonder where we are using above method/regular expression pattern in this challenge. Please advise
We are not using the above regular expression method in this challenge.
The comment was that we could use the above regular expression method in this, as well as several other challenges.
The comment was that we could use the above regular expression method in this, as well as several other challenges.
ASKER
We are not using the above regular expression method in this challenge.
The comment was that we could use the above regular expression method in this, as well as several other challenges.
can you please advise how to use regular exoression for this and other challenges like
startMatch challenge
without x challenge
without x2 challenge
deFront challenge
without 2 challenge
I'd advise that it is more useful to learn to use the basic tools you know than to delve into more unfamiliar specialized tools, but for the sake of closure, one way to do this challenge with replaceAll is
return str.replaceAll("(..?).*"," $1$1$1");
return str.replaceAll("(..?).*","
ASKER
sure.. i will probably open separate question with regular expresiions later
ASKER
what is meaning of above line
if
str.length() == 1 is true then idx is 1
if str.length() == 1 is false then idx is 2?
how it is taking care of string with more than 2 length.
please advise