Programming Languages war
Python, PHP, Javascript (Nodejs)
Reverse Integer

Programming Languages war Python, PHP, Javascript (Nodejs) Reverse Integer

Play this article

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

This is a challenge of leetcode.com I made this challenge with Python3, and next I made PHP and Javascript the same way.

Problem:

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123 Output: 321

I'm going to explain in each language, so, here we go!

Python

I turn from integer to string the number, the string to the dictionary, I had to validate if the number is negative.

Next I reverse the dictionary with the method "reverse", and I use the method "join" to turn the dictionary to string and I add the symbol plus the new string.

I turn the string to integer.

And the end, I validate if the number is an integer of 32 bits yet and return that.

class Solution:
  def reverse(self, x: int):
    x = str(x)
    y = ''
    symbol = ''
    xdict = []
    for xx in x:
      if xx != '-':
        xdict.append(xx)
      else:
        symbol = xx
    xdict.reverse()
    xdict = ''.join(xdict)
    y = symbol + xdict
    y = int(y)
    if y > 2 ** 31 - 1 or y < -2 ** 31:
      y = 0
    return y


sol = Solution()
result = sol.reverse(123)
print(result)

PHP

I turn from integer to string the number, the string to the array, I had to validate if the number is negative.

Next I reverse the array with the method "array_reverse", and I use the method "implode" to turn the array to string and I add the symbol plus the new string.

I turn the string to integer.

And the end, I validate if the number is an integer of 32 bits yet and return that.

<?php
class Solution
{
    public function reverse($x)
    {
        $x      = (string) $x;
        $x      = str_split($x);
        $y      = '';
        $symbol = '';
        $xarray = [];
        foreach ($x as $xx) {
            if ($xx != '-') {
                $xarray[] = $xx;
            } else {
                $symbol = $xx;
            }
        }
        $xarray = array_reverse($xarray);
        $xarray = implode($xarray);
        $y      = $symbol . $xarray;
        $y      = (integer) $y;
        if ($y > 2 ** 31 - 1 || $y < -2 ** 31) {
            $y = 0;
        }
        return $y;
    }
}

$sol    = new Solution();
$result = $sol->reverse(1534236469);
var_dump($result);
?>

Javascript

I turn from integer to string the number, the string to the array, I had to validate if the number is negative.

Next I reverse the array with the method "reverse", and I use the method "join" to turn the array to string and I add the symbol plus the new string.

I turn the string to integer.

And the end, I validate if the number is an integer of 32 bits yet and return that.

class Solution {
  reverse(x) {
    x = x.toString();
    let y = '';
    let symbol = '';
    let xarray = [];
    for (let i = 0; i < x.length; i++) {
      if (x[i] !== '-') {
        xarray.push(x[i]);
      } else {
        symbol = x[i];
      }
    }
    xarray.reverse();
    xarray = xarray.join('');
    y = symbol + xarray;
    y = parseInt(y);
    if (y > 2 ** 31 - 1 || y < (-2) ** 31) {
      y = 0;
    }
    return y;
  }
}

const sol = new Solution();
const result = sol.reverse(123);
console.log(result);

Conclusion

  • Structure: Each language has a little difference.
  • Lines: Python: 23, PHP: 32, Javascript: 27
  • Time to execution: Python: 32 ms, PHP: 8 ms, Javascript: 104 ms
  • Memory Usage: Python: 14.1 MB, PHP: 15.5 MB, Javascript: 40.2 MB

Would you like that I write more challenges? What do you like more about challenges?

I hope you enjoy my post and remember that I am just a Dev like you!