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

asked on

equalIsNot challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p141736


Not sure how to approach. please advise
ASKER CERTIFIED SOLUTION
Avatar of d-glitch
d-glitch
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
Avatar of gudii9

ASKER

iam not a student and this not home work. not sure why people think it is home work?
Avatar of gudii9

ASKER

can you please un delete the post
Avatar of gudii9

ASKER

for experts it may easiest for me it is toughest. Fragment helps
Is it true that you have more than 50K points in the Java Programming topic area?
This is probably a ten line program.

How would you solve these strings with a pencil and paper:
This is not
This is notnot
noisxxnotyynotxisi


Write the algorithm down in English or some other language.  Flow charts are good.  And then translate it into code.
Avatar of gudii9

ASKER

Write the algorithm down in English or some other language.  Flow charts are good.

how to do this online.

paper penceil diagram i cannot share here to cross check my thoughts
flowChart.png
Avatar of gudii9

ASKER

public boolean equalIsNot(String str) {
 
      int countIs = 0;
      int idxIs = 0;
      String subIs="is";
      while ((idxIs = str.indexOf(subIs, idxIs)) != -1) {
          countIs++;
          idxIs += subIs.length();
      }
      
      
       int countNot = 0;
      int idxNot = 0;
      String subNot="Not";
      while ((idxNot = str.indexOf(subNot, idxNot)) != -1) {
          countNot++;
          idxNot += subNot.length();
      }
      if(idxIs==idxNot)
      return true;
      else 
      return false;
      
}

Open in new window


above failed few
Expected      Run            
equalIsNot("This is not") → false      true      X      
equalIsNot("This is notnot") → true      true      OK      
equalIsNot("noisxxnotyynotxisi") → true      true      OK      
equalIsNot("noisxxnotyynotxsi") → false      true      X      
equalIsNot("xxxyyyzzzintint") → true      true      OK      
equalIsNot("") → true      true      OK      
equalIsNot("isisnotnot") → true      true      OK      
equalIsNot("isisnotno7Not") → false      true      X      
equalIsNot("isnotis") → false      true      X      
equalIsNot("mis3notpotbotis") → false      true      X      
other tests
X      
Avatar of gudii9

ASKER

public class TestStr {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("is-->" + equalIsNot("noisxxnotyynotxsi"));
	}

	public static boolean equalIsNot(String str) {

		int countIs = 0;
		int idxIs = 0;
		String subIs = "is";

		while ((idxIs = str.indexOf(subIs, idxIs)) != -1) {
			countIs++;
			idxIs += subIs.length();
		}

		int countNot = 0;
		int idxNot = 0;
		String subNot = "Not";
		while ((idxNot = str.indexOf(subNot, idxNot)) != -1) {
			countNot++;
			idxNot += subNot.length();
		}
		if (idxIs == idxNot)
			return true;
		else
			return false;

	}

}

Open in new window


giving below output

is-->true



hsould give

is-->false
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
@ dpearson - just an update

gudii9 posted eight of these Challenge questions in  seven minutes yesterday.  It isn't working any more.  He has stopped getting help.

I have looked back at some of his earlier questions.  Sometimes he does post buggy code for experts to fix, but there is generally no discussion of algorithms, efficiency, or style.  I really don't believe he is learning anything in the process.
@ gudii9  
I don't understand your flowchart or your code or your algorithm.
Just for fun, here is some commented pseudocode for an alternate, efficient algorithm:

// Go through the string once and keep track of anything interesting

TestStr = "noisxxnotyynotxsi"

recent  = ""					// Recent interesting characters
diff    = 0					// You only need one counter                     

while j < TestStr.len
	char = TestStr.charAt(j)
	if char == 'i' || 'n'  			// Initial target characters
		target = char
	if char ==  'o'				// Middle target characters
		target = target + char
	else if char == 's' 			// Looking for "is" 
		if recent == "i"
			diff += 1		// Increment for "is"
			recent = ""
	else if char == 't'			// Looking for "not"
		if recent == "no"
			diff += 1		// Decrement for "not"
			recent = ""
	else recent = ""

