Python wrong answer?

  • 5

    Hi all,

    here is a simple python code for this question, but I can't figure out a test case

    class Solution:
    # @param num, a list of integers
    # @return a string
    def largestNumber(self, num):
        num = [str(x) for x in num]
        num.sort(cmp=lambda x, y: cmp(y+x, x+y))
        ret = ''.join(num)
        return ret.lstrip('0') or '0'

    The idea is simple, first I convert all int in num to strings, then provide a custom compare function. In this lambda function, just concatenate and compare (y+x) and (x+y, the reason to switch (x+y) and (y+x) is because we want this array to be sorted in decreasing order.

    For example,

    cmp(22, 221) -> should form 22221 (because 22221>22122), hence cmp(22,221) == -1

    Most of the test cases run through fine but the longest test case


    Failed saying "Wrong Answer" and there is no Output.

    I tried this test case on my own machine and the function works perfectly.
    I guess there is something wrong with the system for python?

  • 1

    I had the same problem. The longest case passed locally but didn't work at OJ.

  • 0

    Yeah I had the same problem,214/215 tests passed only the last one failed.

  • 0

    Sorry for that. There is a mistake in Python runner. And it wil be fixed soon.

  • 0

    The fix was just pushed live. Please try again, you should get Accepted. sorry for the inconvenience.

  • 0

    You are right I think, I got the same problem.

  • 0

    It should be fixed already, today my similar python code got accepted: 220 / 220 test cases passed with runtime 56 ms.

Log in to reply

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