Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. def alphanumericLess(s1, s2):
  2.  
  3. def tokenizer(string):
  4. token = []
  5. i = 0
  6. while len(string)>0:
  7. if i == len(string):
  8. token.append(string[0:i])
  9. break
  10.  
  11. if ord(string[i]) > ord('0')-1 and ord(string[i]) < ord('9')+1:
  12. i = i+1
  13. else:
  14. if i>0:
  15. token.append(string[0:i])
  16. token.append(string[i])
  17. string = string[i+1:]
  18. i = 0
  19. else:
  20. print(string[0:i])
  21. token.append(string[i])
  22. string = string[i+1:]
  23. i = 0
  24. return token
  25.  
  26. def compare(a,b): #True if a > b
  27. anum = False
  28. bnum = False
  29.  
  30. if ord(a[0]) > ord('0')-1 and ord(a[0]) < ord('9')+1:
  31. anum = True
  32. a = int(a)
  33. if ord(b[0]) > ord('0')-1 and ord(b[0]) < ord('9')+1:
  34. bnum = True
  35. b = int(b)
  36.  
  37. if not anum and not bnum:
  38. return a > b
  39. if not anum and bnum:
  40. return True
  41. if anum and not bnum:
  42. return False
  43. else: # anum and bnum
  44. return a > b
  45.  
  46.  
  47. tok1 = tokenizer(s1)
  48. tok2 = tokenizer(s2)
  49.  
  50. shorter = min(len(tok1),len(tok2))
  51.  
  52. print(tok1)
  53. print(tok2)
  54.  
  55. for i in range(shorter):
  56. a = tok1[i]
  57. b = tok2[i]
  58. if compare(a,b) and not compare(b,a):
  59. return False
  60. if compare(b,a) and not compare(a,b):
  61. return True
  62.  
  63. # If there is a tie
  64. if len(tok1) == len(tok2):
  65. for i in range(shorter-1,-1,-1):
  66. if (tok1[i][0] == '0' or tok2[i][0] == '0') and int(tok1[i]) == int(tok2[i]):
  67. return len(tok1[i]) > len(tok2[i])
  68.  
  69. # no tie, same elements up to len(shorter)
  70. return len(tok1) < len(tok2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement