How to check if two equal-length strings are at least 50% equal in Python

1 Answer

0 votes
def are_50_percent_equal(str1, str2):
    if str1 is None or str2 is None or len(str1) != len(str2):
        return False

    matching_chars = 0

    for i in range(len(str1)):
        if str1[i] == str2[i]:
            matching_chars += 1

    return (matching_chars / len(str1)) >= 0.5

str1 = "java c# c c++ python"
str2 = "java c# c r rust sql"

if are_50_percent_equal(str1, str2):
    print("yes")
else:
    print("no")


 
 
'''
run:
 
yes
 
'''

 



answered May 10, 2024 by avibootz

Related questions

...