Compare Version Numbers. Python TypeError: 'unicode' object is not callable


  • 0
    A

    Can anyone help me why my python code for this "Compare Version Numbers" problem returns a typeerror " 'unicode' object is not callable" in 29th line? Thank you!

    class Solution(object):
            def compareVersion(self, version1, version2):
                """
                :type version1: str
                :type version2: str
                :rtype: int
                """
                
                while version1 and version2:
                    v1 = self.digits(version1)
                    v2 = self.digits(version2)
                    if v1 > v2:
                        return 1
                    elif v1 < v2:
                        return -1
                    else:
                        version1 = self.cut(version1)
                        version2 = self.cut(version2)
                
                if version1 == "" and version2:
                    return -1
                elif version1 and version2 == "":
                    return 1
                else:
                    return 0
                        
            def digits(self, version):
                for i in range(len(version)):
                    if version(i) != '.':
                        i += 1
                    else:
                        return int(version[:i])
                return int(version)
            
            def cut(self, version):
                for i in range(len(version)):
                    if version(i) != '.':
                        i += 1
                    else:
                        return version[(i+1):len(version)]
                return ""

  • 0

    What's the 29th line?


  • 0
    A

    29th line: if version(i) != '.':

    seems I cannot compare a character to a dot?


  • 0

    No, it's exactly what the error message tells you. Your version is a string and you're trying to call it, which doesn't work. Change (i) to [i].


  • 0
    A

    Could you take a look at the 33rd line: return int(version)
    It hints "Line 33: ValueError: invalid literal for int() with base 10: '' "
    Thanks!


Log in to reply
 

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