• + 1 comment

    After a lot of cheating, it's done

    def divide_and_compare(person_substring, virus):
        str_half_len = len(virus)//2
    # compare first half
        if (person_substring[0:str_half_len] == virus[0:str_half_len]):
            # if first half matched, the culprit is in second half 
            # since we know there is a mismatch here, 
    	# there is no need to compare
            # just make a recursive call with the second halfs,
    	# but what if strings are just 1 char long? 
            if (len(virus[str_half_len:]) == 1):
                return True
            else:
                return divide_and_compare(person_substring[str_half_len:], virus[str_half_len:])
    						
        # compare second half
    elif (person_substring[str_half_len:] == virus[str_half_len:]):
            # first half did not match but second half have matched 
    	# this means culprit is in first half
            if (len(virus[0:str_half_len]) == 1):
                return True
            else:
                return divide_and_compare(person_substring[0:str_half_len], virus[0:str_half_len])
        else:
            # print('# both half did not match, so we are safe')
            return False
    				
    def virusIndices(person, virus):
        match_list = list()
        person_len = len(person)
        virus_len = len(virus)
        if(virus_len > person_len):
            print('No Match!')
            return
        loop_range = person_len - virus_len + 1
        for p_index in range(loop_range):
            person_substring = person[p_index:p_index+virus_len]
            if (person_substring == virus):
                match_list.append(p_index)
                continue
            else:
                compare_result = divide_and_compare(person_substring, virus)
                if compare_result:
                    match_list.append(p_index)    
        if len(match_list) > 0:
            print(*match_list)
        else:
            print('No Match!')