aboutsummaryrefslogtreecommitdiff
path: root/challenge-058/paulo-custodio/python/ch-1.py
blob: 915851c67714164102575e28437a2cb856b26948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3

# Challenge 058
#
# TASK #1 › Compare Version
# Reviewed by Ryan Thompson
# Compare two given version number strings v1 and v2 such that:
#
# If v1 > v2 return 1
# If v1 < v2 return -1
# Otherwise, return 0
# The version numbers are non-empty strings containing only digits, and the dot
# (“.”) and underscore (“_”) characters. (“_” denotes an alpha/development
# version, and has a lower precedence than a dot, “.”). Here are some examples:
#
#    v1   v2    Result
# ------ ------ ------
#   0.1 < 1.1     -1
#   2.0 > 1.2      1
#   1.2 < 1.2_5   -1
# 1.2.1 > 1.2_1    1
# 1.2.1 = 1.2.1    0
# Version numbers may also contain leading zeros. You may handle these how you
# wish, as long as it’s consistent.

import re
import sys

def split_version(v):
    m = re.search(r'^\d+', v)
    if not m:
        return False
    else:
        v_1 = int(m.group(0))
        if len(m.group(0)) == len(v):
            v_sep = ''
            v_rest = ''
        else:
            v_sep = v[len(m.group(0))]
            v_rest = v[len(m.group(0))+1:]
        return True, v_1, v_sep, v_rest

def compare_version(a, b):
    if a == b:
        return 0
    else:
        a_ok, a_1, a_sep, a_rest = split_version(a)
        b_ok, b_1, b_sep, b_rest = split_version(b)
        if not a_ok or not b_ok:
            return 0
        elif a_1 < b_1:
            return -1
        elif a_1 > b_1:
            return 1
        elif a_sep == b_sep:
            return compare_version(a_rest, b_rest)
        elif a_sep == '':
            return -1
        elif b_sep == '':
            return 1
        elif a_sep == '.':
            return -1
        elif b_sep == '.':
            return 1
        else:
            return compare_version(a_rest, b_rest)

a = sys.argv[1]
b = sys.argv[2]
print(compare_version(a,b))