Created
March 18, 2021 07:43
-
-
Save yoshikakbudto/0f8a3347a900cdbcfb5509a0736c481d to your computer and use it in GitHub Desktop.
convert version numbers into one number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Convert version number of only digits into one number, ready for comparing. | |
""" | |
v1=(1,20,0,10) | |
v2=(1,20,1,0) | |
# length in bits of each version number. | |
# for example '8' allows to use version number with elements of a max 256: "256.256.256.256" | |
VER_ELEM_BITS=8 | |
def ver_to_num(a,b,c,d): | |
res = (a<<(VER_ELEM_BITS*3)) + (b<<(VER_ELEM_BITS*2)) + (c<<VER_ELEM_BITS) + d | |
return res | |
v1=ver_to_num(*v1) | |
v2=ver_to_num(*v2) | |
if v2 > v1: | |
print('ok',v2) | |
elif v2 == v1: | |
print('equal',v2) | |
else: | |
print('v2 should be greater',v2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment