algorithm-in-python/math/numberTheory/hammingDistance.py

64 lines
1.7 KiB
Python
Raw Normal View History

#coding: utf-8
''' mbinary
#######################################################################
# File : hammingDistance.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
2019-01-31 12:09:46 +08:00
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-12-17 17:36
# Description:
hamming distance is the number of different position (or binary bit for number) of two strings.
eg 'abac', 'ab': 2 1010,11 : 3
#######################################################################
'''
2020-04-15 12:28:20 +08:00
def hammingDistance(a, b):
if isinstance(a, int):
n = a ^ b
ct = 0
while n:
2020-04-15 12:28:20 +08:00
ct += n % 2
n >>= 1
return ct
else:
2020-04-15 12:28:20 +08:00
n, m = len(a), len(b)
ret = 0
2020-04-15 12:28:20 +08:00
for i, j in zip(a, b):
ret += i == j
return ret+abs(n-m)
2020-04-15 12:28:20 +08:00
def totalHammingDistance(lst):
'''return sum of any two items(num or lst( str)) in lst'''
2020-04-15 12:28:20 +08:00
length = len(lst)
if length == 0:
return 0
if isinstance(lst[0], int):
bits = [0] * len(bin(max(lst)))
for n in lst:
ct = 0
while n:
2020-04-15 12:28:20 +08:00
if n % 2 == 1:
bits[ct] += 1
ct += 1
n >>= 1
return sum(i*(length-i) for i in bits)
else:
2020-04-15 12:28:20 +08:00
mx = len(max(lst, key=len))
position = [dict() for i in range(mx)]
for li in lst:
2020-04-15 12:28:20 +08:00
for i, x in enumerate(li):
if x in position[i]:
2020-04-15 12:28:20 +08:00
position[i][x] += 1
else:
2020-04-15 12:28:20 +08:00
position[i][x] = 1
ret = 0
for dic in position:
left = length
for i in dic.values():
2020-04-15 12:28:20 +08:00
ret += i*(left-i) # key step
left -= i # key step
return ret