Solved

countHi2 challenge

Posted on 2016-09-23
7
52 Views
Last Modified: 2016-09-23
Hi,

I am working on below challenge.
http://codingbat.com/prob/p143900
Psedo code:
1. check the given string starts with hi and does not have x infront of it
2.if true increment by 1 and call recursive function
3. if false call recusive function again

I wrote my code as below and not passing all tests
public int countHi2(String str) {
 if (str.length() <2){ 
	    	return 0;
	    		}
	    if (str.startsWith( "hi")&!str.startsWith("xhi")) {
	    	return 1 + countHi2(str.substring(2));
	    	}
	    	else {
		   return countHi2(str.substring(1));
	    	  
	    	}
	
}

Open in new window


Expected      Run            
countHi2("ahixhi") → 1      2      X      
countHi2("ahibhi") → 2      2      OK      
countHi2("xhixhi") → 0      2      X      
countHi2("hixhi") → 1      2      X      
countHi2("hixhhi") → 2      2      OK      
countHi2("hihihi") → 3      3      OK      
countHi2("hihihix") → 3      3      OK      
countHi2("xhihihix") → 2      3      X      
countHi2("xxhi") → 0      1      X      
countHi2("hixxhi") → 1      2      X      
countHi2("hi") → 1      1      OK      
countHi2("xxxx") → 0      0      OK      
countHi2("h") → 0      0      OK      
countHi2("x") → 0      0      OK      
countHi2("") → 0      0      OK      
countHi2("Hellohi") → 1      1      OK      
other tests
X      
Any improvements or alternate approaches?      

please advise
0
Comment
Question by:gudii9
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 41812810
Your error in logic is this line
if (str.startsWith( "hi")&!str.startsWith("xhi")) 

Open in new window

You will have to break this up into two conditionals. If str starts with "xhi" then skip 3 indexes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41813030
public int countHi2(String str) {
 if (str.length() <2){ 
	    	return 0;
	    		}
	    if (str.startsWith( "hi")) {
	      if(!str.startsWith("xhi")){
	    	return 1 + countHi2(str.substring(2));
	    	}
	    }
	    	else {
		   return countHi2(str.substring(1));
	    	  
	    	}
	
}

Open in new window


something like above
I keep getting below error many times
Compile problems:


Error:      public int countHi2(String str) {
                 ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type int

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

please advise
0
 
LVL 27

Expert Comment

by:rrz
ID: 41813249
I will try to write pseudo code for  the code you posted.

if(length of str is less than 2) return 0  
if(str starts with hi and does not starts with xhi)
         return 1 + whatever countHi2(str minus the first two characters) returns
else return whatever countHi2(str minus the first character) returns

In the second if statement, you and I know that that if str starts with hi then it can't start with xhi(mutually exclusive). But the compiler doesn't know that. So, the compiler thinks that the first condition(start with hi) could be true and the second condition(start with xhi) could be false. If that did happen, then your code wouldn't return anything.  That is why you get that error.
Anyway, as I posted above here, you will have to break this up into two separate conditionals. If str starts with "xhi" then skip 3 indexes.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:gudii9
ID: 41813335
public int countHi2(String str) {
 if (str.length() <2){ 
	    	return 0;
	    		}
	    if (str.startsWith( "hi")) {
	      
	    	return 1 + countHi2(str.substring(2));
	    	}	  
	    	
	    	if (str.startsWith( "xhi")) {
	      
	    	return countHi2(str.substring(3));
	    	}	
	    	
  
	    	else {
		   return countHi2(str.substring(1));
	    	}
}
/*if(length of str is less than 2) return 0  
if(str starts with hi and does not starts with xhi)
         return 1 + whatever countHi2(str minus the first two characters) returns 
else return whatever countHi2(str minus the first character) returns */

Open in new window


above passes all tests. any improvement or alternate approaches?
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 41813356
My code is the same as yours. Personally, I like to format it differently.
public int countHi2(String str) {
    if(str.length() < 2)return 0;
	if(str.startsWith("xhi"))return countHi2(str.substring(3));
	if(str.startsWith("hi"))return 1 + countHi2(str.substring(2));
	return countHi2(str.substring(1));
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41813358
if(str.startsWith("xhi"))return countHi2(str.substring(3));
	if(str.startsWith("hi"))return 1 + countHi2(str.substring(2));

Open in new window

your order as above is reverse compared to my order as below

 if (str.startsWith( "hi")) {
	      
	    	return 1 + countHi2(str.substring(2));
	    	}	  
	    	
	    	if (str.startsWith( "xhi")) {
	      
	    	return countHi2(str.substring(3));
	    	}	
	    	

Open in new window



does it matter the order here or in general??
0
 
LVL 27

Expert Comment

by:rrz
ID: 41813372
does it matter the order here or in general??
In general, it probably matters. But, in this case it doesn't matter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 58
simple java question 3 44
WPF - Tooltips for ComboBox items 5 36
configure dependency in POM for new database 3 17
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now