Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/823485380662095873
removed salutation and quoted task description
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 190

Salutations,

I was doing a few basic Java recursion problems to refresh my memory with recursion (honestly never had to use it in a long, LONG, time), along with using it as a useful way to prepare for any possible interviews for entry level positions. I came upon this problem on codingbat.com:

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

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

My solution, which was all correct for all the test cases they had is as follows:

public int count8(int n) {
 if(n == 0) {
 return 0;
 } else {
 
 int count = (n % 10 == 8)? 1: 0;
 
 //This is testing for adjacent 8s. 
 if(count == 1) {
 int tempN = n;
 tempN = tempN / 10;
 count = (tempN % 10 == 8) ? 2 : 1;
 }
 
 n = n/10;
 return count + count8(n);
 }
}

I was wondering if anyone could critique this solution, and let me know if this is the best approach, or if there is a better way. I am not a big fan of the if(count == 1) that I created, but that was the only way I could think of to test the presence of an 8 to the left of the current one.

Salutations,

I was doing a few basic Java recursion problems to refresh my memory with recursion (honestly never had to use it in a long, LONG, time), along with using it as a useful way to prepare for any possible interviews for entry level positions. I came upon this problem on codingbat.com:

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

My solution, which was all correct for all the test cases they had is as follows:

public int count8(int n) {
 if(n == 0) {
 return 0;
 } else {
 
 int count = (n % 10 == 8)? 1: 0;
 
 //This is testing for adjacent 8s. 
 if(count == 1) {
 int tempN = n;
 tempN = tempN / 10;
 count = (tempN % 10 == 8) ? 2 : 1;
 }
 
 n = n/10;
 return count + count8(n);
 }
}

I was wondering if anyone could critique this solution, and let me know if this is the best approach, or if there is a better way. I am not a big fan of the if(count == 1) that I created, but that was the only way I could think of to test the presence of an 8 to the left of the current one.

I was doing a few basic Java recursion problems to refresh my memory with recursion (honestly never had to use it in a long, LONG, time), along with using it as a useful way to prepare for any possible interviews for entry level positions. I came upon this problem on codingbat.com:

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

My solution, which was all correct for all the test cases they had is as follows:

public int count8(int n) {
 if(n == 0) {
 return 0;
 } else {
 
 int count = (n % 10 == 8)? 1: 0;
 
 //This is testing for adjacent 8s. 
 if(count == 1) {
 int tempN = n;
 tempN = tempN / 10;
 count = (tempN % 10 == 8) ? 2 : 1;
 }
 
 n = n/10;
 return count + count8(n);
 }
}

I was wondering if anyone could critique this solution, and let me know if this is the best approach, or if there is a better way. I am not a big fan of the if(count == 1) that I created, but that was the only way I could think of to test the presence of an 8 to the left of the current one.

Source Link
SomeStudent
  • 361
  • 1
  • 4
  • 12

Counting number of eights

Salutations,

I was doing a few basic Java recursion problems to refresh my memory with recursion (honestly never had to use it in a long, LONG, time), along with using it as a useful way to prepare for any possible interviews for entry level positions. I came upon this problem on codingbat.com:

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

My solution, which was all correct for all the test cases they had is as follows:

public int count8(int n) {
 if(n == 0) {
 return 0;
 } else {
 
 int count = (n % 10 == 8)? 1: 0;
 
 //This is testing for adjacent 8s. 
 if(count == 1) {
 int tempN = n;
 tempN = tempN / 10;
 count = (tempN % 10 == 8) ? 2 : 1;
 }
 
 n = n/10;
 return count + count8(n);
 }
}

I was wondering if anyone could critique this solution, and let me know if this is the best approach, or if there is a better way. I am not a big fan of the if(count == 1) that I created, but that was the only way I could think of to test the presence of an 8 to the left of the current one.

lang-java

AltStyle によって変換されたページ (->オリジナル) /