79549095

Date: 2025-04-01 20:15:33
Score: 1.5
Natty:
Report link

Any help would be appreciated.

First create a test harness.


A test harness for those who would like exercise a solution.

int reverse(int n){
    if(n/10==0) return n;
    int temp = n/10;
    n = n - (n%100) + n%10*10 + n/10%10;//switch the first two digits
    n = reverse(n/10)*10 + n%10;
    int r = reverse(temp);
    n = n-temp+r;
    return n;
}

int rev(int work) {
  int siz = 1, result = work, digit, power = 1;

  while (result > 9)   {
    siz++;
    result /= 10;
  }

  if (siz == 1) /* If just the last digit, then return that digit which will now become the first digit             */
    return work;

  for (int i = 0; i < (siz - 1); i++)
    power *= 10;

  digit = work / 10; /* Aquire the right most digit for the work integer value                                           */
  digit = work - (digit * 10);
  result = rev(work / 10) + digit * power; /* Add the reevaluated digit with the recursive call to reversing function  */
  return result;

}

void  test_rev(const char *s, int (*f)(int)) {
  puts(s);
  int x[] = { 0, 1, 100, 1234, INT_MAX/10, INT_MAX };
  unsigned n = sizeof x/sizeof x[0];
  for (unsigned i = 0; i < n; i++) {
    printf("x:%11d ", x[i]);
    fflush(stdout);
    printf("y:%11d\n", f(x[i]));
    printf("x:%11d ", -1 - x[i]);
    fflush(stdout);
    printf("y:%11d\n", f(-1 - x[i]));
  }
  puts("");
}

#include <limits.h>
#include <stdio.h>

int main(void) {
  test_rev("Daniel Levi", reverse);
  test_rev("NoDakker", rev);
}

Output

Daniel Levi
x:          0 y:          0
x:         -1 y:         -1
x:          1 y:          1
x:         -2 y:         -2
x:        100 y:          1
x:       -124 y:       -421
x:       1234 y:       4411 Fail
x:      -1235 y:      -5411 Negative fail
x:  214748364 y:  666396462 Fail
x: -214748365 y: -766396462 Negative fail
x: 2147483647 y:   82650772 Overflow
x:-2147483648 y:-1082650772 Overflow

NoDakker
x:          0 y:          0
x:         -1 y:         -1
x:          1 y:          1
x:         -2 y:         -2
x:        100 y:          1
x:       -124 y:       -124 Negative, no reversal
x:       1234 y:       4321
x:      -1235 y:      -1235 Negative, no reversal
x:  214748364 y:  463847412
x: -214748365 y: -214748365 Negative, no reversal
x: 2147483647 y:-1126087180 Overflow
x:-2147483648 y:-2147483648 Negative, no reversal
Reasons:
  • Blacklisted phrase (1): appreciated
  • Blacklisted phrase (1): Any help
  • RegEx Blacklisted phrase (3): Any help would be appreciated
  • Long answer (-1):
  • Has code block (-0.5):
  • High reputation (-2):
Posted by: chux