Open in new window

Avatar of gudii9

ASKER

i wonder what is wrong with my approach. why i am failing only for case senstive second arguments in the method?
Avatar of gudii9

ASKER

I don't understand your flowchart or your code or your algorithm.

this is one big question in my mind. How to draw a good flow diagram. For this challenge as a expert how would do draw? please advise
Avatar of gudii9

ASKER

He's definitely not doing homework and I've learned that he learns better when he starts from a good solution and asks questions about it, rather than starting from a bad solution and trying to evolve it to a good one.

one major reason is my lack of free quality time.

I am definitely learning and improving inch by inch with every challenge.
Avatar of gudii9

ASKER

i also learn lot from different approaches and different solutions and also get ideas to approach different problems different ways from all the comments
If you don't have much time, you should use it wisely.
For example, you could do one challenge at a time instead of eight.

Do you realize that there are related concepts connecting all of these challenges?

Have you given any thought to the comment in my first post?

You can approach this challenge in at least two ways:  either by crawling along the line character or by using a higher level search functions.  Which one is better?  And why?

Some of the other challenges present similar choices.  Are you thinking about any of this before you start coding?
Avatar of gudii9

ASKER

You can approach this challenge in at least two ways:  either by crawling along the line character or by using a higher level search functions.  Which one is better?  And why?

i know crawling along the line character. i am not sure on using a higher level search functions.? please advise
Some of the other challenges present similar choices.  Are you thinking about any of this before you start coding?
what are similar choices. how to identify. How to make a simple flow diagrams for these and then how to start with similar choices?
Your first code post, which used str.indexOf is an example of a higher function.

The way to make a flow chart is to perform the task with a pencil and paper slowly, and keep track (write down) your actions and decisions.  For example:
  1. Count how many times "is" appears in the string.
[list=2]Count how many times "not" appears in the string.[/list]
[list=3]Compare the two numbers.[/list]

How are you debugging your code?  Have you checked that your counts for "is" and "not" are correct?
Avatar of gudii9

ASKER

The way to make a flow chart is to perform the task with a pencil and paper slowly, and keep track (write down) your actions and decisions.  For example:
Count how many times "is" appears in the string.
[list=2]Count how many times "not" appears in the string.[/list]
[list=3]Compare the two numbers.[/list]

i will remember and try to follow this advise.
How are you debugging your code?  Have you checked that your counts for "is" and "not" are correct?
usually i use eclipse to debug which takes some time to copy paste run code etc apart from system out print statements? any other better way to debug?
Avatar of gudii9

ASKER

Your first code post, which used str.indexOf is an example of a higher function.

what is meaning of higher function? are there lower functions also?
So when your program fails a test:  What are the counts?
equalIsNot("isisnotno7Not") → false      true      X      
equalIsNot("isnotis") → false      true      X      

Open in new window

 
In fact, when your program passes a test:  What are the counts?
equalIsNot("This is notnot") → true      true      OK      
equalIsNot("noisxxnotyynotxisi") → true      true      OK      

Open in new window

 You can be right half the time just by guessing.
Avatar of gudii9

ASKER

public boolean equalIsNot(String str) {
boolean result=false;
for(int i=0;i<=str.length()-5;i++){
  int countIs=0;
  int countNot=0;
  
 
     /* int countIs = 0;
      int idxIs = 0;
      String subIs="is";*/
      
     /* while ((idxIs = str.indexOf(subIs, idxIs)) != -1) {
          countIs++;
          idxIs += str.length();
      }*/
      
      
     //  int countNot = 0;
     // int idxNot = 0;
      String subNot="Not";
     /* while ((idxNot = str.indexOf(subNot, idxNot)) != -1) {
          countNot++;
          idxNot += str.length();
      }*/
      
      
     /* if(idxIs==idxNot)
      return true;
      else 
      return false;*/
      
      
      if(str.substring(i,i+3).equals("is")){
        countIs++;
        
      }
      
      if(str.substring(i,i+4).equals("not")){
        countNot++;
        
      }
      
      if(countIs==countNot){
        result=true;
      }else{
        
   
      result=false;
      }
     
}
 return result;
      
}

