Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def alphanumericLess(s1, s2):
- def tokenizer(string):
- token = []
- i = 0
- while len(string)>0:
- if i == len(string):
- token.append(string[0:i])
- break
- if ord(string[i]) > ord('0')-1 and ord(string[i]) < ord('9')+1:
- i = i+1
- else:
- if i>0:
- token.append(string[0:i])
- token.append(string[i])
- string = string[i+1:]
- i = 0
- else:
- print(string[0:i])
- token.append(string[i])
- string = string[i+1:]
- i = 0
- return token
- def compare(a,b): #True if a > b
- anum = False
- bnum = False
- if ord(a[0]) > ord('0')-1 and ord(a[0]) < ord('9')+1:
- anum = True
- a = int(a)
- if ord(b[0]) > ord('0')-1 and ord(b[0]) < ord('9')+1:
- bnum = True
- b = int(b)
- if not anum and not bnum:
- return a > b
- if not anum and bnum:
- return True
- if anum and not bnum:
- return False
- else: # anum and bnum
- return a > b
- tok1 = tokenizer(s1)
- tok2 = tokenizer(s2)
- shorter = min(len(tok1),len(tok2))
- print(tok1)
- print(tok2)
- for i in range(shorter):
- a = tok1[i]
- b = tok2[i]
- if compare(a,b) and not compare(b,a):
- return False
- if compare(b,a) and not compare(a,b):
- return True
- # If there is a tie
- if len(tok1) == len(tok2):
- for i in range(shorter-1,-1,-1):
- if (tok1[i][0] == '0' or tok2[i][0] == '0') and int(tok1[i]) == int(tok2[i]):
- return len(tok1[i]) > len(tok2[i])
- # no tie, same elements up to len(shorter)
- return len(tok1) < len(tok2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement