3

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.

CK24616
  • 39
  • 4
  • 2
    Let’s start by assuming that this isn’t a Python bug and is a problem in your code: what have you tried to debug this? Have you used a debugger or adding simple print statements to show the strings being compared, and the result from using `.find()`? – DisappointedByUnaccountableMod Jan 11 '21 at 07:53

1 Answers1

0

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.

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.

a121
  • 798
  • 4
  • 9
  • 20
CK24616
  • 39
  • 4