Open in new window

Expected      Run            
equalIsNot("This is not") → false      true      X      
equalIsNot("This is notnot") → true      true      OK      
equalIsNot("noisxxnotyynotxisi") → true      true      OK      
equalIsNot("noisxxnotyynotxsi") → false      true      X      
equalIsNot("xxxyyyzzzintint") → true      true      OK      
equalIsNot("") → true      false      X      
equalIsNot("isisnotnot") → true      true      OK      
equalIsNot("isisnotno7Not") → false      true      X      
equalIsNot("isnotis") → false      true      X      
equalIsNot("mis3notpotbotis") → false      true      X      
other tests
X      

passed few failed few. please advise
The same advice as before.

Look at any/every test string you get wrong and count the number of is's and not's by hand.
Then use the debugger or print statements to examine the values of countIs and countNot that your code is generating.
You have no hope of fixing your code if you don't know where and why and how things are going wrong.

And check the strings you are getting right too.  Your program may just be lucky.
Avatar of gudii9

ASKER

The same advice as before.

Look at any/every test string you get wrong and count the number of is's and not's by hand.
Then use the debugger or print statements to examine the values of countIs and countNot that your code is generating.
You have no hope of fixing your code if you don't know where and why and how things are going wrong.

And check the strings you are getting right too.  Your program may just be lucky.

Open in new window


i will check it.
Avatar of gudii9

ASKER

i was looking for str.contains("is") to see how many times is there but looks like that is not there
Does that mean that your ran your program on "is" and isCount was zero?

If so, that is a start.  What does it mean, if you can't get the simplest string correct?
I have no idea.  You have to check more strings.

Next run    " is"     "is "     " is "     "isisis"     " is is is "   and   "is this his fish"

Don't go any further until you get the is_counter part of the program working.
Or does it mean that you started using the str.contains() function for some reason instead of the str.IndexOf() function that you were using before?

If so:  Why???
Avatar of gudii9

ASKER

this challenge going over my head..still thinking on design and development of this problem
Avatar of gudii9

ASKER

public class EqualIsNot {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("value-->"+equalIsNot("This is notnot"));
		/*System.out.println("value-->"+equalIsNot("noisxxnotyynotxisi"));
		System.out.println("value-->"+equalIsNot("xxxyyyzzzintint"));
		System.out.println("value-->"+equalIsNot("This is notnot"));
		System.out.println("value-->"+equalIsNot("This is notnot"));
		System.out.println("value-->"+equalIsNot("This is notnot"));
*/
	}
	
	public static boolean equalIsNot(String str) {
		boolean result=false;
		int countIs=0;
		  int countNot=0;
		  System.out.println("len is====>"+str.length());
		for(int i=0;i<=str.length();i++){
		  
		  
		      
		      if(str.substring(i,i+2).equals("is")){
		        countIs++;
		        
		      }
		      
		      if(str.substring(i,i+3).equals("not")){
		        countNot++;
		        
		      }
		     
		      
		      if(countIs==countNot&&countIs>0&&countNot>0){
		        result=true;
		      }else{
		        
		   
		      result=false;
		      }
		     
		}
		 System.out.println("countIs is-->"+countIs);
	      System.out.println("countNot is-->"+countNot);
		 return result;
		      
		}
 
	

}

Open in new window

did debug. Now struck at

len is====>14
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 15
      at java.lang.String.substring(Unknown Source)
      at EqualIsNot.equalIsNot(EqualIsNot.java:29)
      at EqualIsNot.main(EqualIsNot.java:6)
What does "did debug" mean?

What values are you getting for countIs for your test strings?

Why did you stop using str.indexOf() ?

What happens to   str.substring(i,i+3)   when   i=str.length() ?

