Skip to content

Instantly share code, notes, and snippets.

@eribeiro
Created November 29, 2016 18:55
Show Gist options
  • Save eribeiro/a56d896ab0dda992e48b45ef848e270a to your computer and use it in GitHub Desktop.
Save eribeiro/a56d896ab0dda992e48b45ef848e270a to your computer and use it in GitHub Desktop.
Hamming Distance
#!/usr/bin/python
def ham(x,y):
cont = 0
if len(x) == len(y):
for i in zip(x,y):
if i[0] != i[1]:
cont += 1
else:
cont = -1
return cont
def get_entrada():
lista = []
for i in range(0, int(raw_input())):
lista.append(raw_input())
return lista
if __name__ == "__main__":
lista1 = get_entrada()
lista2 = get_entrada()
lista = []
for s1 in lista2:
t = (-1, None)
for s2 in lista1:
d = ham(s1,s2)
# descarta valores invalidos
if d < 0:
continue
if t[0] < 0 or d < t[0]:
t = (d, s2)
if t[0] != -1:
lista.append(t)
for i in lista:
print "%s - %s " % (i[1], i[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment