In: Computer Science
#function which returns list of id's with hamming distance of
candidates whose hamming distance is less than equal to the
threshold k
def find(reference, candidates,threshold):
#answer list
ans=[]
#initialize id to 1
id=1
#iterate for all candidates
for s in candidates:
#to scalculate hamming distance of this candidate
hamming=0
#calculate hamming distance
for i in range(len(s)):
if s[i]!=reference[i]:
hamming+=1
#if hamming distance is less than threshold
if hamming<=threshold:
ans.append([id,hamming])
#move to next candidate
id+=1
#return answer
return ans
[[1, 2], [2, 3]]
[[2, 11], [3, 8], [6, 2], [7, 5]]
CODE and INPUT/OUTPUT
So if you have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.