There are at least three different algorithms used/suggested in this thread.  
Please describe the one you are trying to use.

This seems to be too big a challenge for you to do in one step.
So first write a program just to print out the number of times "is" appears in a string.
Avatar of gudii9

ASKER

public class EqualIsNot {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("value-->"+equalIsNot("This is not"));
		System.out.println("value-->"+equalIsNot("This is notnot"));
		System.out.println("value-->"+equalIsNot("noisxxnotyynotxisi"));
		System.out.println("value-->"+equalIsNot("xxxyyyzzzintint"));
		System.out.println("value-->"+equalIsNot("This is notnot"));
		System.out.println("value-->"+equalIsNot("This is notnot"));
		System.out.println("value-->"+equalIsNot("This is notnot"));

	}
	
	/**
	 * @param str
	 * 
Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).

equalIsNot("This is not") → false
equalIsNot("This is notnot") → true
equalIsNot("noisxxnotyynotxisi") → true
	 * @return
	 */
	public static boolean equalIsNot(String str) {
		boolean result=false;
		int countIs=0;
		  int countNot=0;
		  System.out.println("len is====>"+str.length());
		for(int i=0;i<=str.length();i++){
		  
		if(i<str.length()-3&&str.charAt(i)=='i'&&str.charAt(i+1)=='s'){
			countIs++;   
		      		     
		}
		 
		if(i<str.length()-3&&str.charAt(i)=='n'&&str.charAt(i+1)=='o'&&str.charAt(i+1)=='t'){
			countIs++;   
		}    		     
		
		if(countIs==countNot){
			System.out.println("countIs is-->"+countIs);
		      System.out.println("countNot is-->"+countNot);
			return true;
		}
		//return false;
		 System.out.println("countIs is-->"+countIs);
	      System.out.println("countNot is-->"+countNot);
		
		      
		}

		System.out.println("countIs is-->"+countIs);
	      System.out.println("countNot is-->"+countNot);
		return false;
	}
}

Open in new window


above is failing some edge cases. also not sure hwy it is not printing below
System.out.println("countIs is-->"+countIs);
            System.out.println("countNot is-->"+countNot);


output is

len is====>11
countIs is-->0
countNot is-->0
value-->true
len is====>14
countIs is-->0
countNot is-->0
value-->true
len is====>18
countIs is-->0
countNot is-->0
value-->true
len is====>15
countIs is-->0
countNot is-->0
value-->true
len is====>14
countIs is-->0
countNot is-->0
value-->true
len is====>14
countIs is-->0
countNot is-->0
value-->true
len is====>14
countIs is-->0
countNot is-->0
value-->true
All of your counts are zero.  What does this mean?

Why did you start using   str.charAt()   instead of   str.indexOf() ?

When we don't understand what you are doing or trying to do, we ask questions.
Why don't you answer them?

>>  There are at least three different algorithms used/suggested in this thread.  
       Please describe the one you are trying to use.

What would you like to do next?
Avatar of gudii9

ASKER

i want to  solve the challenge in all possible approaches so that i know all approaches. can you point me what mistake i did in above approach?
You can not solve 27 problems at once.  
You can solve all possible approaches if you want, but you still have to do it one at a time.

So describe the first approach you want to use for this problem.  
What string functions do you think might be helpful?
What control structures do you know how to use?
Why did you start with   str.indexOf() ?
Why did you change?
Avatar of gudii9

ASKER

for loop

Why did you start with   str.indexOf() ?
Why did you change?

i thought using indexOf is very round about rather i can check characters like i and then s and then n then 0 then t for is/not
Which of the code samples you have posted do you think has as worked the best?
Which one do you understand the best?
What functions and control structure did you use in that example?
Avatar of gudii9

ASKER

public class EqualIsNot {

