此任务的目标是找到满足以下所有要求的集合X:
X = {x | x∈Z且x> 0}。
- 醇>
A = {(x mod 2)| x∈X}
B = {y | sqrt(y)∈X}
C = {sqrt(z)| z∈(X∩B)}
D = {w ^ 2 | w∈C}
0< | X |
| X | ∈X。
| A | (不是元素) X。
sum(X)∈B。
sum(X∩B)6 (不是元素) B。
- 醇>
sum(C∪D)∈X。
所以这就是我试图解决的问题。
我尝试用手解决这个问题,发现X = {6,36,31,33,11,4}是一个符合要求的集合。我也用Python编写了代码,但我的代码不起作用。我没有收到任何错误消息,它只是输出了一个满足要求的集合。这是我用Python编写的代码:
import random
import math
def check(t):
X1 = t
X = set([x for x in X1 if x>0])
A = set([x%2 for x in X]) - set([0])
print "A= ", "\t", A
B = set([math.pow(y,2) for y in X ])
print "B= ", "\t", B
C = set([math.pow(z, 0.5) for z in X.intersection(B)])
print "C= ", "\t", C
D = set([math.pow(w, 2) for w in C])
print "D= ", "\t", D, "\n"
# (iii)
if len(X) >0:
print "iii.\t The cardinality of X bigger than 0."
# (iv)
if len(X) in X:
print "iv.\t\t The cardinality of X is element in X"
# (v)
if len(A) not in X:
print "v.\t\t Cardinality of A is not element in X."
# (vi)
if sum(X) in B:
print "vi.\t\t Sum of X is element in B."
# (vii)
if sum(X) and sum(B) not in X:
print "vii.\t Sum of X and B is not element in B"
# (viii)
if sum(C) in X or sum(D) in X:
print "viii.\t Sum of C exits in X."
if len(X)>0 and len(X) in X and len(A) not in X and sum(X) in B and sum(X) not in B and sum(B) not in B and sum(C) in X or sum(D) in X:
print "Set X is ","\t\t ", X
print "Fits the requirements."
# print "A = ", A
else:
print "Does not fit the requirements."
def guess():
b = 20
genSet = random.sample( range(2,b), 6 )
t = set(genSet)
print "---------------"
print "Generated set is: ", t
return check(t)
counter=1
while not guess():
print "try nr: ", teller
counter+=1
正如您可能已经注意到我刚刚假设该设置为长度= 6,但这只是暂时的,因为我试图让代码工作。由于同样的原因,我的随机数范围并不大。
我还在其间写了一些if / else语句,以检查是否满足要求。
答案 0 :(得分:0)
忽略我的评论:不知道我在想什么。只需设置
A = set([x%2 for x in X])
并确保return
来自您的函数check
:
...
print "Fits the requirements."
# print "A = ", A
return True
else:
print "Does not fit the requirements."
return False
...
您需要在比较中获得正确的逻辑(长if
语句):
import random
import math
def check(t):
X1 = t
X = set([x for x in X1 if x>0])
A = set([x%2 for x in X])
print "A= ", "\t", A
B = set([math.pow(y,2) for y in X ])
print "B= ", "\t", B
C = set([math.pow(z, 0.5) for z in X.intersection(B)])
print "C= ", "\t", C
D = set([math.pow(w, 2) for w in C])
print "D= ", "\t", D, "\n"
# (iii)
if len(X) >0:
print "iii.\t The cardinality of X bigger than 0."
# (iv)
if len(X) in X:
print "iv.\t\t The cardinality of X is element in X"
# (v)
if len(A) not in X:
print "v.\t\t Cardinality of A is not element in X."
# (vi)
print '(vi)', sum(X), B
if sum(X) in B:
print "vi.\t\t Sum of X is element in B."
# (vii)
if sum(X) and sum(B) not in X:
print "vii.\t Sum of X and B is not element in B"
# (viii)
if sum(C) in X or sum(D) in X:
print "viii.\t Sum of C exits in X."
if len(X)>0 and len(X) in X and len(A) not in X and sum(X) in B and sum(X.intersection(B)) not in B and sum(C.union(D)) in X:
print "Set X is ","\t\t ", X
print "Fits the requirements."
# print "A = ", A
return True
else:
print "Does not fit the requirements."
return False
def guess():
b = 20
genSet = random.sample( range(1,b), 6 )
t = set(genSet)
print "---------------"
print "Generated set is: ", t
return check(t)
通过这些修改,一个解决方案似乎是{1, 3, 4, 6, 9, 13}
。