Programming Languages war
Python, PHP, Javascript (Nodejs)
ZigZag Conversion

Programming Languages war Python, PHP, Javascript (Nodejs) ZigZag Conversion

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:

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P A H N A P L S I I G Y I R

And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"

General explain:

This is the numRows: 3

This is the string:

P A Y P A L I S H I R I N G 0 1 2 3 4 5 6 7 8 9 0 1 2 3

Firs loop I need to move 4 positions, but if numRows is 4 I have to move 6, then, the logic is numRows * 2 - 2:

Next loop I have to move 4 positions plus the position currently the numRows loop, and in the same loop, I have to get the j position plus the nextLoop variables less the position currently the numRows loop.

The last loop is the same as the first.

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

Python

I have to loop two twice, the first loops the numRows and get this position and the second loop the string if i is greater than 0 and i less than numRows get the position j plus nextLoop less i.

I have to use "try - except" here in Python because "j + i" could get a position out of range of string.

In the end, return result.

class Solution:
  def convert(self, s, numRows):
    if numRows == 1:
      return s
    result = ''
    nextLoop = numRows * 2 - 2
    for i in range(numRows):
      for j in range(0, len(s), nextLoop):
        try:
          result = result + s[j + i]

          if i > 0 and i < numRows - 1 and j + nextLoop - i < len(s):
            result = result + s[j + nextLoop - i]
        except:
          pass
    return result


sol = Solution()
result = sol.convert('PAYPALISHIRING', 3)
print(result)

PHP

I have to change the string to the array. I have to loop two twice, the first loops the numRows and get this position and the second loop the string if i is greater than 0 and i less than numRows get the position j plus nextLoop less i.

If the position doesn't exist, PHP doesn't add anything.

In the end, return result.

<?php
class Solution {
  function convert($s, $numRows) {
    if ($numRows == 1) return $s;
    $s  = str_split($s);
    $result = '';
    $nextLoop = $numRows * 2 - 2;

    for ($i = 0; $i < $numRows; $i++) {
      for ($j = 0; $j < count($s); $j = $j + $nextLoop) {
        $result = $result . $s[$j + $i];        
        if ($i > 0 && $i < $numRows - 1 && $j + $nextLoop - $i < count($s)) {
          $result = $result . $s[$j + $nextLoop - $i];
        }
      }
    }
    return $result;
  }
}

$sol = new Solution();
$result = $sol->convert('PAYPALISHIRING', 3);
var_dump($result);
?>

Javascript

I have to loop two twice, the first loops the numRows and get this position and the second loop the string if i is greater than 0 and i less than numRows get the position j plus nextLoop less i.

I have to validate if the position "j + i" is undefined.

In the end, return result.

class Solution {
  convert(s, numRows) {
    if (numRows == 1) return s;
    let result = '';
    let nextLoop = numRows * 2 - 2;

    for (let i = 0; i < numRows; i++) {
      for (let j = 0; j < s.length; j = j + nextLoop) {
        if (s[j + i] != undefined) {
          result = result + s[j + i];
        }
        if (i > 0 && i < numRows - 1 && j + nextLoop - i < s.length) {
          result = result + s[j + nextLoop - i];
        }
      }
    }
    return result;
  }
}

const sol = new Solution();
const result = sol.convert('PAYPALISHIRING', 3);
console.log(result);

Conclusion

  • Structure: Each language has a little difference.
  • Lines: Python: 21, PHP: 24, Javascript: 23
  • Time to execution: Python: 68 ms, PHP: 24 ms, Javascript: 104 ms
  • Memory Usage: Python: 14.1 MB, PHP: 15.8 MB, Javascript: 43.1 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!