37,719
社区成员
发帖
与我相关
我的任务
分享
import itertools
REPEAT=4
def dist(v1,v2):
return len([1 for i in range(len(v1)) if v1[i]!=v2[i]])
def save_vec(result,i,f_object):
result.append(i)
print(i)
f_object.write(str(i)+"\n")
with open("result"+str(REPEAT)+".txt",'w') as f_object:
result=[]
for i in itertools.product(range(4), repeat=REPEAT):
if result==[]:
save_vec(result,i,f_object)
else:
flag=True
for r in result:
if(dist(r,i)<3):
flag=False
break
if flag:
save_vec(result, i, f_object)
(0, 0, 0, 0)
(0, 1, 1, 1)
(0, 2, 2, 2)
(0, 3, 3, 3)
(1, 0, 1, 2)
(1, 1, 0, 3)
(1, 2, 3, 0)
(1, 3, 2, 1)
(2, 0, 2, 3)
(2, 1, 3, 2)
(2, 2, 0, 1)
(2, 3, 1, 0)
(3, 0, 3, 1)
(3, 1, 2, 0)
(3, 2, 1, 3)
(3, 3, 0, 2)
import itertools
REPEAT=4
def dist(v1,v2):
return len([1 for i in range(len(v1)) if v1[i]!=v2[i]])
print(dist(('0', '1', '2', '2'),('0', '0', '0', '1')))
def save_vec(result,i,f_object):
result.append(i)
print(i)
f_object.write(str(i)+"\n")
with open("result"+str(REPEAT)+".txt",'w') as f_object:
result=[]
for i in itertools.product('0123', repeat=REPEAT):
if result==[]:
save_vec(result,i,f_object)
else:
flag=True
for r in result:
if(dist(r,i)<3):
flag=False
break
if flag:
save_vec(result, i, f_object)
REPEAT=4时的结果如下:
('0', '0', '0', '0')
('0', '1', '1', '1')
('0', '2', '2', '2')
('0', '3', '3', '3')
('1', '0', '1', '2')
('1', '1', '0', '3')
('1', '2', '3', '0')
('1', '3', '2', '1')
('2', '0', '2', '3')
('2', '1', '3', '2')
('2', '2', '0', '1')
('2', '3', '1', '0')
('3', '0', '3', '1')
('3', '1', '2', '0')
('3', '2', '1', '3')
('3', '3', '0', '2')