Python wrong answer?


  • 5
    I

    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

    [6306,9385,7536,3462,4798,5422,5529,8070,6241,9094,7846,663,6221,216,6758,8353,3650,3836,8183,3516,5909,6744,1548,5712,2281......]

    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
    G

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


  • 0
    Y

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


  • 0
    T

    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
    K

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


  • 0
    P

    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.