An accepted 6 line Python solution


  • 2
    Y
    class Solution:
        # @param {string} s
        # @param {integer} numRows
        # @return {string}
        def convert(self, s, numRows):
            if numRows == 1 or numRows >= len(s): return s
            final = [[] for row in xrange(numRows)]
            for i in xrange(len(s)): 
                final[numRows -1 - abs(numRows - 1 - i % (2 * numRows - 2))].append(s[i])
            return "".join(["".join(final[i]) for i in xrange(numRows)])

  • 0
    M

    6 lines of hard to read code. meh...You should have put all your processing in a single live while you're at it.
    i.e. show off, not helpful.


  • 0
    Y

    Hey, that's rather discouraging. Really, I cound've just compressed the code into a single return statement but that'll be silly. The program is short because it is straight-forward and intuitive (and written in Python), if anything. It defines a nRow array and sequencially reads s, and put each character to the corresponding row according to the rule [rowNumber in the result array = numRows -1 - abs(numRows - 1 - i % (2 * numRows - 2))] (just draw a picture and verify). Programs should be readable and crystal clear, sure, but there's no point of filling even the slightest gap for the reader, is there? If I add those as comment between the codes, it will be like repeating exactly what the codes say. I respect the other rule: don't insult your reader's intellegence.


  • 0
    M

    Easy to read code is appreciated by all, the intelligent and not so intelligent.


  • 0
    Y

    First apologies for being slightly rude. That said, I believe my code is rather clear. It almost looks like psyudocode. In that regard it is indeed easy to read, if you will. The idea behind it though, does require a bit of thinking. But not much. It is common if you are in the world of algorithms, right? Merge sort is much more complex than this but do you expect a short excerpt from TAOC before every merge sort code? In the much harder problems that require dynamic programming it would be almost impossible to expain the code fully if you do not already know dynamic programming. They'll be much harder to read than this, I'm afraid.


  • 0
    M

    It is probably clear, if you know what zigzag means.
    There's no point in me or anyone else checking your code. I believe you when you say it works. The task is so simple a drawn out calculation would run as fast, if one knew what actually zigzag meant for row sizes other than 3.
    I don't mean to offend, but if you're trying to help you need not post solutions, explanations and comments go a long way to communicate your thought process, especially when the problem is not defined well. I'd rather read your comments and write my own solution than "reverse engineer" and reproduce. To each his/her own.


  • 0
    Y

    Thanks for the advice.


  • 0
    L

    Good coding style for me to learn, I like those concise solutions.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.