# -*- coding: utf-8 -*-
__author__ = 'ypp'
import re
def versionCompare(v1="1.1.1", v2="1.2"):
v1_check = re.match("\d+(\.\d+){0,2}", v1)
v2_check = re.match("\d+(\.\d+){0,2}", v2)
if v1_check is None or v2_check is None or v1_check.group() != v1 or v2_check.group() != v2:
return "版本號格式不對,正確的應該是x.x.x,只能有3段"
v1_list = v1.split(".")
v2_list = v2.split(".")
v1_len = len(v1_list)
v2_len = len(v2_list)
if v1_len > v2_len:
for i in range(v1_len - v2_len):
v2_list.append("0")
elif v2_len > v1_len:
for i in range(v2_len - v1_len):
v1_list.append("0")
else:
pass
for i in range(len(v1_list)):
if int(v1_list[i]) > int(v2_list[i]):
return "v1大"
if int(v1_list[i]) < int(v2_list[i]):
return "v2大"
return "相等"
# 測試用例
print(versionCompare(v1="", v2=""))
print(versionCompare(v1="1.0.a", v2="d.0.1"))
print(versionCompare(v1="1.0.1", v2="1.0.1"))
print(versionCompare(v1="1.0.2", v2="1.0.1"))
print(versionCompare(v1="1.0.1", v2="1.0.2"))
print(versionCompare(v1="1.0.11", v2="1.0.2"))
設計思想:
1.使用正則表達式判斷版本號格式是否正確
2.將字符串用”.”分隔成數組
3.比較數組長度,將長度短的數組用“0”補齊成相等長度數組
4.逐個遍歷數組元素,比較大小
測試用例:
1.版本號為空
2.版本號含非數字
3.版本號長度不一致
4.版本號以點為分隔,逐位比較
