gudii9
asked on
gHappy challenge
Hi,
[
I am working on below challenge
http://codingbat.com/prob/p198664
I am not clear on how to approach. please advise
[
I am working on below challenge
http://codingbat.com/prob/p198664
I am not clear on how to approach. please advise
Haven't you asked this question before?
We won't (and are not allowed to) write the code for you.
At least come up with some code you wrote yourself.
Starting from that we can (try to) help you further.
At least come up with some code you wrote yourself.
Starting from that we can (try to) help you further.
Unbelievable. I don't think you should be showering the Java TA with questions in this way.
ASKER
public class gHappy {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* We'll say that a lowercase 'g' in a string is "happy" if there is
* another 'g' immediately to its left or right. Return true if all the
* g's in the given string are happy.
*
* gHappy("xxggxx") → true gHappy("xxgxx") → false gHappy("xxggyygxx") →
* false
*/
System.out.println("i is-->" + gHappy("xxggxx"));
System.out.println("i2 is-->" + gHappy("xxgxx"));
}
public static boolean gHappy(String str) {
for (int i = 0; i < str.length()-1; i++) {
if (str.charAt(i) == str.charAt(i+1))
return true;
}/*else {
return false;
}*/
return false;
}
}
i am getting as below true true both times
i is-->true
i2 is-->true
i have not done this 3rd level complex ones in coding bat
please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
First, tell us what scenario you have in mind to accomplish the challenge.
Not in java. In words.
Not in java. In words.
ASKER
i am thinking to check where is g position. once we know that check before and after char if equal return true.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
let me think
ASKER
public boolean gHappy(String str) {
for(int i=0;i<=str.length();i++){
if(str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return false;
}
above pasing few failing few more'
Expected Run
gHappy("xxggxx") → true true OK
gHappy("xxgxx") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 5 (line number:3) X
gHappy("xxggyygxx") → false true X
gHappy("g") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: -1 (line number:3) X
gHappy("gg") → true Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: -1 (line number:3) X
gHappy("") → true Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 0 (line number:3) X
gHappy("xxgggxyz") → true true OK
gHappy("xxgggxyg") → false true X
gHappy("xxgggxygg") → true true OK
gHappy("mgm") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 3 (line number:3) X
gHappy("mggm") → true true OK
gHappy("yyygggxyy") → true true OK
other tests
X
Your progress graph for this problem
I didn't ask for code. I asked for the complete scenario in words.
First think, then code.
PS. The challenge description states:
Return true if all the g's in the given string are happy.
First think, then code.
PS. The challenge description states:
Return true if all the g's in the given string are happy.
ASKER
First think, then code.sure
complete scenario
We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.
gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false
1. check lower case g in given string
2. make sure that g is having other g on left or right
3. if yes return true ie happy g
4.if no return false ie unhappy g
ASKER
i wonder why i keep getting string index bound lot of times.
i thought i followed same steps here not sure what i am missing
please advise
i thought i followed same steps here not sure what i am missing
please advise
public boolean gHappy(String str) {
for(int i=0;i<=str.length();i++){
if(str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return false;
}
So what's going to happen when i is zero (as it is at the start)?(str.charAt(i-1)=='g'
ASKER
public boolean gHappy(String str) {
for(int i=0;i<=str.length()-1;i++){
if(str.length()==2 && str.charAt(1)=='g' && str.charAt(0)=='g') return true;
else if(str.length()>2 && str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return false;
}
now i am failing only 3 tests with edge cases
Expected Run
gHappy("xxggxx") → true true OK
gHappy("xxgxx") → false false OK
gHappy("xxggyygxx") → false true X
gHappy("g") → false false OK
gHappy("gg") → true true OK
gHappy("") → true false X
gHappy("xxgggxyz") → true true OK
gHappy("xxgggxyg") → false true X
gHappy("xxgggxygg") → true true OK
gHappy("mgm") → false false OK
gHappy("mggm") → true true OK
gHappy("yyygggxyy") → true true OK
other tests
X
Correct for more than half the tests
ASKER
public boolean gHappy(String str) {
for(int i=0;i<=str.length()-1;i++){
if(str.length()==0) return true;
else if(str.length()==2 && str.charAt(1)=='g' && str.charAt(0)=='g') return true;
else if(str.length()>2 && str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return false;
}
i wrote as above still failing by adding if(str.length()==0) return true;i though "" means str.length(P) is zero. please advise
gHappy("") → true false X
If you search this site for gHappy you'll find a discussion about the empty string scenario and in the same question is an implementation, which, if it works, could be seen as a reference implementation
ASKER
public boolean gHappy(String str) {
boolean result=false;
for(int i=0;i<=str.length()-1;i++){
if(str.length()==0) result=true;
else if(str.length()==2 && str.charAt(i+1)=='g' && str.charAt(i)=='g') result=true;
else if(str.length()>2 && str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) result=true;
}
return result;
}
above fixed some failing some
xpected Run
gHappy("xxggxx") → true true OK
gHappy("xxgxx") → false false OK
gHappy("xxggyygxx") → false true X
gHappy("g") → false false OK
gHappy("gg") → true Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:5) X
gHappy("") → true false X
gHappy("xxgggxyz") → true true OK
gHappy("xxgggxyg") → false true X
gHappy("xxgggxygg") → true true OK
gHappy("mgm") → false false OK
gHappy("mggm") → true true OK
gHappy("yyygggxyy") → true true OK
other tests
X
Correct for more than half the tests
Personally i wouldn't view programming as an additive process. If you are failing some tests it doesn't mean that you should tweak things by trial and error until it works. It means you have the wrong approach
ASKER
what is additive process.
when some tests fail i try to fix some other fails. it is like chasing rats from open box which run all over the place
when some tests fail i try to fix some other fails. it is like chasing rats from open box which run all over the place
what is additive process.You answered your own question ;)
ASKER
i am missing all g part let me see what i caan do all g scenario
ASKER
gHappy("")
what would be length of above string?
is it not 0? please advise
i wonder why i keep getting string index bound lot of times.I already told you in one of your other questions.
Which proves that you complete a challenge without learning/remembering anything.
You should seriously ask yourself if your learning process is ok.
i am missing all g partIndeed you are.
what would be length of above string?The length of an empty string is 0.
If you doubt that, I think you'll need to restart learning java.
ASKER
what would be length of above string?
The length of an empty string is 0.
If you doubt that, I think you'll need to restart learning java.
my challenge is failing some reason even i put that if condition?
ASKER
We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.
gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false
check if first letter g if yes check next letter g if yes increment gCount by one repeate till end
my story telling fr this movie challenge is above. let me code now
ASKER
public boolean gHappy(String str) {
for(int i=0;i<=str.length()-1;i++){
/* if(str.length()>2 &&(str.charAt(1)=='g'&&(str.charAt(0))) return true;
else if(str.length()>2 &&((str.charAt(i)=='g')&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g'))) {
return true;
}
}
return false;
*/
if(str.charAt(i)=='g' && (str.charAt(i+1)=='g'||str.charAt(i-1)=='g')) return true;
//else if(str.length()>2 && str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return false;
}
failing 4
xpected Run
gHappy("xxggxx") → true true OK
gHappy("xxgxx") → false false OK
gHappy("xxggyygxx") → false true X
gHappy("g") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 1 (line number:13) X
gHappy("gg") → true true OK
gHappy("") → true false X
gHappy("xxgggxyz") → true true OK
gHappy("xxgggxyg") → false true X
gHappy("xxgggxygg") → true true OK
gHappy("mgm") → false false OK
gHappy("mggm") → true true OK
gHappy("yyygggxyy") → true true OK
other tests
X
how to take care of for all g case?
please advise
ASKER
do i need to use continue ?
above also fails below where there are more than one occurence f g at different spots
public boolean gHappy(String str) {
boolean result=false;
for(int i=0;i<=str.length()-1;i++){
/* if(str.length()>2 &&(str.charAt(1)=='g'&&(str.charAt(0))) return true;
else if(str.length()>2 &&((str.charAt(i)=='g')&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g'))) {
return true;
}
}
return false;
*/
if(str.charAt(i)=='g' && (str.charAt(i+1)=='g'||str.charAt(i-1)=='g')){
result=true;
continue;
}
//else if(str.length()>2 && str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return result;
}
above also fails below where there are more than one occurence f g at different spots
Expected Run
gHappy("xxggxx") → true true OK
gHappy("xxgxx") → false false OK
gHappy("xxggyygxx") → false true X
gHappy("g") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 1 (line number:14) X
gHappy("gg") → true Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 2 (line number:14) X
gHappy("") → true false X
gHappy("xxgggxyz") → true true OK
gHappy("xxgggxyg") → false Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 8 (line number:14) X
gHappy("xxgggxygg") → true Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 9 (line number:14) X
gHappy("mgm") → false false OK
gHappy("mggm") → true true OK
gHappy("yyygggxyy") → true true OK
ASKER
import java.util.LinkedList;
public class Ghappy {
public static void main(String[] args) {
System.out.println("valuer is-->"+gHappy("xxggyygxx"));
}
public static boolean gHappy(String str) {
boolean result=false;
for(int i=0;i<=str.length()-1;i++){
/* if(str.length()>2 &&(str.charAt(1)=='g'&&(str.charAt(0))) return true;
else if(str.length()>2 &&((str.charAt(i)=='g')&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g'))) {
return true;
}
}
return false;
*/
if(str.charAt(i)=='g' && (str.charAt(i+1)=='g'||str.charAt(i-1)=='g') ){
{
if(str.charAt(i+1)=='g'&&(str.substring(i+1,str.length()).contains("gg")) )result=true;
// continue;
}
}
//else if(str.length()>2 && str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) return true;
}
return result;
}
/*public boolean gHappy(String str) {
for(int i=0;i<=str.length()-1;i++){
if(str.length()==2&&str.charAt(1)=='g'&&(str.charAt(0))) return true;
else if(str.length()>2 &&((str.charAt(i)=='g')&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g'))) {
return true;
}
}
return false;
}
*/
}
[/code
i tried as above no luck yet
[code]xpected Run
gHappy("xxggxx") → true false X
gHappy("xxgxx") → false false OK
gHappy("xxggyygxx") → false false OK
gHappy("g") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (line number:14) X
gHappy("gg") → true Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:14) X
gHappy("") → true false X
gHappy("xxgggxyz") → true true OK
gHappy("xxgggxyg") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 8 (line number:14) X
gHappy("xxgggxygg") → true Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 9 (line number:14) X
gHappy("mgm") → false false OK
gHappy("mggm") → true false X
gHappy("yyygggxyy") → true true OK
other tests
how to make all g happy so that i will be happy. please advise
[1]
If you would listen to what we tell you and try to complete this scenario in words, than the coding would be easy.
[2]
I no longer look at badly indented code. Sorry. I asked you several times to pay attention to that.
You know you can edit your posts, right?
If you would listen to what we tell you and try to complete this scenario in words, than the coding would be easy.
Example (start of what I expect you to tell me):
* Initialize a variable named 'happy' to false
* Loop over each and every character in the string
* if the character is something different from 'g' continue with the next character
* if the character is a 'g' ... (up to you to continue)
* ...
* return the variable 'happy'
[2]
I no longer look at badly indented code. Sorry. I asked you several times to pay attention to that.
You know you can edit your posts, right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
if(str.charAt(i)=='g'&&!((str.charAt (Math.abs( i+1))=='g' ||str.char At(Math.ab s(i-1))==' g'))){happ y = false;}
you passed all tests with above magic line.
you are checking character at i is 'g' and (char at i+1 or i-1 not g) then returning false
all other cases returning true?
why did you use Math.abs() method?
Example (start of what I expect you to tell me):above you have not talked about 'all g' scenario right which i was struggling to address and write and code.
* Initialize a variable named 'happy' to false
* Loop over each and every character in the string
* if the character is something different from 'g' continue with the next character
* if the character is a 'g' ... (up to you to continue)
* ...
* return the variable 'happy'
one time gg comes first time i passed all tests
if string is like abcggdefghi
i am still getting true insteead i have to get false
ASKER
i was thinking true scenario? How in the world you thought
about false scenario first and then true scenario? I never got that thought? please advise on whne we should think false scenario first then true scenario for logic building
about false scenario first and then true scenario? I never got that thought? please advise on whne we should think false scenario first then true scenario for logic building
You replied to my post with 2 posts of your own within minutes of mine. This shows me you haven't given sufficient thought either to what I posted, nor to what you should learn from it. So my proposal is : you put your trigger finger in a splint for a while to give yourself more time to think about each line of code or comment that you see we've bothered posting to you, and try to a) understand the constituent parts of the comments or code (i.e. the lines of code), and then b), see if you can manage to link them all together and arrive at some understanding of the whole.
You should stop bashing-out your responses in a sort of stream-of-consciousness fashion, (this is not a race where you are being judged by how quickly you can be seen to respond to what we've told you, for fear of losing some kind of credibility as a cybergeek or something), and you type-away without regard to the keystrokes you are making, running sentences together, asking bits of new and old questions all at the same time, mixing them between Experts, and regurgitating your own ideas and rebuttals of what we tell you, all wrapped-up in a fog of basic misunderstanding of the very basics of computer programming.
In short, you are helping confuse and lengthen your own learning process and irritate Experts here in all sorts of ways. It would be good if you recognised this and did something about it immediately. There is NO POINT AT ALL in you asking a question here, getting help from the likes of zzynx and CEHJ, and then often going off to some other website and coming back here with a load of quoted code and comment that we've never seen before, and, quite frankly, don't want to see, and don't need. Take a look at those guys' back histories of answered questions - if that doesn't convince you they deserve their points and deserve to be listened to vertically here, IN CONTEXT, then . . . I'm afraid you will possibly never get to where you want to be.
You should stop bashing-out your responses in a sort of stream-of-consciousness fashion, (this is not a race where you are being judged by how quickly you can be seen to respond to what we've told you, for fear of losing some kind of credibility as a cybergeek or something), and you type-away without regard to the keystrokes you are making, running sentences together, asking bits of new and old questions all at the same time, mixing them between Experts, and regurgitating your own ideas and rebuttals of what we tell you, all wrapped-up in a fog of basic misunderstanding of the very basics of computer programming.
In short, you are helping confuse and lengthen your own learning process and irritate Experts here in all sorts of ways. It would be good if you recognised this and did something about it immediately. There is NO POINT AT ALL in you asking a question here, getting help from the likes of zzynx and CEHJ, and then often going off to some other website and coming back here with a load of quoted code and comment that we've never seen before, and, quite frankly, don't want to see, and don't need. Take a look at those guys' back histories of answered questions - if that doesn't convince you they deserve their points and deserve to be listened to vertically here, IN CONTEXT, then . . . I'm afraid you will possibly never get to where you want to be.
you passed all tests with above magic line.
you are checking character at i is 'g' and (char at i+1 or i-1 not g) then returning false
all other cases returning true?
why did you use Math.abs() method?
1) There is no magic going on. This is a computer we are feeding here.
2) Math.abs(). Why don't you try to answer that yourself ? Is it even correct? Is it necessary ?? If so, why ? How *could* or *might* it relate to what one of the other Experts mentioned to you already ? And what can you learn from that comparison ?
As for your other comment about true first or second - this is part of the algorithmic thinking that should come from having understood the question properly in the first place - you *know* (or at least you have been TOLD) that any single 'g' which has no adjacent other 'g' is not happy . . . so that constraint on happiness only needs to be satisfied ONCE somewhere in the string for it to be time to return false (unhappy). You should GET that before you even TOUCH your IDE.
ASKER
that any single 'g' which has no adjacent other 'g' is not happy . . . so that constraint on happiness only needs to be satisfied ONCE somewhere in the string for it to be time to return false (unhappy). You should GET that before you even TOUCH your IDE.i got it now.
str+"|";
why we are using pipe | here?
i debugged and saw false happening for below bolded g and all rest of characters in xxggyygxx it is true
xxggyygxx
import java.util.LinkedList;
public class Ghappy {
public static void main(String[] args) {
System.out.println("valuer is-->" + gHappy("xxggyygxx"));
}
public static boolean gHappy(String str) {
boolean happy = true;
str = str + "|";
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == 'g' && !((str.charAt(Math.abs(i + 1)) == 'g' || str.charAt(Math.abs(i - 1)) == 'g'))) {
happy = false;
}
}
return happy;
}
/*
* public static boolean gHappy(String str) { boolean result = false; for
* (int i = 0; i <= str.length() - 1; i++) {
*
* if(str.length()>2 &&(str.charAt(1)=='g'&&(str.charAt(0))) return true;
*
* else if(str.length()>2
* &&((str.charAt(i)=='g')&&(str.charAt(i-1)=='g'||str.charAt(i-1)== 'g')))
* { return true; } } return false;
*
*
*
* if (str.charAt(i) == 'g' && (str.charAt(i + 1) == 'g' || str.charAt(i -
* 1) == 'g')) { { if (str.charAt(i + 1) == 'g' && (str.substring(i + 1,
* str.length()).contains("gg"))) result = true;
*
*
*
*
*
*
*
* // else if(str.length()>2 && //
* str.charAt(i)=='g'&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g')) //
* return true;
*
* // continue; }
*
* }
*
* } return result; }
*/
/*
* public boolean gHappy(String str) { for(int i=0;i<=str.length()-1;i++){
* if(str.length()==2&&str.charAt(1)=='g'&&(str.charAt(0))) return true;
*
* else if(str.length()>2
* &&((str.charAt(i)=='g')&&(str.charAt(i-1)=='g'||str.charAt(i-1)=='g'))) {
* return true; } } return false;
*
* }
*/
}
why we are using pipe | here?Same answer as krakatoa's: Why don't you try to answer that yourself?
Hint: you can replace that "|" by "#" or "@" or every other character that is not a 'g'
Try to really understand each bit of that code.
You could do that by debugging that piece of code and let it run step by step for the bunch of string you have to check the happiness of.
ASKER
if i remove Math.abs below test failing
gHappy("g") →
gHappy("g") →
ASKER
public boolean gHappy(String str) {
boolean happy = true;
str = str+"Q";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(Math.abs(i+1))=='g'||str.charAt(Math.abs(i-1))=='g'))){happy = false;}
}
return happy;
}
works with any string other than g
ASKER
including one space as below
does not work it is below.
i am confused between differenece between "" and " "
public boolean gHappy(String str) {
boolean happy = true;
str = str+" ";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(Math.abs(i+1))=='g'||str.charAt(Math.abs(i-1))=='g'))){happy = false;}
}
return happy;
}
does not work it is below.
public boolean gHappy(String str) {
boolean happy = true;
str = str+"";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(Math.abs(i+1))=='g'||str.charAt(Math.abs(i-1))=='g'))){happy = false;}
}
return happy;
}
why . please advise.i am confused between differenece between "" and " "
works with any string other than g
. . . well of course it does - that's the point! If you added another 'g' you'd be back to square one and have to remove it again!
i am confused between differenece between "" and " "
. . . well, as you've been told before, "" is not a String - how can it be there is nothing in it! When you typed "" into the keyboard, DID you type anything between the first " and the second " ? Of course you didn't, so how can there be a String in it ???
But " " IS a String of course, because it has a character in it, namely the space character!
why . please advise.You really don't get it, do you?
I honestly think you should first thoroughly study java and then come here again.
It's like asking how you would say something in Chinese. We ask you to write it down yourself, but you're not able. Then we're giving you the Chinese translation. But you can't read it. That's not the way to learn a language. That's not the way to learn Java.
At the beginning of processing the given string, we add one character (different from 'g') at the end of the given string just to avoid a StringIndexOutOfBoundsExce
str.charAt(i+1)
Remark:
1) since your loop starts at i=0, here you can remove the Math.abs(), but you can't for i-1.
I hope you understand why. If not, it's hopeless...
2) I don't agree with krakatoa when he says that String str = "" is not a string.
When you write
String str = "";
the variable str does contain a String: the empty string. Agree, a string containing no characters. But it's a string.When you write
String str = " ";
the variable str does contain a String, containing one character: a space.When you write
String str = null;
the variable str does NOT contain a String.
I don't agree with krakatoa when he says that String str = "" is not a string.
ok technically speaking you are correct.
"" has no length was my point to gudii9, as there seems to be massive confusion in his mind about why the results are as they are, which, in this case, would be because adding "" to the str variable will NOT add a character, and therefore makes the code I posted fail. Obviously (to us) if he added " " instead, then it would not fail. That *effectively* makes "" behave not like a String for these purposes, and so the nuance of "" being a String but not consisting of anything is a refinement that is not going to help much here.
if i remove Math.abs below test failing
zynxx has given you the reason for this behaviour. One of those Math.abs() calls is redundant, as he explained. Putting it in there was my attempt at getting you to finally understand the way arrays and strings are enumerated in terms of their indices, starting at zero - which is a concept that you keep returning to in your questions without fully ever understanding it properly it seems to me. That's not to say it's intuitive, understanding that offsets start with zero, but you need to cement that in your brain once and for all, in order to go into second gear and put it to use in loops and methods which rely on a proper implementation of it.
ASKER
zynxx has given you the reason for this behaviour. One of those Math.abs() calls is redundant, as he explained. Putting it in there was my attempt at getting you to finally understand the way arrays and strings are enumerated in terms of their indices, starting at zero - which is a concept that you keep returning to in your questions without fully ever understanding it properly it seems to me. That's not to say it's intuitive, understanding that offsets start with zero, but you need to cement that in your brain once and for all, in order to go into second gear and put it to use in loops and methods which rely on a proper implementation of it.
i understand it. index always starts with 0 and when we say i-1 it goes to -1 which we do not want.
so using Math.abs() function here
ASKER
gHappy("") → true
how this case pssign
public boolean gHappy(String str) {
boolean happy = true;
str = str+"@";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt (Math.abs( i+1))=='g' ||str.char At(Math.ab s(i-1))==' g'))){happ y = false;}
}
return happy;
}
in c=above code we are saying if one charaater is g and before and after not g return unhappy
otherwise hapy
?
so str is empty string or without any g how above code works?
ASKER
i see your point.
onnly if a character is g and adjacent character not g you are saying return unhappy
all other cases like
1.if a character is g and adjacent character is a 'g'
2. if given string(str) has no g
3. if given string str is blank then return happy
onnly if a character is g and adjacent character not g you are saying return unhappy
all other cases like
1.if a character is g and adjacent character is a 'g'
2. if given string(str) has no g
3. if given string str is blank then return happy
Your landing gear seems finally to have been deployed. See if you can now make a smooth touch-down, and annotate each line of code with its plain English logical equivalent.
if i remove Math.abs below test failing
gHappy("g") →
And you didn't say *which* Math.abs() call is redundant - so which one is it? More importantly, WHY?
ASKER
as we are starting with i=0
Math.abs(i+1)===>redundant as i+1 always is positive
Math.abs(i-1)===>needed as i-1 can go negative. so we need Math.abs() so that it will be positive always eventhough actual value is negative
above is passing all tests
Math.abs(i+1)===>redundant
Math.abs(i-1)===>needed as i-1 can go negative. so we need Math.abs() so that it will be positive always eventhough actual value is negative
public boolean gHappy(String str) {
boolean happy = true;
str = str+"@";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(i+1)=='g'||str.charAt(Math.abs(i-1))=='g'))){happy = false;}
}
return happy;
}
above is passing all tests
ASKER
Your landing gear seems finally to have been deployed. See if you can now make a smooth touch-down, and annotate each line of code with its plain English logical equivalent.
i want smooth travel and touch down all the time like you experts. Smooth, easy, unruffled ride of java coding jouney
Right, well, you've dealt with the Math.abs() issue correctly. Tick that box.
You also understand why we can set the happy boolean to true up front. Tick that box too.
You also said you get the 0 index-ing CS paradigm, so another box ticked.
Now you can see why I said earlier that another Expert had already asked you the index question :
here.
If you go back over your previous comments and questions, hopefully you will be able to now see what the answers to them are, and it is this 'recursive' process of jigsaw fitting what you know now, with what you didn't know before, that should help you most of all.
You also understand why we can set the happy boolean to true up front. Tick that box too.
You also said you get the 0 index-ing CS paradigm, so another box ticked.
Now you can see why I said earlier that another Expert had already asked you the index question :
here.
If you go back over your previous comments and questions, hopefully you will be able to now see what the answers to them are, and it is this 'recursive' process of jigsaw fitting what you know now, with what you didn't know before, that should help you most of all.
The addition by me of an extra character at the end of each test string, is what they call, in the patent world, an 'inventive step'. It - artificially here - defeats the conundrum of what happens when there is a 'g' right at the end, for example.
Now, whether my 'innovation' (of lengthening the string) is truly acceptable or not in a CS sense, is another question entirely, and I am not sure that I even agree with the principle of my approach in such "interfering". Perhaps one of the others will comment on that if they feel so inclined. I think it might cause problems in other more complicated scenarios.
Now, whether my 'innovation' (of lengthening the string) is truly acceptable or not in a CS sense, is another question entirely, and I am not sure that I even agree with the principle of my approach in such "interfering". Perhaps one of the others will comment on that if they feel so inclined. I think it might cause problems in other more complicated scenarios.
Here's another way to do it. I'm not sure I like this completely either.
public boolean gHappy(String str) {
boolean happy = true;
if(str.length()==0){return happy;}
if(str.equals("g")||str.charAt(str.length()-1)=='g'&&!(str.charAt(str.length()-2)=='g')){return false;}
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(i+1)=='g'||str.charAt(i-1)=='g'))){happy = false;}
}
return happy;
}
ASKER
public boolean gHappy(String str) {
boolean happy = true;
if(str.length()==0){return happy;}
if(str.equals("g")||str.charAt(str.length()-1)=='g'&&!(str.charAt(str.length()-2)=='g')){return false;}
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(i+1)=='g'||str.charAt(i-1)=='g'))){happy = false;}
}
return happy;
}
not able to understand above completely?below clear saying retun happy true as string length is 0 ie ""
if(str.length()==0){return
as below returning happy as false if only one g is there or g is there at last position of string and penultimate position is not g..which kind of make sense
if(str.equals("g")||str.ch
as below you are loopign in for loop see character at i is g and following and preceding character not g then false..
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((
rest all cases true??
please advise
Look . . . back here you were passing all tests - so I assume you understood what was going on . . . . . . . . . ?
So why is it now apparently MORE difficult to understand a somewhat easier piece of code ??
You seem to be starting from scratch again with the same doubts and questions (about loops) . . . what is going on?
So why is it now apparently MORE difficult to understand a somewhat easier piece of code ??
You seem to be starting from scratch again with the same doubts and questions (about loops) . . . what is going on?
ASKER
old code and new code are slightly different
both pass all the tests. I am trying to understand new code also closely
old code
public boolean gHappy(String str) {
boolean happy = true;
str = str+"@";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(i+1)=='g'||str.charAt(Math.abs(i-1))=='g'))){happy = false;}
}
return happy;
}
new code
public boolean gHappy(String str) {
boolean happy = true;
if(str.length()==0){return happy;}
if(str.equals("g")||str.charAt(str.length()-1)=='g'&&!(str.charAt(str.length()-2)=='g')){return false;}
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(i+1)=='g'||str.charAt(i-1)=='g'))){happy = false;}
}
return happy;
}
both pass all the tests. I am trying to understand new code also closely
ASKER
new code there are 2 additional if blocks and also it does not have like below code
str = str+"@";
str = str+"@";
ASKER
public boolean gHappy(String str) {
str = "X" + str + "X";
for (int i = 1; i < str.length() - 1; i++)
if (str.charAt(i) == 'g' && str.charAt(i - 1) != 'g'
&& str.charAt(i + 1) != 'g')
return false;
return true;
}
above also passes all tests
If three pieces of code all three pass the tests, that means that it are three correct different ways to do the same thing. It's a good exercise for you to (try to) understand them all three.
If that last piece of code is your work then you just made a giant leap for gudiikind.
ASKER
if(str.equals("g")||str.charAt(str.length()-1)=='g'&&!(str.charAt(str.length()-2)=='g')){return false;}
above check string is just g or string whose last character is g but not penultimate charactger not g then return false(as g is alone withut other g next to it) right?
ASKER
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt(i+1)=='g'||str.charAt(i-1)=='g'))){happy = false;}
}
above also checks if character at i is g and also one character before and after is also not g then also false
all other case just return true?
ASKER
public boolean gHappy(String str) {
boolean happy = true;
if(str.length()==0){returnhappy;}
if(str.equals("g")||str.charAt(str.l ength()-1) =='g'&&!(s tr.charAt( str.length ()-2)=='g' )){return false;}
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt (i+1)=='g' ||str.charAt(i-1)=='g'))){ha ppy = false;}
}
return happy;
}
instead of || if i use && failing some tests
public boolean gHappy(String str) {
boolean happy = true;
if(str.length()==0){returnhappy;}
if(str.equals("g")||str.charAt(str.l ength()-1) =='g'&&!(s tr.charAt( str.length ()-2)=='g' )){return false;}
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='g'&&!((str.charAt (i+1)=='g' &&str.charAt(i-1)=='g'))){ha ppy = false;}
}
return happy;
}
Expected Runplease advise why should only use || not &&
gHappy("xxggxx") → true false X
gHappy("xxgxx") → false false OK
gHappy("xxggyygxx") → false false OK
gHappy("g") → false false OK
gHappy("gg") → true Exception:java.lang.StringIndexOutOf BoundsExce ption: String index out of range: -1 (line number:10) X
gHappy("") → true true OK
gHappy("xxgggxyz") → true false X
gHappy("xxgggxyg") → false false OK
gHappy("xxgggxygg") → true false X
gHappy("mgm") → false false OK
gHappy("mggm") → true false X
gHappy("yyygggxyy") → true false X
other tests
X
La-ta-da-da-da-da-da, la-da-da-da-da-da.
I *think* I can hear sirens . . . .
I *think* I can hear sirens . . . .
ASKER
immediately to its left or right.not
immediately to its left and right.as per challenge
i got your answer
:)
OK - so I didn't test your last piece of code, I only read through it.
ARE YOU SAYING that it doesn't work, and that you want us to explain your own code to you? Or, as I asked last time, IS it ACTUALLY your code?
ARE YOU SAYING that it doesn't work, and that you want us to explain your own code to you? Or, as I asked last time, IS it ACTUALLY your code?