The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Hi,

I am working on below challenge

http://codingbat.com/prob/p192383

i am not clear on how to find eight count using recursion. please advise

I am working on below challenge

http://codingbat.com/prob/p192383

Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

count8(8) → 1

count8(818) → 2

count8(8818) → 4

i am not clear on how to find eight count using recursion. please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

https://www.experts-exchange.com/questions/28968896/count7-challenge.html

There is just an extra conditional here.

```
public int count8(int n) {
if (n < 1) {
return 0;
}
if ((n % 10) == 8){ return 1 + count8(n/10);
}
else {
return count8(n/10);
}
}
```

above failing some tests

Expected Run

count8(8) → 1 1 OK

count8(818) → 2 2 OK

count8(8818) → 4 3 X

count8(8088) → 4 3 X

count8(123) → 0 0 OK

count8(81238) → 2 2 OK

count8(88788) → 6 4 X

count8(8234) → 1 1 OK

count8(2348) → 1 1 OK

count8(23884) → 3 2 X

count8(0) → 0 0 OK

count8(1818188) → 5 4 X

count8(8818181) → 5 4 X

count8(1080) → 1 1 OK

count8(188) → 3 2 X

count8(88888) → 9 5 X

count8(9898) → 2 2 OK

count8(78) → 1 1 OK

other tests

X

https://www.experts-exchange.com/questions/28968896/count7-challenge.html

There is just an extra conditional here.

what is extra conditional compared to count7 challenge here for count8 challenge?

what is extra conditionalThe challenge stated

except that an 8 with another 8 immediately to its left counts doubleSo, we need something like

```
if(n % 10 == 8){
if(next digit is 8)return 2 + count8(n / 10);
else return 1 + count8(n / 10);
}
```

--

DECLARE @intValue AS VARCHAR(10) = '1818E8'

SELECT SUM( CASE WHEN SUBSTRING(@intValue,number

(

SELECT DISTINCT number FROM

MASTER..SPT_VALUES WHERE number > 0 AND number <= DATALENGTH(@intValue)

) x

--

```
public int count8(int n) {
int result=0;
if (n < 1) {
result=0;
}
if ((n % 10) == 8){
if ((n+1 % 10) == 8){
result= 2 + count8(n/10);
// return result;
}
else{
result= 1 + count8(n/10);
// return result;
}
}
return result;
}
/* else {
return count8(n/10);
}*/
/* if(n % 10 == 8){
if(next digit is 8)return 2 + count8(n / 10);
else return 1 + count8(n / 10);*/
```

above fails below tests

Expected Run

count8(8) → 1 1 OK

count8(818) → 2 1 X

count8(8818) → 4 1 X

count8(8088) → 4 2 X

count8(123) → 0 0 OK

count8(81238) → 2 1 X

count8(88788) → 6 2 X

count8(8234) → 1 0 X

count8(2348) → 1 1 OK

count8(23884) → 3 0 X

count8(0) → 0 0 OK

count8(1818188) → 5 2 X

count8(8818181) → 5 0 X

count8(1080) → 1 0 X

count8(188) → 3 2 X

count8(88888) → 9 5 X

count8(9898) → 2 1 X

count8(78) → 1 1 OK

other tests

X

please advise

```
if ((n+1 % 10) == 8)
```

That is not what you want to do. That tests whether the present input + 1 has a 8 as its last digit. You want to use n/10 to peek at the input that will follow the present one.
Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
public int count8(int n) {
int result=0;
if (n < 1) {
result=0;
}
if ((n % 10) == 8){
if ((n/10) == 8){
result= 2 + count8(n/10);
// return result;
}
else{
result= 1 + count8(n/10);
// return result;
}
}
return result;
}
/* else {
return count8(n/10);
}*/
/* if(n % 10 == 8){
if(next digit is 8)return 2 + count8(n / 10);
else return 1 + count8(n / 10);*/
/*That is not what you want to do. That tests whether the present input + 1
has a 8 as its last digit.
You want to use n/10 to peek at the input that will follow the present one.*/
```

like above?it still fails same tests?
```
public int count8(int n) {
//int result=0;
if (n < 1) {
return 0;
}
if (n >=88 && n%100==88){
return 2 + count8(n/10);
// return result;
}
else if( n%10==8){
// result= 1 + count8(n/10);
// return result;
return 1 + count8(n/10);
}
return count8(n/10);
// return result;
}
/* else {
return count8(n/10);
}*/
/* if(n % 10 == 8){
if(next digit is 8)return 2 + count8(n / 10);
else return 1 + count8(n / 10);*/
/*That is not what you want to do. That tests whether the present input + 1
has a 8 as its last digit.
You want to use n/10 to peek at the input that will follow the present one.*/
```

above passed all tests. any improvements or alternate approaches?

in above fashion code is there else belong to top if or second(last if in this case)?

```
if (n >=88 && n%100==88){
```

That doesn't look right.Here is my solution.

```
public int count8(int n) {
if(n == 0)return 0;
if(n % 10 == 8){
if((n / 10) % 10 == 8)return 2 + count8(n / 10);
else return 1 + count8(n / 10);
}
else return count8(n / 10);
}
```

```
public int count8(int n) {
if(n == 0)return 0;
if(n % 10 == 8){
if((n / 10) % 10 == 8)return 2 + count8(n / 10);
else return 1 + count8(n / 10);
}
else return count8(n / 10);
}
```

i like this approach
Java EE

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

Open in new window