Compare Version Numbers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def compareVersion(self, version1: str, version2: str) -> int:
        # Split the versions by '.' and convert to integers
        version1_parts = list(map(int, version1.split('.')))
        version2_parts = list(map(int, version2.split('.')))

        # Pad the shorter version with zeros to make them of equal length
        max_length = max(len(version1_parts), len(version2_parts))
        version1_parts += [0] * (max_length - len(version1_parts))
        version2_parts += [0] * (max_length - len(version2_parts))

        # Compare the corresponding parts
        for v1, v2 in zip(version1_parts, version2_parts):
            if v1 < v2:
                return -1
            elif v1 > v2:
                return 1

        # If all parts are equal, the versions are the same
        return 0