We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defdivide_and_compare(person_substring,virus):str_half_len=len(virus)//2# compare first halfif(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):returnTrueelse:returndivide_and_compare(person_substring[str_half_len:],virus[str_half_len:])# compare second halfelif(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 halfif(len(virus[0:str_half_len])==1):returnTrueelse:returndivide_and_compare(person_substring[0:str_half_len],virus[0:str_half_len])else:# print('# both half did not match, so we are safe')returnFalsedefvirusIndices(person,virus):match_list=list()person_len=len(person)virus_len=len(virus)if(virus_len>person_len):print('NoMatch!')returnloop_range=person_len-virus_len+1forp_indexinrange(loop_range):person_substring=person[p_index:p_index+virus_len]if(person_substring==virus):match_list.append(p_index)continueelse:compare_result=divide_and_compare(person_substring,virus)ifcompare_result:match_list.append(p_index)iflen(match_list)>0:print(*match_list)else:print('NoMatch!')
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Save Humanity
You are viewing a single comment's thread. Return to all comments →
After a lot of cheating, it's done