      public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("value-->"+equalIsNot("This is not"));
            /*System.out.println("value-->"+equalIsNot("This is notnot"));
            System.out.println("value-->"+equalIsNot("noisxxnotyynotxisi"));
            System.out.println("value-->"+equalIsNot("xxxyyyzzzintint"));
            System.out.println("value-->"+equalIsNot("This is notnot"));
            System.out.println("value-->"+equalIsNot("This is notnot"));
            System.out.println("value-->"+equalIsNot("This is notnot"));
*/
      }
      
      /**
       * @param str
       *
Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).

equalIsNot("This is not") → false
equalIsNot("This is notnot") → true
equalIsNot("noisxxnotyynotxisi") → true
       * @return
       */
      public static boolean equalIsNot(String str) {
            boolean result=false;
            int countIs=0;
              int countNot=0;
              System.out.println("len is====>"+str.length());
            for(int i=0;i<=str.length();i++){
             
            if(i<str.length()-3&&str.charAt(i)=='i'&&str.charAt(i+1)=='s'){
                  countIs++;  
                                   
            }
             
            if(i<str.length()-3&&str.charAt(i)=='n'&&str.charAt(i+1)=='o'&&str.charAt(i+1)=='t'){
                  countIs++;  
            }                    
            
            if(countIs==countNot){
                  System.out.println("countIs is-->"+countIs);
                  System.out.println("countNot is-->"+countNot);
                  return true;
            }
            //return false;
             System.out.println("countIs is-->"+countIs);
            System.out.println("countNot is-->"+countNot);
            
                 
            }

            System.out.println("countIs is-->"+countIs);
            System.out.println("countNot is-->"+countNot);
            return false;
      }
}

above code function is method names like main and equalsIsNot()
for is one loop i used.
i used if statements as control structures to filter and check some conditions
Avatar of gudii9

ASKER


Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).

equalIsNot("This is not") → false
equalIsNot("This is notnot") → true
equalIsNot("noisxxnotyynotxisi") → true


check first letter if starts with i or n
if i check next letter is s
if n check next lettro and next t

repeate for last but one character for is..get count..one for loop
repeate for last but 2 for not..get count..separate second for loop
if counts equal true
'as one expert said writing in words help before writing code to finalize on best approach. i wrote as above. let me try that way
Avatar of gudii9

ASKER

yahooo...i finally passed all my tests

public boolean equalIsNot(String str) {
		boolean result = false;
		int countIs = 0;
		int countNot = 0;
	//	System.out.println("len is====>" + str.length());
		for (int i = 0; i <= str.length() - 2; i++) {

			if (str.charAt(i) == 'i' && str.charAt(i + 1) == 's') {
				countIs++;
			//	System.out.println("countIs is====>" + countIs);

			}

		}

		for (int j = 0; j <= str.length() - 3; j++) {
			if (str.charAt(j) == 'n' && str.charAt(j + 1) == 'o' && str.charAt(j + 2) == 't') {
				countNot++;
			//	System.out.println("countNot is====>" + countNot);
			}

		}

		if (countIs == countNot) {
			// System.out.println("countIs is 000-->"+countIs);
			// System.out.println("countNot is 000-->"+countNot);
			return true;
		}

		return false;
	}

Open in new window


equalIsNot("This is not") → false      false      OK      
equalIsNot("This is notnot") → true      true      OK      
equalIsNot("noisxxnotyynotxisi") → true      true      OK      
equalIsNot("noisxxnotyynotxsi") → false      false      OK      
equalIsNot("xxxyyyzzzintint") → true      true      OK      
equalIsNot("") → true      true      OK      
equalIsNot("isisnotnot") → true      true      OK      
equalIsNot("isisnotno7Not") → false      false      OK      
equalIsNot("isnotis") → false      false      OK      
equalIsNot("mis3notpotbotis") → false      false      OK      
other tests
OK

how to improve above code and approach.
\so lesson to take home for me is write in my  own words how i approach a code or challenge or task before beginning a single line of code.

other leson is use eclipse debugger to see any fine gaps which we miss using naked eye.
Avatar of gudii9

ASKER

also use pencil and paper or board/slate and erasable marker which makes code and data and flow easy