Cheating in python with cmp()


  • 1
    J

    The cmp() built-in has the ability to compare arrays already, so hey, as long it's python 2...

    IMPORTANT NOTE: The cmp() function has been removed from python 3! In this context, this code gave me success when I submitted it as a solution.

    class Solution:
        # @param version1, a string
        # @param version2, a string
        # @return an integer
        def compareVersion(self, version1, version2):
            v1 = map(int,version1.split("."))
            v2 = map(int,version2.split("."))
            v1.extend([0] * (len(v2) - len(v1)))
            v2.extend([0] * (len(v1) - len(v2)))
            return cmp(v1, v2)

  • 0
    B

    wow this is pretty neat :)


  • 0
    B

    Wonderful solution! By the way, we don't really need cmp:

    class Solution:
        # @param version1, a string
        # @param version2, a string
        # @return an integer
        def compareVersion(self, version1, version2):
            v1 = map(int,version1.split("."))
            v2 = map(int,version2.split("."))
            v1.extend([0] * (len(v2) - len(v1)))
            v2.extend([0] * (len(v1) - len(v2)))
            return 0 if v1 == v2 else {True: 1, False:-1}[v1 > v2]

Log in to reply
 

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