更新:一旦我不知道3 %4 =0......
def gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
return a
Run Code Online (Sandbox Code Playgroud)
我认为它的工作原理如下:
gcd(3,4) => while 4: => 3,4 = 4, 3%4 =>
Run Code Online (Sandbox Code Playgroud) 我在这里遇到了代码:
>>> import urllib, re
>>> prefix = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
>>> findnothing = re.compile(r"nothing is (\d+)").search
>>> nothing = '12345'
>>> while True:
... text = urllib.urlopen(prefix + nothing).read()
... print text
... match = findnothing(text)
... if match:
... nothing = match.group(1)
... print " going to", nothing
... else:
... break
Run Code Online (Sandbox Code Playgroud)
什么是1在match.group(1)是什么意思?