Q: version compare error using python I have following python code which compares two versions v1 and v2 and outputs the following:
0 = version's are same
1 = v1 > v2
-1 = v2 > v1

code is as follows:
class Solution(object):
    def compareVersion(self, version1, version2):

        n1, n2 = len(version1), len(version2)
        i, j = 0, 0
        while i < n1 or j < n2:
            v1, v2 = 0, 0
            while i < n1 and version1[i] != '.':
                v1 = v1*10 + int(version1[i])
                i += 1
            while j < n2 and version2[j] != '.':
                v2 = v2*10 + int(version2[j])
                j += 1
            if v1 != v2:
                return 1 if v1 > v2 else -1
            i += 1
            j += 1

        return 0

if __name__ == "__main__":
    print (Solution().compareVersion("2.10", "2.06"))

The above code outputs the following because v1> v2.
1

However when I type
if __name__ == "__main__":
    print (Solution().compareVersion("2.1", "2.06"))

the output is 
-1`

How do I fix this here the output is incorrect due to missing zero.

A: Split the string at the dots and treat the numbers as integers:
>>> version_string = "2.06"
>>> version_as_integers = tuple(int(segment) for segment in version_string.split('.'))
>>> version_as_integers
(2, 6)

Then, just compare the versions:
if version_1_as_integers < version_2_as_integers:
    return -1
elif version_1_as_integers == version_2_as_integers:
    return 0
else:
    return 1

Complete solution:
def version_to_integers(version_string):
    return tuple(int(segment) for segment in version_string.split('.'))

def compare_versions(version_string_1, version_string_2):
    v1 = version_to_integers(version_string_1)
    v2 = version_to_integers(version_string_2)
    return ((v1 > v2) - (v1 < v2))

The last line is a trick from a codegolf.stackexchange.com question

A: Cast to a float instead of an int
>>> i = "2.10"
>>> j = "2.06"
>>> float(i) > float(j)
True
>>> i = "2.1"
>>> float(i) > float(j)
True


A: With a bit research and @zvone and @DeepSpace comments to the original post. I found out this:
Versions are not floating point numbers, hence they do not fall under category of decimal numbers, hence the arithmetic of decimal numbers won't apply to versioning. According to semantic versioning 2.0.0 (https://semver.org/#spec-item-2) a version number should not be followed by leading zeros. Hence 2.06 is a bad versioning way to go. It should have been2.1, 2.6 etc to begin with. If you follow semantic versioning 2.0.0 you can avoid this dilema.
