哪些值导致这些函数在python中返回True?

时间:2014-11-21 03:00:32

标签: python

对于以下每个函数,查找将使函数返回True的参数值。展示你的作品。提示:理由通过这个。只考虑可能的输入。例如,如果给出x + y == 16且x> y> 0,那么我们只需要考虑(15,1),(14,2),...,(9,7)。这总共只有7(x,y)个点,其中一个必须是正确的!

def f2(x):
    return ((type(x) != type(int(x))) and ((x % 1)**2 == int(x % 1)) and
            (100 > x**2 > 8*x > 0))

def f6(x):
    assert ((type(x) == int) and (1000 > x > 0))
    y = x % 10*100 + x/10%10*10 + x/100
    return ((x == y) and (int(x**0.5) == 11) and (x / 125) > (x / 135))

2 个答案:

答案 0 :(得分:1)

'我会帮助你,因为问题看起来很有趣:

def f2 (x):
    return ((type(x) != type(int(x))) and
        # type of x is not the same as the type of (int) x
        # since it has to be a number (otherwise it'll blow up),
        # this implies that x is a decimal
        ((x % 1)**2 == int(x % 1)) and
        # (x mod 1) squared equals the integer part of (x mod 1)
        # x has to be a whole number, i.e., 100.0 or 99.0 etc. etc.
        # Let's see why. Try 8.5
        # (8.5 % 1)**2 == (0.5 ** 2) == 0.25
        # this does not equal int(8.5 % 1) == int(0.5) == 0
        (100 > x**2 > 8*x > 0))
        # if x squared is greater than 8*x, x must be greater than 8,
        # but less than 13. This leaves us with 9.0, 10.0, 11.0, and 12.0,
        # since we know that x has to be a decimal of the form n.0
        # 9.0 returns True

def f6 (x):
    # x is an integer and (0,1000) exclusive
    # throws error otherwise
    assert ((type(x) == int) and (1000 > x > 0))
    y = ((x % 10) * 100) + x/10%10*10 + x/100
    # y is
    # (x mod 10) * 100 PLUS
    # ((x/10)%10)*10 PLUS
    # (x/100)
    return ((x == y) and (int(x ** 0.5) == 11) and (x / 125) > (x / 135))
    # x equals y
    # the integer value of the square root of x is 11
    # this puts x between 121 and 143 inclusive
    # x/125 (int) is greater than x/135 (int)
    # this puts x between 125 and 134

不要手动检查,所以让我们使用for循环:

for x in range(0,10):
    print(str(x+125) + " " + str(f6(x+125)))

# prints
125 False
126 False
127 False
128 False
129 False
130 False
131 True
132 False
133 False
134 False

答案 1 :(得分:0)

第1部分: F2(x)的

((type(x) != type(int(x)))

" X"不能是int,而是支持转换为int。因此," x" 浮动字符串的形式

((x % 1)**2 == int(x % 1))

模数运算符返回第一个和第二个数之间的余数。在我们的例子中,它将返回小数点后的数字部分。从第二部分开始" x%1"将返回-1和1之间的数字。" int()"该结果将返回-1,0或1.然后我们可以说

    ((x % 1)**2 == -1, 0, or 1)

进一步简化

    ((x % 1) == sqrt(-1), 0, or sqrt(1))
    ((x % 1) == sqrt(-1), 0, or 1)  <--- x % 1 cannot return "1.0" so
    ((x % 1) == sqrt(-1), 0)

小数位后面的数字必须是&#34; sqrt(-1)&#34;或&#34; 0&#34;。作为&#34; sqrt(-1)&#34;是一个想象的数字,在我的理解范围之外,如何用它来操纵整数,我想我们可以轻松地说:

小数位后的数字必须为&#34; 0&#34;。

继续前进

    (100 > x**2 > 8*x > 0))
    8*x > 0
    x > 0

    100 > x**2
    10 > x

    min: x = 0
    max: x = 10

    x**2 > 8*x
    x > 8

因此,为了满足第三个条件:

    10 > x > 8

要将TRUE返回到所有三个初始条件,我们得到:

x = 9.0

第2部分: F6(X):

我们的第一个假设是&#34; x&#34;是一个整数,因此&#34; x&#34;

没有小数点

首先我会跳过&#34; y&#34;至于返回TRUE

"x == y"

我们稍后会处理此事

我们的下一个条件是

(int(x**0.5) == 11)

有一些工作会产生

11.5 > sqrt(x) > 11
132.25 >= x >= 121

最终条件

    (x / 125) > (x / 135)

相当合理。它更好理解:

    125*(x / 125) > 125*(x / 135)
    x > 125*(x / 135)
    x > (125/135)*x
    x > 0.92592x 

所有实数都应满足该条件

满足所有三个条件

1) x is an int
2) x satisfies that big equation
3) 132.25 >= x >= 121
4) x is a real number

满足1,3和4:

x = 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132

通过这个大方程运行并检查&#34; x == y&#34;产生

x = 131