gudii9
asked on
deFront challenge
Hi,
I am trying below challenge
http://codingbat.com/prob/p110141
I did not understand challenge clearly. can you please advise. Thanks in advance
I am trying below challenge
http://codingbat.com/prob/p110141
Given a string, return a version without the first 2 chars. Except keep the first char if it is 'a' and keep the second char if it is 'b'. The string may be any length. Harder than it looks.
deFront("Hello") → "llo"
deFront("java") → "va"
deFront("away") → "aay"
I did not understand challenge clearly. can you please advise. Thanks in advance
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The challenge basically says return the string without the first two characters with the exceptions that, if the first character is an a, keep it and/or if the second character is a b, then keep it. So given the first two examples of "Hello" and "java", neither have a first character of a or a second character of b, so it returns the string without the first two characters. In the third example, "away", since a is the first character keep it but also return the remaining string without the first two characters. I would assume that a string like "ebay" would return "eay" (since b is the second character) or a string like "about" would return "about" (since a is the first character and b is the second character).
"ebay" would return "eay"
I'm pretty sure it should be "bay"
public String deFront(String str) {
String remainStr = str.substring(2);
String a = "";
String b = "";
if (str.startsWith("a")) { a = "a"; }
if (str.startsWith("b", 1)) { b = "b"; }
return a + b + remainStr;
}
Kimputer, thanks for catching my error. Of course, it should be "bay" which is what my code above would return.
ASKER
"ebay" would return "eay"
I'm pretty sure it should be "bay"
i too thought it should return "eay" based on comment ID: 40553607.
(by the way how to insert comment like ID: 40553607 as link. I do not know that trick. please advise)
I wonder why it should be "bay"?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ASKER
Test passed
ASKER
For an internal link within the same question, add "http:#a" to the ID
http:#a40553607
How to do in different question
ASKER
public String deFront(String str) {
String st=str;
if(st.substring(0,1).equals("a"))
{
st=st;
}
else
{
st=st.substring(1);
}
if(st.substring(1,2).equals("b"))
{
st=st;
}
else
{
st=st.substring(0,1)+st.substring(2);
}
return st;
}
i tried as above failing some test cases. please advise
Expected Run
deFront("Hello") → "llo" "elo" X
deFront("java") → "va" "aa" X
deFront("away") → "aay" "aay" OK
deFront("axy") → "ay" "ay" OK
deFront("abc") → "abc" "abc" OK
deFront("xby") → "by" "b" X
deFront("ab") → "ab" "ab" OK
deFront("ax") → "a" "a" OK
deFront("axb") → "ab" "ab" OK
deFront("aaa") → "aa" "aa" OK
deFront("xbc") → "bc" "b" X
deFront("bbb") → "bb" "bb" OK
deFront("bazz") → "zz" "az" X
deFront("ba") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:17)" X
deFront("abxyz") → "abxyz" "abxyz" OK
deFront("hi") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:17)" X
deFront("his") → "s" "i" X
deFront("xz") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:17)" X
deFront("zzz") → "z" "z" OK
other tests
X
Correct for more than half the tests
Your progress graph for this problem
ASKER
public String deFront(String str) {
String st=str;
if(st.substring(0,1).equals("a"))
{
st=st;
}
else
{
st=st.substring(1);
}
if(st.substring(1,2).equals("b"))
{
st=st;
}
else
{
st=st.substring(1);
}
return st;
}
i modified as above.
still failing some test cases
Expected Run
deFront("Hello") → "llo" "llo" OK
deFront("java") → "va" "va" OK
deFront("away") → "aay" "way" X
deFront("axy") → "ay" "xy" X
deFront("abc") → "abc" "abc" OK
deFront("xby") → "by" "y" X
deFront("ab") → "ab" "ab" OK
deFront("ax") → "a" "x" X
deFront("axb") → "ab" "xb" X
deFront("aaa") → "aa" "aa" OK
deFront("xbc") → "bc" "c" X
deFront("bbb") → "bb" "bb" OK
deFront("bazz") → "zz" "zz" OK
deFront("ba") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:17)" X
deFront("abxyz") → "abxyz" "abxyz" OK
deFront("hi") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:17)" X
deFront("his") → "s" "s" OK
deFront("xz") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:17)" X
deFront("zzz") → "z" "z" OK
other tests
ASKER
I wonder how i got below output as llo for my code
Hello
01234
since substring(1) should include begin index at 1 right
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)
please advise
st=st.substring(1);
deFront("Hello") → "llo" "llo" OK
Hello
01234
since substring(1) should include begin index at 1 right
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)
please advise
Can you explain what you are doing? As the answer was already given by awking00 in the 4th post?
ASKER
i just want to try my own way also so that i know if i make some mistakes i can correct which probably could be other approach.
How to do in different questionhttps://www.experts-exchange.com/questions/28597672/deFront-challenge.html?anchorAnswerId=40553607#a40553607
would work as a link from anywhere
The code you wanted to try yourself has SEVERAL flaws.
You used fixed lengths (substring(x,y), so it means it will throw errors if you don't check for the correct length and you input something out of bounds.
Second, you have 2 pieces of code that do not comply with the problem:
This is never to be found in your code
So, the problem required you to work out every line in unison, and you have two seperate if statements, so it will never work if you keep coding that way.
You used fixed lengths (substring(x,y), so it means it will throw errors if you don't check for the correct length and you input something out of bounds.
Second, you have 2 pieces of code that do not comply with the problem:
Given a string, return a version without the first 2 chars.
This is never to be found in your code
Except keep the first char if it is 'a'This is in your code, but with the exception flaw
and keep the second char if it is 'b'.This code is only halfway implemented, and doesn't work in conjunction with previous line (negating it, therefore making the whole code flawed), and also have the exception flaw.
So, the problem required you to work out every line in unison, and you have two seperate if statements, so it will never work if you keep coding that way.
ASKER
You used fixed lengths (substring(x,y), so it means it will throw errors if you don't check for the correct length and you input something out of bounds.i thought i fixed this issue as below
public String deFront(String str) {
String st=str;
if(str.length()>=2){
if(st.substring(0,1).equals("a"))
{
st=st;
}
else
{
st=st.substring(1);
}
if(st.substring(1,2).equals("b"))
{
st=st;
}
else
{
st=st.substring(1);
}
}
return st;
}
still get index out of bound for
eFront("ba") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:18)" X
please advise
ASKER
and you have two seperate if statementsi thought i covered that?
please advise
You really shouldn't start coding first. You should structure the logic first. You can tweak as many small things as you want, but the logic to begin with is failing. Write down the basics first, then build on it, and when it looks good, finally put it in code.
Your last piece of code already starts off with handling ONLY if the string is 2 or more. The original problem already stated it could be ANY length.
So write down the logic in normal short language first.
Your last piece of code already starts off with handling ONLY if the string is 2 or more. The original problem already stated it could be ANY length.
So write down the logic in normal short language first.
ASKER
sure
I agree with kimputer that you need to begin with the logic first before you start to code. When you do start to code, do it one "baby" step at a time. The basic logic should tell you that there are three parts to the result, the first character, the second character, and the rest of the characters in the string (if they exist). In your first attempt at the code, you show
Expected Run
deFront("Hello") → "llo" "elo" X ==> Why would you test any further knowing this part is wrong? The first character is not "a" and the second character is not "b" and the remaining characters are "llo" so get this to work first before you try another test.
Expected Run
deFront("Hello") → "llo" "elo" X ==> Why would you test any further knowing this part is wrong? The first character is not "a" and the second character is not "b" and the remaining characters are "llo" so get this to work first before you try another test.
ASKER
deFront("Hello") → "llo" "elo" X ==> Why would you test any further knowing this part is wrong? The first character is not "a" and the second character is not "b" and the remaining characters are "llo" so get this to work first before you try another test.
i think i passed that as below
public String deFront(String str) {
String st=str;
if(str.length()>=2){
if(st.substring(0,1).equals("a"))
{
st=st;
}
else
{
st=st.substring(1);
}
if(st.substring(1,2).equals("b"))
{
st=st;
}
else
{
st=st.substring(1);
}
}
return st;
}
still failing some tests.
Expected Run
deFront("Hello") → "llo" "llo" OK
deFront("java") → "va" "va" OK
deFront("away") → "aay" "way" X
deFront("axy") → "ay" "xy" X
deFront("abc") → "abc" "abc" OK
deFront("xby") → "by" "y" X
deFront("ab") → "ab" "ab" OK
deFront("ax") → "a" "x" X
deFront("axb") → "ab" "xb" X
deFront("aaa") → "aa" "aa" OK
deFront("xbc") → "bc" "c" X
deFront("bbb") → "bb" "bb" OK
deFront("bazz") → "zz" "zz" OK
deFront("ba") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:18)" X
deFront("abxyz") → "abxyz" "abxyz" OK
deFront("hi") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:18)" X
deFront("his") → "s" "s" OK
deFront("xz") → "" "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:18)" X
deFront("zzz") → "z" "z" OK
other tests
I need to check further based on above posts
You're still continuing on the wrong path, instead of listening to other advice.
I'll repeat that the challenge involves three parts of a string, the first character, the second character, and the rest of the characters if they exist. How would you assign values to each of these parts? Once you figure that out, you can then return the concatenation of the three parts as one string.
ASKER
the first character,
the second character,
and the rest of the characters if they exist. How would you assign values to each of these
parts? Once you figure that out,
public String deFront(String str) {
String st = null;
int strLen=str.length();
String st1=str.substring(0,1);
String st2=str.substring(1,2);
String st3=str.substring(2);
if(strLen<=2){
st=str;
}
if(st1.equals("a")&& !st2.equals("b"))
{
st=st1+st3;
}
if(!st1.equals("a")&& st2.equals("b"))
{
st=st2+st3;
}
if(st1.equals("a")&& st2.equals("b"))
{
st=st3;
}
if(!st1.equals("a")&& !st2.equals("b"))
{
st=st3;
}
return st;
}
i think i am following what you are mentioning .
public class Test48 {
/*I'll repeat that the challenge involves three parts of a string,
the first character,
the second character,
and the rest of the characters if they exist. How would you assign values to each of these
parts? Once you figure that out,
you can then return the concatenation of the three parts as one string.
Given a string, return a version without the first 2 chars. Except keep the first char if it is
'a' and keep the second char if it is 'b'. The string may be any length. Harder than it looks.*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String output=deFront("abllo");
System.out.println("output is-->"+output);
}
public static String deFront(String str) {
String st = null;
int strLen=str.length();
String st1=str.substring(0,1);
String st2=str.substring(1,2);
String st3=str.substring(2);
System.out.println("st1 is"+st1);
System.out.println("st2 is"+st2);
System.out.println("st3 is"+st3);
if(strLen<=2){
st=str;
}
if(st1.equals("a")&& !st2.equals("b"))
{
st=st1+st3;
}
if(!st1.equals("a")&& st2.equals("b"))
{
st=st2+st3;
}
if(st1.equals("a")&& st2.equals("b"))
{
st=st3;
}
/*
if(str.length()>=2){
if(st.substring(0,1).equals("a"))
{
st=st;
}
else
{
st=st.substring(1);
}
if(st.substring(1,2).equals("b"))
{
st=st;
}
else
{
st=st.substring(1);
}
}*/
if(!st1.equals("a")&& !st2.equals("b"))
{
st=st3;
}
return st;
}
}
i am failing in two test cases. please advise
ASKER
public String deFront(String str) {
String st = null;
int strLen=str.length();
String st1=str.substring(0,1);
String st2=str.substring(1,2);
String st3=str.substring(2);
if(strLen>2){
if(st1.equals("a")&& !st2.equals("b"))
{
st=st1+st3;
}
if(!st1.equals("a")&& st2.equals("b"))
{
st=st2+st3;
}
if(st1.equals("a")&& st2.equals("b"))
{
st=st1+st2+st3;
}
if(!st1.equals("a")&& !st2.equals("b"))
{
st=st3;
}
}
if(strLen<=2){
if(st1.equals("a")&& !st2.equals("b"))
{
st=st1+st3;
}
if(!st1.equals("a")&& st2.equals("b"))
{
st=st2+st3;
}
if(st1.equals("a")&& st2.equals("b"))
{
st=st1+st2;
}
if(!st1.equals("a")&& !st2.equals("b"))
{
st="";
}
}
return st;
}
As above i paaed all tests. I am thinking i am following above suggestions. Please correct me where i am wrong and also suggest me on how to improve my code?
ASKER
my eclipse code is
public class Test48 {
/*I'll repeat that the challenge involves three parts of a string,
the first character,
the second character,
and the rest of the characters if they exist. How would you assign values to each of these
parts? Once you figure that out,
you can then return the concatenation of the three parts as one string.
Given a string, return a version without the first 2 chars. Except keep the first char if it is
'a' and keep the second char if it is 'b'. The string may be any length. Harder than it looks.*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String output=deFront("xyllo");
System.out.println("output is-->"+output);
}
public static String deFront(String str) {
String st = null;
int strLen=str.length();
String st1=str.substring(0,1);
String st2=str.substring(1,2);
String st3=str.substring(2);
if(strLen>2){
if(st1.equals("a")&& !st2.equals("b"))
{
st=st1+st3;
}
if(!st1.equals("a")&& st2.equals("b"))
{
st=st2+st3;
}
if(st1.equals("a")&& st2.equals("b"))
{
st=st1+st2+st3;
}
if(!st1.equals("a")&& !st2.equals("b"))
{
st=st3;
}
}
if(strLen<=2){
if(st1.equals("a")&& !st2.equals("b"))
{
st=st1+st3;
}
if(!st1.equals("a")&& st2.equals("b"))
{
st=st2+st3;
}
if(st1.equals("a")&& st2.equals("b"))
{
st=st1+st2;
}
if(!st1.equals("a")&& !st2.equals("b"))
{
st="";
}
}
return st;
}
}
You've already been introduced to the ternary operator which is kind of a shortcut (your words) for if-then-else. Since you have three parts to your challenge, each of which can be determined by if-then-else why not just -
public static string deFront(String str) {
firstChar = str.substring(0,1).equals( "a") ? "a" : "";
secondChar = str.substring(1,2).equals( "b") ? "b" : "";
rest = str.length() <= 2? "" ; str.substring(2);
return firstChar + secondChar + rest;
}
public static string deFront(String str) {
firstChar = str.substring(0,1).equals(
secondChar = str.substring(1,2).equals(
rest = str.length() <= 2? "" ; str.substring(2);
return firstChar + secondChar + rest;
}
ASKER
that pased all.
so 2 lessons for me
1. Alway break (challenge to sub units of 2 or more) and conquer(accordingly write code for each sub unit/part then concatenate /merge/join)
2. wheneve if else if comes to mind use shortcut operator.
so 2 lessons for me
1. Alway break (challenge to sub units of 2 or more) and conquer(accordingly write code for each sub unit/part then concatenate /merge/join)
2. wheneve if else if comes to mind use shortcut operator.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
not shortcut"short-circuit" in Java, C, et al., usually refers to the &&/|| operators skipping evaluation of their second operand when their first operand is false/true
"short-hand" (not to be confused with "shorthanded") usually refers to a shorter method of writing something.
"shortcut" usually refers to a shorter, quicker, or easier way to get to a place
Which may tend to get confused with either of the above concepts.