[英]Understanding the implementation of Euclid's Algorithm for GCF in Python
Euclid's Algorithm for GCF of two numbers is: GCF(a, b)=GCF(b, a mod b)
. Euclid的两个数字的GCF算法是:
GCF(a, b)=GCF(b, a mod b)
。 I have seen this implemented in Python as follows: 我已经看到这在Python中实现如下:
def gcf(a, b):
return b and gcf(b, a%b) or a
I don't understand how to parse this function or specifically how boolean logic is applied to integers. 我不明白如何解析这个函数或具体如何将布尔逻辑应用于整数。 For example,
gcf(42, 56) = 14
. 例如,
gcf(42, 56) = 14
。 When I walk through it, I see that eventually the recursive part returns zero. 当我走过它时,我看到最终递归部分返回零。 I follow that
0 or n == n
and 0 and n == 0
. 我遵循
0 or n == n
和0 and n == 0
。 However, once I have a pair of non-zero integers being compared with and/or logic, I don't understand what happens and why. 但是,一旦我将一对非零整数与逻辑和/或逻辑进行比较,我不明白发生了什么以及为什么。
Can someone walk me through this function? 有人可以引导我完成这个功能吗?
Python boolean operators 'or' and 'and' don't return boolean values. Python布尔运算符'或'和'和'不返回布尔值。 They return one of the values they are comparing.
他们返回他们正在比较的值之一。
0 or n - returns n 0或n - 返回n
0 and n - returns 0 0和n - 返回0
a and b or c
is just a trick to implement the (a ? b : c)
syntax in C. a and b or c
只是在C中实现(a ? b : c)
语法的技巧。
Read section 4.6 of Dive into Python for a great description of python's boolean operations and this trick. 阅读Dive into Python的 4.6节,了解python的布尔操作和这个技巧。
In your case, the call stack will look like this: 在您的情况下,调用堆栈将如下所示:
gcf(42, 56) gcf(42,56)
gcf(56, 42) // since b was non-zero, will recurse and pass 42 % 56 (=42) as second arg gcf(56,42)//由于b非零,将递归并通过42%56(= 42)作为第二个arg
gcf(42, 14) // since b was non-zero, will recurse and pass 56 % 42 (=14) as second arg gcf(42,14)//因为b非零,将递归并通过56%42(= 14)作为第二个arg
gcf(14, 0) // since b was non-zero, will recurse and pass 42 % 14 (=0) as second arg gcf(14,0)//因为b非零,将递归并传递42%14(= 0)作为第二个arg
return a // since b was zero, will just return a (14) 返回//因为b为零,只返回一个(14)
This will pop all the way to the top. 这将一直弹到顶部。 In python, the and will return the number and not a boolean, which is why popping up returns the result and not a 1/true.
在python中,和将返回数字而不是布尔值,这就是为什么弹出返回结果而不是1 / true。
However, once I have a pair of non-zero integers being compared with and/or logic, I don't understand what happens and why.
但是,一旦我将一对非零整数与逻辑和/或逻辑进行比较,我不明白发生了什么以及为什么。
What happens is that the first value that affects the result is returned. 会发生什么是返回影响结果的第一个值。
x or y
-> evaluates to x
whenever code in an if x:
block would run, and otherwise evaluates to y
. 只要
if x:
block中的代码运行, x or y
- >计算结果为x
,否则计算结果为y
。
x and y
-> evaluates to x
whenever code in an if x:
block would not run, and otherwise evaluates to y
. 只要
if x:
block中的代码不运行, x and y
- >计算结果为x
,否则计算结果为y
。
Why that happens is because GvR said so. 为什么会这样,是因为GvR这么说。 It might, possibly, have been precisely to make this trick work, back before the
x if C else y
construct was added to the language. 它可能正是使这个技巧发挥作用,回到
x if C else y
之前, x if C else y
结构添加到语言中。
But, you know... you could have just tested it for yourself. 但是,你知道......你本可以亲自测试一下。 That's what the REPL is for :)
这就是REPL的用途:)
If b is not equal to zero then the result is gcf(b, a%b) (recurse). 如果b不等于零,则结果为gcf(b,a%b)(递归)。 If b is equal to zero then the result is a.
如果b等于零,则结果为a。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.