Programming Languages war
Python, PHP, Javascript (Nodejs)
Find Median Sorted Arrays

Programming Languages war Python, PHP, Javascript (Nodejs) Find Median Sorted Arrays

English

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 two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

Example 1:

Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.

Python

Python is simple to merge two arrays, with the symbol "+" is enough =P, next it's necessary sort the array with the method "sort()", I get the length of the list merged and divided by 2, I need to know if the result has decimals or not, for that I used the method "is_integer".

If it hasn't decimals I use the method "int()" to transform float in decimal number to get one position the middle of the list, the second position is "-1", so add the two data in these positions, and divided by 2.

If it has decimals, it's odd the length of the list, with the method "int()" round out the number to the middle of the list.

In the end I return the result.

class Solution:
  def findMedianSortedArrays(self, nums1, nums2):
    list1 = nums1 + nums2
    list1.sort()
    position = len(list1) / 2
    if (position).is_integer():
      position = int(position)
      result = (list1[position] + list1[position-1]) / 2
    else:
      result = list1[int(position)]
    return result


nums1 = [1, 3]
nums2 = [2]
sol = Solution()
result = sol.findMedianSortedArrays(nums1, nums2)
print(result)

PHP

PHP needs to merge two arrays a method called "array_merge", next it's necessary sort the array with the method "sort()", I get the length of the array merged and divided by 2, I need to know if the result has decimals or not, for that I used the method "is_float".

If it hasn't decimals I use the method "intval()" to transform float in decimal number to get one position the middle of the array, the second position is "-1", so add the two data in these positions, and divided by 2.

If it has decimals is odd the length of the array, with the method "intval()" round out the number to the middle of the array.

In the end I return the result.

<?php

class Solution
{

    public function findMedianSortedArrays($nums1, $nums2)
    {
      $array1 = array_merge($nums1, $nums2);
      sort($array1);
      $position = count($array1) / 2;
      if (!is_float($position)){
        $position = intval($position);
        $result = ($array1[$position] + $array1[$position-1]) / 2;
      }else{

        $result = $array1[intval($position)];
      }
      return $result;
    }
}

$nums1  = [1, 3];
$nums2  = [2];
$sol    = new Solution();
$result = $sol->findMedianSortedArrays($nums1, $nums2);
print_r($result);
?>

Javascript

Javascript needs to merge two arrays a method called "concat", next it's necessary sort the array with the method "sort()", The method sort needs a function to compare numbers "function(a,b){return a - b}" if not add this function the numbers don't sort the right way, I get the length of the array merged and divided by 2, I need to know if the result has decimals or not, for that I used the validation "(position - Math.floor(position)".

If it hasn't decimals I use the method "parseInt()" to transform float in decimal number to get one position the middle of the array, the second position is "-1", so add the two data in these positions, and divided by 2.

If it has decimals is odd the length of the array, with the method "parseInt()" round out the number to the middle of the array.

In the end I return the result.

class Solution
{
    findMedianSortedArrays(nums1, nums2)
    {
      let result = 0;
      const array1 = nums1.concat(nums2);
      array1.sort(function(a,b){return a - b});
      let position = array1.length / 2;
      if ((position - Math.floor(position)) === 0){
        position = parseInt(position);
        result = (array1[position] + array1[position-1]) / 2;
      }else{
        result = array1[parseInt(position)];
      }
      return result;
    }
}

const nums1  = [1, 3];
const nums2  = [2];
const sol    = new Solution();
const result = sol.findMedianSortedArrays(nums1, nums2);
console.log(result);

Conclusion

  • Structure: This time the three codes are very similar. But the method "sort()" in Javascript needs a specific function to sort numbers.
  • Lines: Python: 18, PHP: 27, Javascript: 24
  • Time to execution: Python: 92 ms, PHP: 32 ms, Javascript: 276 ms
  • Memory Usage: Python: 14.6 MB, PHP: 15.9 MB, Javascript: 49.5 MB

Would you like that I write about more challenges?

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