小编ilj*_*yya的帖子

无法理解"def gcd()"的代码

更新:一旦我不知道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)

python python-2.7 greatest-common-divisor

1
推荐指数
1
解决办法
573
查看次数

match.group(1) 中的 1 是什么意思?

我在这里遇到了代码:

>>> 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)

什么是1match.group(1)是什么意思?

python

0
推荐指数
1
解决办法
1万
查看次数