How can I write a code that fulfill all the requirements for this set?

妖精的绣舞 提交于 2019-12-08 13:55:40

问题


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.


回答1:


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}.



来源:https://stackoverflow.com/questions/28440271/how-can-i-write-a-code-that-fulfill-all-the-requirements-for-this-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!