- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 Python 和 stackoverflow 的新手。我一直在尝试解决 Pyschools 的平方根近似的“while 循环”示例(主题 5:问题 9)。但是我无法获得所需的输出。我不确定这个问题是否与循环或公式有关。问题如下:
Create a function that takes in a positive number and return 2 integers such that the number is between the squares of the 2 integers. It returns the same integer twice if the number is a square of an integer.
示例:
sqApprox(2)
(1, 2)
sqApprox(4)
(2, 2)
sqApprox(5.1)
(2, 3)
这是我的代码:
<小时/>def sqApprox(num):
i = 0
minsq = 1 # set lower bound
maxsq = minsq # set upper bound
while i*i<=num: # set 'while' termination condition
if i*i<=num and i >=minsq: # complete inequality condition
minsq = i
if i*i<=num and i <=maxsq: # complete inequality condition
maxsq = i
i=i+1 # update i so that 'while' will terminate
return (minsq, maxsq)
<小时/>
如果我创建此函数 sqApprox(4)
并在 IDE 上调用它,我会得到输出 (2, 0)
。
有人可以让我知道我做错了什么吗?提前致谢。
最佳答案
这就是您的代码执行其操作的原因:
行后maxsq = minsq
执行后,这两个值都是 1。
当我们进入循环时
while i*i<=num: # set 'while' termination condition
if i*i<=num and i >=minsq: # complete inequality condition
minsq = i
if i*i<=num and i <=maxsq: # complete inequality condition
maxsq = i
i=i+1 # update i so that 'while' will terminate
首先注意循环内部 i*i<=num
,因此无需重新测试。因此它相当于:
while i*i<=num:
if i >=minsq:
minsq = i
if i <=maxsq:
maxsq = i
i=i+1
在第一次循环中i == 0
但是maxsq == 1
,使第二个条件成立,因此设置 maxsq
等于 i
的当前值,即 0。在随后的循环中,i <= maxsq
是 false (因为 maxsq == 0
但 i > 0
)因此 maxsq
永远不会超过 0。另一方面,while 循环中的第一个条件不断更新 minsq
正如预期的那样。
我建议忘记两者 minsq
和maxsq
完全地。让循环简单地是:
while i*i <= num:
i += 1 #shortcut for i = i + 1
循环执行完毕后,进行一个简单的测试,涉及 i-1
足以确定返回什么。
关于python - pyschools 平方根近似,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39995912/
所以我必须用以下方法来近似 Pi:4*(1-1/3+1/5-1/7+1/9-...)。它也应该基于迭代次数。所以函数应该是这样的: >>> piApprox(1) 4.0 >>> piApprox(1
输入:图 G 输出:多个独立集,使得一个节点对所有独立集的成员资格是唯一的。因此,节点与它自己的集合中的任何节点都没有连接。这是一个示例路径。 由于这里需要澄清,因此再次改写: 将给定的图划分为多个集
我已经使用查找表和低阶多项式近似实现了定点 log2 函数,但对整个 32 位定点范围 [-1,+1) 的准确度不太满意。输入格式为 s0.31,输出格式为 s15.16。 我在这里发布这个问题,以便
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它可以帮助我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注
我的目标是近似二项式变量总和的分布。我使用以下纸张The Distribution of a Sum of Binomial Random Variables作者:肯·巴特勒和迈克尔·斯蒂芬斯。 我想
我知道有方法 approximate cubic Bezier curves ( this page 也是一个很好的引用),但是有没有更快的方法来逼近 N 次贝塞尔曲线?还是只能使用下面的概括? 来自
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它有助于我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注意
我是 C++ 和编码本身的初学者,所以请原谅任何词汇错误。我找不到这个具体问题,但在互联网上找到了类似的问题,但我仍然很难获得我需要的结果。 所以我使用莱布尼茨公式来近似 pi,即: pi = 4 ·
有多种方法可以通过显示名称查找联系人。例如这个答案Android - Find a contact by display name 但是我需要找到模糊匹配的联系人。例如如果找不到“Kim”,我需要返回
我一直在尝试使用以下代码使用级数表示来近似 e 以获得尽可能多的精度数字,但无论我计算多少项,精度数字的数量似乎都保持不变。即: 2.718281984329223632812500000000000
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它可以帮助我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注
大多数拥有CS学位的人当然会知道Big O stands for是什么。 它可以帮助我们评估算法的可扩展性。 但是我很好奇,您如何计算或估算算法的复杂性? 最佳答案 我会尽力在这里简单地解释它,但要注
大多数拥有计算机科学学位的人肯定知道什么是Big O stands for。 它有助于我们衡量一个算法的实际效率,如果您知道在what category the problem you are try
大多数拥有计算机科学学位的人肯定知道什么是Big O stands for。 它有助于我们衡量一个算法的实际效率,如果您知道在what category the problem you are try
我做了很多随机的数学程序来帮助我完成作业(合成除法是最有趣的),现在我想反转一个激进的表达式。 例如,在我方便的 TI 计算器中我得到 .2360679775 好吧,我想将该数字转换为等效的无理数表达
我可以通过 CPU 分析器看到,compute_variances() 是我项目的瓶颈。 % cumulative self self total
大多数拥有 CS 学位的人肯定知道什么 Big O stands for . 它帮助我们衡量算法的可扩展性。 但我很好奇,你如何计算或近似算法的复杂性? 最佳答案 我会尽我所能用简单的术语在这里解释它
这是迄今为止我的代码, from math import * def main(): sides = eval(input("Enter the number of sides:"))
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
大多数拥有 CS 学位的人肯定知道什么 Big O stands for . 它帮助我们衡量算法的扩展性。 但我很好奇,你如何计算或近似算法的复杂性? 最佳答案 我会尽我所能用简单的术语在这里解释它,
我是一名优秀的程序员,十分优秀!