2

I am trying to compare the similarities between 2 strings. I did it successfully by import SequenceMatcher

The result should only return 3 possibilities,

i) Good which means second string contains in the first string. Eg: smack and mac

ii) Almost good which means part of second string contained in first string. Eg: smack and smart . Both are 60% similar and should be classified as almost good.

iii)Bad which means none of the elements in second string contained in first string. Eg:smack and van

However, If I would like to perform the same task without importhing SequenceMatcher, how will I be able to perform it? I am stuck at writing an algorithm to slice the strings and compare it with another. Also, is there anyways to define the algorithm SequenceMatcher?

Here's a link to my code

Sa=input('enter something A: ')
Sb=input('enter something B: ')
Sc=Sa.lower()
Sd=Sb.lower()
from difflib import SequenceMatcher
if Sd in Sc or SequenceMatcher(a=Sc,b=Sd).ratio() ==1:
    print('Good') 

if Sd not in Sc and 0<SequenceMatcher(a=Sc,b=Sd).ratio() <1:
    print('Almost Good')

if SequenceMatcher(a=Sc,b=Sd).ratio() ==0:
    print('Bad')

Much appreciated!

sachin dubey
  • 755
  • 9
  • 28

1 Answers1

0

I used if-elif instead of If

Sa=raw_input('enter something A: ')
Sb=raw_input('enter something B: ')
Sc=Sa.lower()
Sd=Sb.lower()

count = 0
for val in Sc:
    for char in Sd:
        if char == val:
            count = count + 1

if count == 0:
    print("Bad")
elif Sc in Sd or Sd in Sc:
    print('Good')
else:
    print('Almost Good')
sachin dubey
  • 755
  • 9
  • 28
  • Thank you for your solution and edit. works perfectly! Thanks once again! – Marcus Khoo Nov 25 '17 at 12:57
  • may i know what should i do to limit the difference between 1 value of the string. The code you wrote returns almost good even for much and mars – Marcus Khoo Nov 28 '17 at 12:34
  • which means, at most difference in n number of value is allowed to be different – Marcus Khoo Nov 28 '17 at 12:35
  • I edited my post.Now check whether it is working correctly ? @MarcusKhoo – sachin dubey Nov 28 '17 at 13:04
  • unfortunately, your code will still print *almost good* even with 1 alphabet that is same. – Marcus Khoo Nov 28 '17 at 13:11
  • sorry ,But my code's output is exactly equal to your's.what should print if only one alphabet match ? @MarcusKhoo. – sachin dubey Nov 28 '17 at 13:28
  • Don't be sorry :) lets says there is 5 alphabets in both strings. Maximum number of difference allowed is only 1. So, 4/5 must be the same and according to sequence. For example *post* and *tops* they both are equally same alphabets. but it should be a bad string, because no alphabets are in the nth position of each other. – Marcus Khoo Nov 28 '17 at 13:41
  • if there's n number of alphabets in the string,n-1 number of alphabet must correspond in another string for the system to print almost good string. Any value under n-1 is considered bad string , because there is more than 1 difference – Marcus Khoo Nov 28 '17 at 13:45