Solved

count8 challlenge

Posted on 2016-09-09
13
173 Views
Last Modified: 2016-10-15
Hi,
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
0
Comment
Question by:gudii9
  • 7
  • 4
  • 2
13 Comments
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41792255
--
----

DECLARE @intValue AS VARCHAR(10) = '8818'
 
SELECT SUM( CASE WHEN SUBSTRING(@intValue,number,1) LIKE '[8]' THEN 1 ELSE 0 END ) Count8DIGITS FROM
( 
  SELECT DISTINCT number FROM
  MASTER..SPT_VALUES WHERE number > 0 AND number <= DATALENGTH(@intValue) 
) x

--
--

Open in new window

0
 
LVL 27

Expert Comment

by:rrz
ID: 41793103
This challenge is almost the same as  
https://www.experts-exchange.com/questions/28968896/count7-challenge.html 
There is just an extra conditional here.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41793537
I will try
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Author Comment

by:gudii9
ID: 41793657
public int count8(int n) {
  if (n < 1) {
    return 0;
  }
  if ((n % 10) == 8){ return 1 + count8(n/10);
  }
  else {
  return count8(n/10);
  }
}

Open in new window


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      

This challenge is almost the same as  
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?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41793738
what is extra conditional
The challenge stated
except that an 8 with another 8 immediately to its left counts double
So, we need something like
    if(n % 10 == 8){
		if(next digit is 8)return 2 + count8(n / 10);
		else return 1 + count8(n / 10);
    }

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41793766
Enjoy ! Without Recursion and loop;

--

DECLARE @intValue AS VARCHAR(10) = '1818E8'
 
SELECT SUM( CASE WHEN SUBSTRING(@intValue,number,1) LIKE '[8]' THEN 1 ELSE 0 END ) Count8DIGITS FROM
(
  SELECT DISTINCT number FROM
  MASTER..SPT_VALUES WHERE number > 0 AND number <= DATALENGTH(@intValue)
) x

--
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796638
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);*/
    

Open in new window



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
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 41796836
 if ((n+1 % 10) == 8)

Open in new window

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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796901
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.*/

Open in new window

like above?it still fails same tests?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796935
I think i got approach.
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.*/

Open in new window


above passed all tests. any improvements or alternate approaches?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796946
if then if then else
in above fashion code is there else belong to top if or second(last if in this case)?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41796955
  if (n >=88 && n%100==88){

Open in new window

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);
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41796961
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);
}

Open in new window

i like this approach
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to concatenate fields in zpl, and how to use conditional statement? 3 53
how to debug htl and js pages 8 39
intellij error 2 17
Why use this lambda? 12 39
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

830 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