Is there a wrong implementation in the code or just bug in Python?

Good Day,

This is the snippet of the code that will be used to test 100,000 characters of a string of another string of 1 to 100,000.(Cannot compare string 1 by 1 as this will be very tedious in big data). Again, as this is just a snippet of the code, it should compare the 3 characters and will return a True if only one character is different and False if more than 1 character is different.

def compare_three_characters(string, substring):
    first_half_string = string[:len(string) // 2]
    second_half_string = string[len(string) // 2:]

    first_half_substring = substring[:len(substring) // 2]
    second_half_substring = substring[len(substring) // 2:]

    # print("x", first_half_string, second_half_string, first_half_substring, second_half_substring)
    if first_half_string.find(first_half_substring) == -1 and second_half_string.find(second_half_substring) == -1:  # both halves are wrong
        return False
    else:
        compare_two_characters_if_only_one_character_is_different(second_half_string, second_half_substring)

def compare_two_characters_if_only_one_character_is_different(string, substring): # returns true if only character is different
    first_character_of_string = string[:1]
    second_character_of_string = string[1:]

    first_character_of_substring = substring[:1]
    second_character_of_substring = substring[1:]
    print(first_character_of_string, second_character_of_string,first_character_of_substring,second_character_of_substring)
    if first_character_of_string.find(first_character_of_substring) == -1:
        if second_character_of_string.find(second_character_of_substring) == -1:
            return False
    else:
        return True

# This one give a wrong result
if compare_three_characters("aaa", "aaa"):
    print("True")
else:
    print("False")

#################THIS ONE HAS CORRECT OUTPUT AND RETURNS TRUE
# if compare_two_characters_if_only_one_character_is_different("aa", "aa"):
#     print("True")
# else:
#     print("False")

Notice that if forced to use compare_two_characters_if_only_one_character_is_different("aa", "aa"), it gives correct value as compared to compare_three_characters("aaa", "aaa").

What could have been done wrong or this is a Python bug?

This is a snippet for the solution for a certain challenge question involving a very long string for comparison, and characters can vary to include all letters in alphabet.

1 answer

  • answered 2021-01-11 08:22 CK24616

    Just for those who might have encountered the same error, I may have found the cause of why it has this result.

    In def compare_three_characters(string, substring): function, instead of,

       else:
           compare_two_characters_if_only_one_character_is_different(second_half_string, second_half_substring)
    

    It should be,

    else:
        return compare_two_characters_if_only_one_character_is_different(second_half_string, second_half_substring)
    

    We need to add the explicit "return" keyword in the command instead of just relying on the return statement of the subsequent "def compare_two_characters_if_only_one_character_is_different" function.

    Hope this helps, please comment.

    Actually, this is recursive before, not limited to three characters but just a single function, but I tried to separate conditions to pinpoint where the cause of the problem is.

    Will try later to use the recursion approach. Thank you.