我如何编写满足此集合所有要求的代码? [英] How can I write a code that fulfill all the requirements for this set?

查看:44
本文介绍了我如何编写满足此集合所有要求的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此任务的目标是找到满足以下所有要求的集合 X:

<块引用>

  1. X = {x |x ∈ Z 且 x > 0}.

  2. A = {(x mod 2) |x∈X}

B = {y |sqrt(y) ∈ X}

C = {sqrt(z) |z ∈ (X ∩ B)}

D = {w^2|w∈C}

  1. 0 <|X|.

  2. |X|∈ X.

  3. |A|(不是元素在) X.

  4. sum(X)∈B.

  5. sum(X ∩ B) 6 (非元素在) B.

  6. sum(C ∪ D) ∈ X.

这就是我要解决的问题.

我试过手工解决这个问题,发现 X = {6, 36, 31, 33, 11, 4} 是一个符合要求的集合.我也用 Python 编写了代码,但我的代码不起作用.我没有收到任何错误消息,它只是不会输出满足要求的集合.这是我用 Python 编写的代码:

随机导入导入数学定义检查(t):X1 = tX = set([x for x in X1 if x>0])A = set([x%2 for x in X]) - set([0])打印 "A= ", "\t", AB = set([math.pow(y,2) for y in X ])打印 "B= ", "\t", BC = set([math.pow(z, 0.5) for z in X.intersection(B)])打印 "C= ", "\t", CD = set([math.pow(w, 2) for w in C])打印 "D= ", "\t", D, "\n"# (iii)如果 len(X) > 0:打印iii.\t X 大于 0 的基数."# (四)如果 len(X) 在 X 中:print "iv.\t\t X 的基数是 X 中的元素"# (五)如果 len(A) 不在 X 中:打印v.\t\t A 的基数不是 X 中的元素."# (六)如果 sum(X) 在 B 中:打印 "vi.\t\t X 的总和是 B 中的元素."#(七)如果 sum(X) 和 sum(B) 不在 X 中:打印 "vii.\t X 和 B 的总和不是 B 中的元素"#(八)如果 X 中的 sum(C) 或 X 中的 sum(D):打印 "viii.\t Sum of C exits in X."如果 len(X)>0 并且 len(X) 在 X 和 len(A) 不在 X 和 sum(X) 在 B 和 sum(X) 不在 B 和 sum(B) 在 B 和 sum(C)) 在 X 或 sum(D) 在 X:print "Set X is ","\t\t ", X打印符合要求".# 打印 "A = ", A别的:打印不符合要求."定义猜测():乙 = 20genSet = random.sample( range(2,b), 6 )t = 集(发电机组)打印 " -  -  -  -  -  -  - -"print "生成集为:", t退货支票(t)计数器=1而不是猜测():打印尝试编号:",出纳员计数器+=1

您可能会注意到,我刚刚假设该集合的长度为 6,但这只是现在,因为我正在尝试使代码正常工作.出于同样的原因,我的随机数范围也没有那么大.

我还在中间写了一些 if/else 语句来检查是否满足要求.

解决方案

忽略我的评论:不知道我在想什么.简单设置

A = set([x%2 for x in X])

并确保你从你的函数checkreturn一些东西:

<代码> ...打印符合要求".# 打印 "A = ", A返回真别的:打印不符合要求."返回错误...

并且您需要在比较中获得正确的逻辑(长 if 语句):

随机导入导入数学定义检查(t):X1 = tX = set([x for x in X1 if x>0])A = set([x%2 for x in X])打印 "A= ", "\t", AB = set([math.pow(y,2) for y in X ])打印 "B= ", "\t", BC = set([math.pow(z, 0.5) for z in X.intersection(B)])打印 "C= ", "\t", CD = set([math.pow(w, 2) for w in C])打印 "D= ", "\t", D, "\n"# (iii)如果 len(X) > 0:打印iii.\t X 大于 0 的基数."# (四)如果 len(X) 在 X 中:print "iv.\t\t X 的基数是 X 中的元素"# (五)如果 len(A) 不在 X 中:打印v.\t\t A 的基数不是 X 中的元素."# (六)打印 '(vi)', sum(X), B如果 sum(X) 在 B 中:打印 "vi.\t\t X 的总和是 B 中的元素."#(七)如果 sum(X) 和 sum(B) 不在 X 中:打印 "vii.\t X 和 B 的总和不是 B 中的元素"#(八)如果 X 中的 sum(C) 或 X 中的 sum(D):打印 "viii.\t Sum of C exits in X."如果 len(X)>0 和 len(X) 在 X 和 len(A) 不在 X 和 sum(X) 在 B 和 sum(X.intersection(B)) 不在 B 和 sum(C.union(D)) 在 X 中:print "Set X is ","\t\t ", X打印符合要求".# 打印 "A = ", A返回真别的:打印不符合要求."返回错误定义猜测():乙 = 20genSet = random.sample( range(1,b), 6 )t = 集(发电机组)打印 " -  -  -  -  -  -  - -"print "生成的集合是:", t退货支票(t)

通过这些编辑,一种解决方案似乎是 {1, 3, 4, 6, 9, 13}.

The objective of this task is to find the set X that fulfill all of the following requirements:

  1. X = {x | x ∈ Z and x > 0}.

  2. A = {(x mod 2) | x ∈ X}

B = {y |sqrt(y) ∈ X}

C = {sqrt(z) | z ∈ (X ∩ B)}

D = {w^2| w ∈ C}

  1. 0 < |X|.

  2. |X| ∈ X.

  3. |A| (not element in) X.

  4. sum(X) ∈ B.

  5. sum(X ∩ B) 6 (not element in) B.

  6. sum(C ∪ D) ∈ X.

So this is the problem I'm trying to solve.

I've tried solving this by hand and found that X = {6, 36, 31, 33, 11, 4} is a set that fits the requirements. I've written a code in Python as well but my code isn't working. I'm not getting any error messages, it just won't output a set that fulfill the requirements. This is the code I've written in 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

As you may notice I've just assumed the set to be of length=6, but that's just for now as I'm trying to get the code to work. And my range of random number isn't that big either for the same reason.

I've also written some if/else-statements in between to check whether the requirements are fulfillled or not.

解决方案

Ignore my comments: don't know what I was thinking. Simply set

A = set([x%2 for x in X])

and make sure you return something from your function check:

    ...
    print "Fits the requirements."
    # print "A = ", A
    return True
else:
    print "Does not fit the requirements."
    return False
...

And you need to get the logic right in your comparison (the long if statement):

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)

With these edits, one solution appears to be {1, 3, 4, 6, 9, 13}.

这篇关于我如何编写满足此集合所有要求的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