Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def hamming_distance(name_A, name_B):
- count=0
- for i in range (len(name_A)):
- if name_A[i]!= name_B[i]:
- count +=1
- return count
- """ This function calculates and return the Hamming Distance between name_A and name_B"""
- def name_distance(name_A, name_B):
- list1=list(name_A)
- list2=list(name_B)
- if len(name_A)>len(name_B):
- min_array = [] #holds all hamming distances
- diffs = 0 #the hamming distance at each iteration
- #loops through number of times comparisons should be made,
- for i in range (len(name_A)-len(name_B)+1):
- #compares the index of strings and record hamming distance
- for j in range (len(name_B)):
- if list1[j]!= list2[j]:
- diffs +=1
- #pops the first letter of string with longer length so that correct comparison can be made
- list1.pop(0)
- #adds the hamming distance to array
- min_array.append(diffs)
- #resets hamming distance
- diffs=0
- alength = len(name_A)
- blength = len(name_B)
- #gets the smallest hamming distance
- diffs = min(min_array)
- return diffs + alength - blength
- if len(name_B)>len(name_A):
- min_array = []
- diffs = 0
- for i in range (len(name_B)-len(name_A)+1):
- for j in range (len(name_A)):
- if list1[j]!= list2[j]:
- diffs +=1
- list1.pop(0)
- min_array.append(diffs)
- diffs=0
- alength = len(name_A)
- blength = len(name_B)
- diffs = min(min_array)
- return diffs + blength - alength
- """This function calculates and return the distance between name_A and name_B where these names may be different length. """
- def get_name_lists(file_name):
- with open(file_name, 'r') as fr:
- return [name[:-1] for name in fr]
- def name_matching(filename, target, k):
- ret_list = []#holding all the names that would be returned
- for i in get_name_lists(filename):#all the names from the textfile
- if name_distance(target,i) < k: #checking if the distance is less than k
- ret_list.append(i)# if it is, add it to the list returned
- return ret_list
- """This function loads in a list of names from a file, compares the target name to each name, and returns a list of all names within a distance of k from the target name"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement