DEV Community

Annette Michaels
Annette Michaels

Posted on

Reverse an Integer - Code Challenge

Leetcode Problem 7

Given a 32-bit signed integer, reverse digits of an integer.

Examples:
Input: -123
Output: -321

Input: 120
Output: 21

Input: 123
Output: 321

At first glance this problem is easy. In Ruby just convert to string, reverse, and convert back to integer and Ruby has all those nice built in functions to do just that and make it easy.

def reverse(x)

    x.to_s.reverse.to_i

end
Enter fullscreen mode Exit fullscreen mode

However, this will not accomodate a negative number as the reverse function does not accomodate then negative. So this is what happens.

irb(main):016:0> x = -123
=> -123
irb(main):017:0> x.to_s.reverse.to_i
=> 321

Enter fullscreen mode Exit fullscreen mode

What we really want is -321 and one way to accomodate this is to create a variable that indicates whether or not it should be negative and add it if necessary on the return statement.

def reverse(x)

    neg = x < 0 
    x.to_s.reverse.to_i
    return neg ? -x : x

end

Enter fullscreen mode Exit fullscreen mode

Almost there. We have not considered a key part of the problem. It is a 32-bit integer.

Integers can be huge, however, when programming there are limitations. Technically you could have a number that is infinite places long and have it be an integer but that would also break whatever you are working on (hello stackoverflow). So when the question says 32-bit integer what it is saying is the largest binary number with 32 places so...

11111111111111111111111111111111 in base 2 math is equal to

4294967295 in base 10 math.

However, the problem also specifies signed so the first bit is a + or - resulting in 31 1's not 32 so we end up with the magic number as

2147483648

So the range is -2147483648 to 2147483648 right? But what about 0? Well the positive integer gets it so the range results in

-2147483648 to 2147483647 non-inclusive of those numbers.

To accommodate this we have to check if the integer input is greater or less than those ranges before returning a result.

def reverse(x)

    neg = x < 0
    x = x.to_s.reverse.to_i

    return  0 if (x > 2147483646 || x < -2147483647)

    return neg ? -x : x
end
Enter fullscreen mode Exit fullscreen mode

If you wanted to not use as many built in functions building out the reverse is always good practice.

def reverse(x)
    i = 0

    neg = x < 0
    x = x.to_s

    x.length % 2 == 0? (half = x.length/2) : (half = x.length/2 + 1)
    len = x.length - 1

    while i < half
      temp = x[i]        
      x[i] = x[len - i]
      x[len - i] = temp
      i += 1
    end

    x = x.to_i

    return  0 if (x > 2147483646 || x < -2147483647)

    return neg ? -x : x
end
Enter fullscreen mode Exit fullscreen mode

In Javascript we end up with

var reverse = function(x) {
    const neg = x < 0

    x = Math.abs(x).toString().split("").reverse().join("")
    x = parseInt(x)

    if (neg) {
        x = - + x
    }

    if (x < -2147483647 || x > 2147483646){
        return 0
    } else {
        return x
    }
}
Enter fullscreen mode Exit fullscreen mode

Of course the parseInt can be combined with the line above it but it's a little easier to read on a different line.

Until my next code challenge.

Top comments (2)

Collapse
 
smartcodinghub profile image
Oscar • Edited

You can do this with mathematical operations that are way faster and simpler. Example in C#:

int number = -321; // Number
int sign = Math.Sign(number); // Sign
number *= sign; // Abs

int n = 0; // Accumulator
do
{
   /* '* 10' moves the accumulator the the left to accumulate one more digit. 
    * Then, '% 10' extracts the last digit */
    n = n * 10 + number % 10;
} while ((number /= 10) > 0); // '/ 10' removes the last digit

n *= sign; // Add the sign to the already reversed number

So, with -321:

  1. Take the sign (-) and the abs (321).
  2. The accumulator is 0.
    1. Move left: n * 10 = 0. Digit: number % 10 = 1. Remove: number / 10 = 32.
    2. Move left: n * 10 = 10. Digit: number % 10 = 2. Remove: number / 10 = 3.
    3. Move left: n * 10 = 120. Digit: number % 10 = 3. Remove: number / 10 = 0.
  3. Then, we have 321 and multiply by the sign(-) and have -123.
Collapse
 
rygelxvi profile image
Annette Michaels

I didn't even think about using regex. I like it =)