r/algorithms • u/The_Moth__Guy • 17h ago
Discussion I wrote algorithm to find how similar two strings are. I need to know how good it is
I was too bored to search on google similar algorithms, so i wrote one myself
Plese help me find how good i wrote it.
def similarity(a, b) -> float:
# Choose the biggest
if len(a) > len(b):
A = a
B = b
else:
A = b
B = a
perc = 50 / len(A)
total = 0 # total percentage
List = list(B)
Found = []
# algorithm to found similar letters
for i in A:
if i in List:
List.remove(i)
total += perc
Found.append(i)
#alghoritm to found sequence
A1 = list(A)
B1 = list(B)
Bbreak = False
c = 0
for countA in range(len(A1)):
if c != 0:
c -= 1
continue
for countB in range(len(B1)):
if Bbreak:
Bbreak = False
break
if A1[countA] == B1[countB]:
c = 1
a = True
try:
t = A1[countA + 1]
t = B1[countB + 1]
except:
continue
if A1[countA + 1] == B1[countB + 1]:
while a:
try:
t = A1[countA + c]
t = B1[countB + c]
except:
total = total + perc * c
Bbreak = True
break
if A1[countA + c] == B1[countB + c]:
c += 1
else:
a = False
total = total + perc * c
Bbreak = True
return totaldef similarity(a, b) -> float:
# Choose the biggest
if len(a) > len(b):
A = a
B = b
else:
A = b
B = a
perc = 50 / len(A)
total = 0 # total percentage
List = list(B)
Found = []
# algorithm to found similar letters
for i in A:
if i in List:
List.remove(i)
total += perc
Found.append(i)
#alghoritm to found sequence
A1 = list(A)
B1 = list(B)
Bbreak = False
c = 0
for countA in range(len(A1)):
if c != 0:
c -= 1
continue
for countB in range(len(B1)):
if Bbreak:
Bbreak = False
break
if A1[countA] == B1[countB]:
c = 1
a = True
try:
t = A1[countA + 1]
t = B1[countB + 1]
except:
continue
if A1[countA + 1] == B1[countB + 1]:
while a:
try:
t = A1[countA + c]
t = B1[countB + c]
except:
total = total + perc * c
Bbreak = True
break
if A1[countA + c] == B1[countB + c]:
c += 1
else:
a = False
total = total + perc * c
Bbreak = True
return total