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.
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
----
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
--
--
public int count8(int n) {
if (n < 1) {
return 0;
}
if ((n % 10) == 8){ return 1 + count8(n/10);
}
else {
return count8(n/10);
}
}
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 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);
}
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);*/
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
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.
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.*/
If you are experiencing a similar issue, please ask a related question
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 |
Join the community of 500,000 technology professionals and ask your questions.