jhylands

string_equivilence.py

Feb 7th, 2021
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. BREAK = "|"
  2. break_charactors = ["_", "-", " "]
  3.  
  4. def eq(string1, string2):
  5.     # I think we can look through the different cases that we have here
  6.     # So cleary string1===string2 => True
  7.     if string1==string2:
  8.         return True
  9.     # But then we want to use more inteligence to work out what we are dealing with
  10.     # I think the next thing we should do is to beak the text appart into numeric, symbolic and alphabetical, keeping the order
  11.  
  12.     if break_down(string1) == break_down(string2):
  13.         return True
  14.     if break_down(string1).replace(BREAK, "") == break_down(string2).replace(BREAK, ""):
  15.         return True
  16.     print(break_down(string1))
  17.     print(break_down(string2))
  18.     return False
  19.  
  20. def xor(b1, b2):
  21.     return bool(b1) != bool(b2)
  22.  
  23. def issymbol(char):
  24.     return not (char.isalnum() or char in break_charactors)
  25.  
  26. def break_down(string):
  27.     # I would say we have a few classes here
  28.     #divider type \s _ \l\u  a break from alphabetical
  29.     #alphabetical
  30.     #symbolic
  31.     #numeric
  32.     acc = ""
  33.     for last_char, this_char in zip(BREAK + string[:-1],string):
  34.         # what we want here is if the previous char was upper and this is lower to create a break
  35.         if last_char.islower() and this_char.isupper():
  36.             #insert a break before this char
  37.             acc += BREAK + this_char.lower()
  38.             continue
  39.         if last_char in break_charactors:
  40.             acc += BREAK + this_char
  41.             continue
  42.         # might want to condition this on is alphanumeric
  43.         if last_char.isalnum() and this_char.isalnum() and xor(last_char.isnumeric(), this_char.isnumeric()):
  44.             acc += BREAK + this_char
  45.             continue
  46.         if this_char.isalnum():
  47.             acc += this_char
  48.     return acc.lower()
  49.  
Advertisement
Add Comment
Please, Sign In to add comment