我正在努力应对这一挑战:Challenge #6
我有一个工作二进制和汉明距离函数,但二进制函数刚停止工作。它给了我一个
"ValueError: Unknown format code 'b' for object of type 'str'"
错误代码由于某种原因。这是我用于两者的代码......
# returns only 1's and 0's of binary for int n
def binary(n):
return '{0:08b}'.format(n)
# returns hamming distance of s1 and s2
def Hamm(s1, s2):
d = 0 # number of differences between s1 and s2 binary
for c1, c2 in zip(s1, s2): # compares s1[x] and s2[x]
if c1 != c2:
for a, b in zip(binary(c1), binary(c2)):
if a != b:
d += 1 # if 1 or 0 do not match up, d = d + 1
print(d)
答案 0 :(得分:1)
UNION
可以是字符串,而不是int - 即。
n
你得到错误。
所以你需要'{0:08b}'.format("1")
int()