[英]Python Homework - Not making sense
OK, our professor explained (kinda) this problem, but it still doesn't make much sense. 好吧,我们的教授解释了(有点)这个问题,但它仍然没有多大意义。
Question: Implement the function knice(f,a,b,k)
that will return 1 if for some integer a <= x <= b
and some integer n <= k
, n applications of f
on x will be x, (eg f(f(f...(f(x)))) = x
) and 0 if not. 问题:实现函数knice(f,a,b,k)
将返回1,如果对于某个整数a <= x <= b
和一些整数n <= k
,N的应用f
x上是x,(例如f(f(f...(f(x)))) = x
),否则为0。
What the professor provided was: 教授提供的是:
def knice(f,a,b,k):
f(f(f(...(f(x)))) = x
for i = a to b:
y = f(i)
if y = i break
for j = z to k:
y = f(y)
if y = i break
Personally, that example makes no sense to me, so looking to see if I can get clarification. 就个人而言,这个例子对我来说毫无意义,所以想看看我是否能得到澄清。
OP EDIT 1/19/2012 3:03pm CST OP EDIT 1/19/2012 3:03 pm CST
This is the final function that was figured out with the help of the GTA: 这是在GTA的帮助下计算出来的最终功能:
def f(x):
return 2*x-3
def knice(f,a,b,k):
x = a
while x <= b:
n = 1
y = f(x)
if y == x:
return 1
while n <= k:
y = f(y)
n=n+1
if y == x:
return 1
x=x+1
return 0
Ignore his code; 忽略他的代码; you should write whatever you feel comfortable with and work out the kinks later. 你应该写下你觉得舒服的东西,然后再解决问题。
You want to work out whether 你想弄清楚是否
f(a) = a
, or f(f(a)) = a
, or ..., or f^n(a) = a
, or , f(a) = a
,或f(f(a)) = a
,或......,或f^n(a) = a
, 或 f(a+1) = a+1
, or f(f(a+1)) = a+1
, or ..., or f^n(a+1) = a+1
, or , f(a+1) = a+1
,或f(f(a+1)) = a+1
,或......,或f^n(a+1) = a+1
, 或者 , f(b) = b
, or f(f(b)) = b
, or ..., or f^n(b) = b
. f(b) = b
,或f(f(b)) = b
,或......,或f^n(b) = b
。 An obvious algorithm should come to mind immediately: try all these values one-by-one! 应立即想到一个明显的算法:逐个尝试所有这些值! You will need two (nested) loops, because you are iterating over a rectangle of values. 您将需要两个(嵌套)循环,因为您正在迭代一个值的矩形。 Can you now see what to do? 你现在可以看看该怎么办了?
Yeah, I can see why that might be confusing. 是的,我可以看出为什么这可能会令人困惑。
Was f(f(f(...(f(x)))) = x
wrapped in triple-double-quotes? That's a function documentation string, sort of like commenting your code. It shouldn't have been stand-alone without something protecting it. 是f(f(f(...(f(x)))) = x
用三重双引号括起来吗?这是一个函数文档字符串,有点像评论你的代码。它不应该是独立的没有保护它的东西。
Imagine f was called increment_by_one. 想象一下f被称为increment_by_one。
Calling increment_by_one 10 times like that on an x of 2 would give 12. No matter how many times you increment, you never seem to get back 2. 在x为2时调用increment_by_one 10次将得到12.无论你增加多少次,你似乎永远不会回来2。
Now imagine f was called multiply_by_one. 现在假设f被称为multiply_by_one。
Calling multiply_by_one 5 times like that on an x of 3 would give 3. Sweet. 在x为3时调用multiply_by_one 5次将得到3. Sweet。
So, some example outputs you can test against (you have to write the functions) 那么,你可以测试一些示例输出(你必须编写函数)
knice(increment_by_one, 1, 3, 5)
would return 0. knice(increment_by_one, 1, 3, 5)
将返回0。
knice(multiply_by_one, 1, 3, 5)
would return 1. knice(multiply_by_one, 1, 3, 5)
将返回1。
As another hint, indentation is important in python. 另外一个提示,缩进在python中很重要。
Here's a concrete example. 这是一个具体的例子。 Start small, and suppose you called knice(f, a=1, b=2, k=1)
. 从小处开始,假设你称为knice(f, a=1, b=2, k=1)
。 For k==1
, we don't have to worry about iterating the function. 对于k==1
,我们不必担心迭代函数。 The only values of x
to consider are 1 and 2, so knice
can return 1 (ie, True) if f(1)==1
or f(2)==2
. 要考虑的x
的唯一值是1和2,因此如果f(1)==1
或f(2)==2
, knice
可以返回1(即,True)。
Now suppose you called knice(f, a=1, b=2, k=2)
. 现在假设你叫knice(f, a=1, b=2, k=2)
。 You'll have to check f(f(1))
and f(f(2))
as well. 你还必须检查f(f(1))
和f(f(2))
。
As k
gets bigger, you'll have to call f
more. 随着k
变大,你将不得不再打电话给f
。 And as the range between a
and b
gets bigger, you'll have to try more values of x
as an argument to f
. 随着a
和b
之间的范围变大,你将不得不尝试将更多的x
值作为f
的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.