python - Algorithm to reduce a software version? -
i've been struggling problem in last few hours , couldn't figure out solution myself. problem simple:
in: 'subtract('1.1.5', 4)' out: '1.1.1' in: 'subtract('1.1.5', 6)' out: '1.0.9' in: 'subtract('1.0.0', 1)' out: '0.9.9' in: 'subtract('1.14.5', 8)' out: '1.13.7' in: 'subtract('1.10.10', 20)' out: '1.9.0'
i've come couple of complex algorithms complex seems simple solve, ideas?
this i've tried far (not working cases):
def subtract(version, sub): arrv = version.split('.') index = len(arrv) - 1 sub = -abs(sub) while(sub < 0 , index >= 0): vlen = len(arrv[index]) m = 10 ** (vlen - 1) = int(arrv[index]) if (i + sub < 0): arrv[index] = str(m - (abs(sub) % m)) sub = int(sub / m) else: arrv[index] = str(i + sub) sub = 0 index -= 1 print(arrv) return '.'.join(arrv) print(subtract('1.10.10', 5) == '1.10.5') print(subtract('1.10.10', 12) == '1.9.8') print(subtract('1.10.10', 10) == '1.10.0') print(subtract('1.10.10', 1010) == '1.0.0')
Comments
Post a Comment