一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 907|回复: 15
收起左侧

[CS61A]Lab03: Recrusion

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-5-6 22:25:40 | 显示全部楼层 |阅读模式

[其他]CS61A #1 - 4@UCBerkely

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
本帖最后由 AveMaleficum 于 2015-5-7 01:29 编辑

第三次lab,对于rerusion和tree recursion的应用
http://gaotx.com/cs61a/lab/lab03/


补充内容 (2015-5-9 13:07):
My answer
http://gaotx.com/blogs/2015/05/10/cs61a-lab03/

评分

1

查看全部评分

AveMaleficum 发表于 2015-5-7 01:27:05 | 显示全部楼层
奇怪,怎么标题都没有显示61A呢?
回复 支持 反对

使用道具 举报

FFFelix 发表于 2015-5-9 23:58:48 | 显示全部楼层
LAB3 画diagram和extra questions都不太容易....

贴一下自己的代码吧
lab03:
  1. # Q2
  2. def make_buzzer(n):
  3.     """ Returns a function that prints numbers in a specified
  4.     range except those divisible by n.

  5.     >>> i_hate_fives = make_buzzer(5)
  6.     >>> i_hate_fives(10)
  7.     Buzz!
  8.     1
  9.     2
  10.     3
  11.     4
  12.     Buzz!
  13.     6
  14.     7
  15.     8
  16.     9
  17.     """
  18.     "*** YOUR CODE HERE ***"
  19.     def buzz(m):
  20.         i = 0
  21.         while i < m:
  22.             if i % n ==0:
  23.                 print("Buzz")
  24.             else:
  25.                 print(i)
  26.             i += 1
  27.     return buzz

  28. # Q4
  29. def f1():
  30.     """
  31.     >>> f1()
  32.     3
  33.     """
  34.     "*** YOUR CODE HERE ***"
  35.     return 3

  36. def f2():
  37.     """
  38.     >>> f2()()
  39.     3
  40.     """
  41.     "*** YOUR CODE HERE ***"
  42.     return lambda: 3

  43. def f3():
  44.     """
  45.     >>> f3()(3)
  46.     3
  47.     """
  48.     "*** YOUR CODE HERE ***"
  49.     return lambda x: x

  50. def f4():
  51.     """
  52.     >>> f4()()(3)()
  53.     3
  54.     """
  55.     "*** YOUR CODE HERE ***"
  56.     return lambda: lambda x: lambda: x

  57. # Q6
  58. def sum(n):
  59.     """Computes the sum of all integers between 1 and n, inclusive.
  60.     Assume n is positive.

  61.     >>> sum(1)
  62.     1
  63.     >>> sum(5)  # 1 + 2 + 3 + 4 + 5
  64.     15
  65.     """
  66.     "*** YOUR CODE HERE ***"
  67.     if n == 1:
  68.         return 1
  69.     return sum(n-1)

  70. # Q7

  71. def sum_every_other_number(n):
  72.     """Return the sum of every other natural number
  73.     up to n, inclusive.

  74.     >>> sum_every_other_number(8)
  75.     20
  76.     >>> sum_every_other_number(9)
  77.     25
  78.     """
  79.     if n == 0:
  80.         return 0
  81.     elif n == 1:
  82.         return 1
  83.     else:
  84.         return n + sum_every_other_number(n - 1)


  85. def fibonacci(n):
  86.     """Return the nth fibonacci number.
  87.    
  88.     >>> fibonacci(11)
  89.     89
  90.     """
  91.     if n == 0:
  92.         return 0
  93.     elif n == 1:
  94.         return 1
  95.     else:
  96.         return fibonacci(n - 1) + fibonacci(n - 2)


  97. # Q8
  98. def hailstone(n):
  99.     """Print out the hailstone sequence starting at n, and return the
  100.     number of elements in the sequence.

  101.     >>> a = hailstone(10)
  102.     10
  103.     5
  104.     16
  105.     8
  106.     4
  107.     2
  108.     1
  109.     >>> a
  110.     7
  111.     """
  112.     "*** YOUR CODE HERE ***"
  113.     print (n)
  114.     if n == 1:
  115.         return 1
  116.     elif n % 2 == 0:
  117.         return 1 + hailstone(n // 2)
  118.     else:
  119.         return 1 + hailstone(3 * n + 1)
复制代码
lab03_extra:
  1. # Q9
  2. def cycle(f1, f2, f3):
  3.     """ Returns a function that is itself a higher order function
  4.     >>> def add1(x):
  5.     ...     return x + 1
  6.     >>> def times2(x):
  7.     ...     return x * 2
  8.     >>> def add3(x):
  9.     ...     return x + 3
  10.     >>> my_cycle = cycle(add1, times2, add3)
  11.     >>> identity = my_cycle(0)
  12.     >>> identity(5)
  13.     5
  14.     >>> add_one_then_double = my_cycle(2)
  15.     >>> add_one_then_double(1)
  16.     4
  17.     >>> do_all_functions = my_cycle(3)
  18.     >>> do_all_functions(2)
  19.     9
  20.     >>> do_more_than_a_cycle = my_cycle(4)
  21.     >>> do_more_than_a_cycle(2)
  22.     10
  23.     >>> do_two_cycles = my_cycle(6)
  24.     >>> do_two_cycles(1)
  25.     19
  26.     """
  27.     "*** YOUR CODE HERE ***"
  28.     def sec_func(n):
  29.         def final_func(x):
  30.             i = 0
  31.             while i < n:
  32.                 if i % 3 == 0:
  33.                     x = f1(x)
  34.                 elif i % 3 == 1:
  35.                     x = f2(x)
  36.                 else:
  37.                     x = f3(x)
  38.                 i += 1
  39.         return final_func
  40.     return sec_func
  41. # Q10
  42. def lambda_curry2(func):
  43.     """
  44.     Returns a Curried version of a two argument function func.
  45.     >>> from operator import add
  46.     >>> x = lambda_curry2(add)
  47.     >>> y = x(3)
  48.     >>> y(5)
  49.     8
  50.     """
  51.     "*** YOUR CODE HERE ***"
  52.     return lambda a: lambda b: func(a,b)

  53. # Q12
  54. def paths(m, n):
  55.     """Return the number of paths from one corner of an
  56.     M by N grid to the opposite corner.

  57.     >>> paths(2, 2)
  58.     2
  59.     >>> paths(5, 7)
  60.     210
  61.     >>> paths(117, 1)
  62.     1
  63.     >>> paths(1, 157)
  64.     1
  65.     """
  66.     "*** YOUR CODE HERE ***"
  67.     if m == 1 or n == 1:
  68.         return 1
  69.     else:
  70.         return paths(m-1,n) + paths(m,n-1)

  71. # Q13
  72. def gcd(a, b):
  73.     """Returns the greatest common divisor of a and b.
  74.     Should be implemented using recursion.

  75.     >>> gcd(34, 19)
  76.     1
  77.     >>> gcd(39, 91)
  78.     13
  79.     >>> gcd(20, 30)
  80.     10
  81.     >>> gcd(40, 40)
  82.     40
  83.     """
  84.     "*** YOUR CODE HERE ***"
  85.     a, b = max(a,b), min(a,b)
  86.     if a % b == 0:
  87.         return b
  88.     else:
  89.         return gcd(b, a % b)
复制代码
其中Q9搞了半天... 发现原来不能直接return 要x = f(x)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

goldpanda 发表于 2015-5-10 05:40:23 | 显示全部楼层
# Q2
def make_buzzer(n):
    """ Returns a function that prints numbers in a specified
    range except those divisible by n.

    >>> i_hate_fives = make_buzzer(5)
    >>> i_hate_fives(10)
    Buzz!
    1
    2
    3
    4
    Buzz!
    6
    7
    8
    9
    """
    def print_buzzer(upper):
        for i in range(upper):
            if i%n == 0:
                print('Buzz!')
            else:
                print(i)
    return print_buzzer

# Q4
def f1():
    """
    >>> f1()
    3
    """
    return 3

def f2():
    """
    >>> f2()()
    3
    """
    return lambda : 3

def f3():
    """
    >>> f3()(3)
    3
    """
    return lambda x : x

def f4():
    """
    >>> f4()()(3)()
    3
    """
    return lambda : lambda x : lambda : x

# Q6
def sum(n):
    """Computes the sum of all integers between 1 and n, inclusive.
    Assume n is positive.

    >>> sum(1)
    1
    >>> sum(5)  # 1 + 2 + 3 + 4 + 5
    15
    """
    if n == 1:
        return 1
    else:
        return n + sum(n-1)

# Q7

def sum_every_other_number(n):
    """Return the sum of every other natural number
    up to n, inclusive.

    >>> sum_every_other_number(8)
    20
    >>> sum_every_other_number(9)
    25
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)


def fibonacci(n):
    """Return the nth fibonacci number.
   
    >>> fibonacci(11)
    89
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# Q8
def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the
    number of elements in the sequence.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    print(n)
    if n == 1:
        return 1
    else:
        if n%2 == 0:
            return 1 + hailstone(int(n/2))
        else:
            return 1 + hailstone(3*n+1)
   
# Q9
def cycle(f1, f2, f3):
    """ Returns a function that is itself a higher order function
    >>> def add1(x):
    ...     return x + 1
    >>> def times2(x):
    ...     return x * 2
    >>> def add3(x):
    ...     return x + 3
    >>> my_cycle = cycle(add1, times2, add3)
    >>> identity = my_cycle(0)
    >>> identity(5)
    5
    >>> add_one_then_double = my_cycle(2)
    >>> add_one_then_double(1)
    4
    >>> do_all_functions = my_cycle(3)
    >>> do_all_functions(2)
    9
    >>> do_more_than_a_cycle = my_cycle(4)
    >>> do_more_than_a_cycle(2)
    10
    >>> do_two_cycles = my_cycle(6)
    >>> do_two_cycles(1)
    19
    """
    def f(n):
        if n == 0:
            return lambda x : x
        elif n%3 == 1:
            return lambda x : f1(f(n-1)(x))
        elif n%3 == 2:
            return lambda x : f2(f(n-1)(x))
        else:
            return lambda x : f3(f(n-1)(x))
    return f

# Q10
def lambda_curry2(func):
    """
    Returns a Curried version of a two argument function func.
    >>> from operator import add
    >>> x = lambda_curry2(add)
    >>> y = x(3)
    >>> y(5)
    8
    """
    return lambda x : lambda y : func(x, y)

# Q12
def paths(m, n):
    """Return the number of paths from one corner of an
    M by N grid to the opposite corner.

    >>> paths(2, 2)
    2
    >>> paths(5, 7)
    210
    >>> paths(117, 1)
    1
    >>> paths(1, 157)
    1
    """
    if m == 1 or n == 1:
        return 1
    else:
        return paths(m-1,n) + paths(m,n-1)

# Q13
def gcd(a, b):
    """Returns the greatest common divisor of a and b.
    Should be implemented using recursion.

    >>> gcd(34, 19)
    1
    >>> gcd(39, 91)
    13
    >>> gcd(20, 30)
    10
    >>> gcd(40, 40)
    40
    """
    if a % b == 0:
        return b
    elif b% a == 0:
        return a
    elif a > b:
        return gcd(a%b,b)
    else:
        return gcd(b%a,a)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

geminiiiiiii 发表于 2015-5-10 15:25:39 | 显示全部楼层
# Q2
def make_buzzer(n):
    def inter(range):
        i = 0
        while i < range:
            if i % n == 0:
                f = 'Buzz!'
            else:
                f = i
            print(f)
            i += 1
        return inter   
    "*** YOUR CODE HERE ***"

# Q4
def f1():
    return 3
    "*** YOUR CODE HERE ***"

def f2():
    return lambda : 3
    "*** YOUR CODE HERE ***"

def f3():
    return lambda x : x
    "*** YOUR CODE HERE ***"

def f4():

    return lambda : lambda x: lambda :x
    "*** YOUR CODE HERE ***"

# Q6
def sum(n):
    if n == 0:
        return 0
    else: return sum(n-1) + n
    "*** YOUR CODE HERE ***"

# Q7

def sum_every_other_number(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)


def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# Q8
def hailstone(n):
    if n % 2 ==0:
        print(n)
        return hailstone( n/2 ) + 1
    elif n == 1:
        print(1)
        return 1
    else:
        print(n)
        return hailstone( n*3 + 1) + 1
    "*** YOUR CODE HERE ***"

# Q9
def cycle(f1, f2, f3):
    def f(n):
        def g(h):
            i = 0
            while i < n:
                if i % 3 == 0:
                    h = f1(h)
                elif i % 3 == 1:
                    h = f2(h)
                else:
                    h = f3(h)
                i +=1
            return h
        return g
    return f
    "*** YOUR CODE HERE ***"

# Q10
def lambda_curry2(func):
    return lambda x : lambda y: func(x, y)

# Q12
def paths(m, n):
    if m ==1 and n ==1:
        return 2
    else:
        paths(m, n-1) + paths(m-1, n)

    "*** YOUR CODE HERE ***"

# Q13
def gcd(a, b):
    if a % b == 0:
        return b
    else:
        gcd(b, a % b)

    "*** YOUR CODE HERE ***"

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

go7going 发表于 2015-5-12 22:27:34 | 显示全部楼层
lab 3  

lambda 部分不懂 回去好好看看

""""
def make_buzzer(n):
    def f(range):
        i=0
        while i<=range:
            if i%n!=0:
                print(i)
            else:
                print('Buzz')
            i=i+1
    return f
o=make_buzzer(5)
print o(10)
"""""

""""
Q6:

def sum(n):
    if n == 1:
        return 1
    return n + sum(n - 1)
"""""
""""
Q7:
def sum_every_other_number(n):
def sum_every_other_number(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)

def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)
"""""
""""
def hailstone(n):
    print(n)
    if n == 1:
        return 1
    elif n % 2 == 0:
        return  hailstone(n // 2)
    else:
        return  hailstone(3 * n + 1)
print hailstone(10)

"""""
""""
Q9:
def cycle(f1, f2, f3):
    def ret_fn(n):
        def ret(x):
            i = 0
            while i < n:
                if i % 3 == 0:
                    x = f1(x)
                elif i % 3 == 1:
                    x = f2(x)
                else:
                    x = f3(x)
                i += 1
            return x
        return ret
    return ret_fn

Q10: lambda的函数完全不懂。。。要去恶补

Q12:
def paths(m, n):
    if m == 1 or n == 1:
        return 1
    return paths(m - 1, n) + paths(m, n - 1)
不会算法的我表示发明这种算法的真的很聪明-。-
"""""

""""
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    if a % b == 0:
        return b
    else:
        return gcd(b, a % b)
"""""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

karte_polo 发表于 2015-5-13 00:00:49 | 显示全部楼层
# Q2
def make_buzzer(n):
    """ Returns a function that prints numbers in a specified
    range except those divisible by n.

    >>> i_hate_fives = make_buzzer(5)
    >>> i_hate_fives(10)
    Buzz!
    1
    2
    3
    4
    Buzz!
    6
    7
    8
    9
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        i =0
        while i<x:
            if i%n==0:
                print ("Buzz!")
            else:
                print (i)
            i+=1
    return h

# Q4
def f1():
    """
    >>> f1()
    3
    """
    "*** YOUR CODE HERE ***"
    return 3

def f2():
    """
    >>> f2()()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda :3

def f3():
    """
    >>> f3()(3)
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda x:x

def f4():
    """
    >>> f4()()(3)()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda : lambda x: lambda:x

# Q6
def sum(n):
    """Computes the sum of all integers between 1 and n, inclusive.
    Assume n is positive.

    >>> sum(1)
    1
    >>> sum(5)  # 1 + 2 + 3 + 4 + 5
    15
    """
    "*** YOUR CODE HERE ***"
    s = n
    if s>1:
        return s+sum(n-1)
    else:
        return s

# Q7

def sum_every_other_number(n):
    """Return the sum of every other natural number
    up to n, inclusive.

    >>> sum_every_other_number(8)
    20
    >>> sum_every_other_number(9)
    25
    """
    if n == 0:
        return 0
    elif n==1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)


def fibonacci(n):
    """Return the nth fibonacci number.
   
    >>> fibonacci(11)
    89
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# Q8
def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the
    number of elements in the sequence.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"
    print (n)
    if n>1:
        if n%2 == 0:
            return 1+hailstone(n//2)
        else:
            return 1+hailstone(n*3+1)
    else:
        return 1

# Q09
def cycle(f1,f2,f3):
    def r(n):
        def ret(x):
            i = 0
            while i<n:
                if i%3 ==0:
                    x = f1(x)
                elif i%3==1:
                    x = f2(x)
                else:
                    x = f3(x)
                i+=1
            return x
        return ret
    return r

# Q10

def lambda_curry2(func):
    """
    Returns a Curried version of a two argument function func.
    >>> from operator import add
    >>> x = lambda_curry2(add)
    >>> y = x(3)
    >>> y(5)
    8
    """
    "*** YOUR CODE HERE ***"
    return lambda arg1: lambda arg2: func(arg1,arg2)

# Q12
def paths(m,n):
    if m == 1 or n==1:
        return 1
    else:
        return paths(m-1,n)+paths(m,n-1)
        
# Q13

def gcd(a,b):
    a,b = max(a,b),min(a,b)
    if a%b ==0:
        return b
    else:
        return gcd(b,a%b)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

jy_121 发表于 2015-5-15 10:50:41 | 显示全部楼层
# Q2
def make_buzzer(n):
    def make_divisible(m):
        for i in range(m):
            if(i%n == 0):
                print "Buzz!"
            else:
                print i
    return make_divisible



# Q4
def f1():
    """
    >>> f1()
    3
    """
    return 3

def f2():
    """
    >>> f2()()
    3
    """
    return lambda:3

def f3():
    """
    >>> f3()(3)
    3
    """
    return lambda x:x

def f4():
    """
    >>> f4()()(3)()
    3
    """
    return lambda : lambda x: lambda : x

# Q6
def sum(n):
    if(n==1):
        return 1
    else:
        return n + sum(n-1)
        

# Q7

def sum_every_other_number(n):
    if n == 0 or n == 1:
        return 0
    else:
        return n + sum_every_other_number(n - 2)


def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# Q8
def hailstone(n):
   
    if(n == 1):
        return 1
   
    else:
        if(n % 2 == 0):
            
            return 1 + hailstone(n/2)
        else:
        
            return 1 + hailstone(3*n + 1)



# Q9
def cycle(f1, f2, f3):
    def h(n):
        def g(x):
            i = 0
            while i < n:
                if i % 3 == 0:
                    x = f1(x)
                elif i % 3 == 1:
                    x = f2(x)
                else:
                    x = f3(x)
                i += 1
        return g
    return h
# Q10
def lambda_curry2(func):
    return lambda x: lambda y: func(x, y)

# Q12
def paths(m, n):
    """Return the number of paths from one corner of an
    M by N grid to the opposite corner.

    >>> paths(2, 2)
    2
    >>> paths(5, 7)
    210
    >>> paths(117, 1)
    1
    >>> paths(1, 157)
    1
    """
    if(m==1 or n==1):
        return 1
    else return paths(m-1,n) + paths(m,n-1)

# Q13
def gcd(a, b):
    """Returns the greatest common divisor of a and b.
    Should be implemented using recursion.

    >>> gcd(34, 19)
    1
    >>> gcd(39, 91)
    13
    >>> gcd(20, 30)
    10
    >>> gcd(40, 40)
    40
    """
    a,b = max(a,b),min(a,b)
    if(a%b = 0):
        return b
    else:
        return gcd(a,a%b)
   

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

乐百氏开开心心 发表于 2015-5-17 16:59:40 | 显示全部楼层
Question 11 Community 这个题是什么意思呀?
>>> def troy():
...     abed = 0
...     while abed < 10:
...         britta = lambda: abed
...         abed += 1
...     abed = 20
...     return britta
...
>>> jeff = troy()
>>> shirley = lambda : jeff
>>> pierce = shirley()
>>> pierce()
回复 支持 反对

使用道具 举报

神罗天征 发表于 2015-5-19 14:06:55 | 显示全部楼层
  1. # Q2
  2. def make_buzzer(n):
  3.     """ Returns a function that prints numbers in a specified
  4.     range except those divisible by n.

  5.     >>> i_hate_fives = make_buzzer(5)
  6.     >>> i_hate_fives(10)
  7.     Buzz!
  8.     1
  9.     2
  10.     3
  11.     4
  12.     Buzz!
  13.     6
  14.     7
  15.     8
  16.     9
  17.     """
  18.     def buzzer(m):
  19.         for i in range(1,m):
  20.             if(i == n):
  21.                 print ("Buzz!")
  22.             else:
  23.                 print(i)
  24.     return buzzer

  25. # Q4
  26. def f1():
  27.     """
  28.     >>> f1()
  29.     3
  30.     """
  31.     return 3

  32. def f2():
  33.     """
  34.     >>> f2()()
  35.     3
  36.     """
  37.     return lambda: 3

  38. def f3():
  39.     """
  40.     >>> f3()(3)
  41.     3
  42.     """
  43.     return lambda x: x

  44. def f4():
  45.     """
  46.     >>> f4()()(3)()
  47.     3
  48.     """
  49.     return lambda: lambda x: lambda: x

  50. # Q6
  51. def sum(n):
  52.     """Computes the sum of all integers between 1 and n, inclusive.
  53.     Assume n is positive.

  54.     >>> sum(1)
  55.     1
  56.     >>> sum(5)  # 1 + 2 + 3 + 4 + 5
  57.     15
  58.     """
  59.     if n ==1:
  60.         return 1
  61.     else;
  62.         return n+sum(n-1)

  63. # Q7

  64. def sum_every_other_number(n):
  65.     """Return the sum of every other natural number
  66.     up to n, inclusive.

  67.     >>> sum_every_other_number(8)
  68.     20
  69.     >>> sum_every_other_number(9)
  70.     25
  71.     """
  72.     if n == 0:
  73.         return 0
  74.     elif n ==1:
  75.         return 1
  76.     else:
  77.         return n + sum_every_other_number(n - 2)


  78. def fibonacci(n):
  79.     """Return the nth fibonacci number.
  80.    
  81.     >>> fibonacci(11)
  82.     89
  83.     """
  84.     if n == 0:
  85.         return 0
  86.     elif n == 1:
  87.         return 1
  88.     else:
  89.         return fibonacci(n - 1) + fibonacci(n - 2)


  90. # Q8
  91. def hailstone(n):
  92.     """Print out the hailstone sequence starting at n, and return the
  93.     number of elements in the sequence.

  94.     >>> a = hailstone(10)
  95.     10
  96.     5
  97.     16
  98.     8
  99.     4
  100.     2
  101.     1
  102.     >>> a
  103.     7
  104.     """
  105.     if n == 1:
  106.         return 0
  107.     elif n%2==0:
  108.         return 1+ hailstone(n//2)
  109.     else:
  110.         return 1+ hailstone(3*n+1)
  111.             

  112. # Q9
  113. def cycle(f1, f2, f3):
  114.     """ Returns a function that is itself a higher order function
  115.     >>> def add1(x):
  116.     ...     return x + 1
  117.     >>> def times2(x):
  118.     ...     return x * 2
  119.     >>> def add3(x):
  120.     ...     return x + 3
  121.     >>> my_cycle = cycle(add1, times2, add3)
  122.     >>> identity = my_cycle(0)
  123.     >>> identity(5)
  124.     5
  125.     >>> add_one_then_double = my_cycle(2)
  126.     >>> add_one_then_double(1)
  127.     4
  128.     >>> do_all_functions = my_cycle(3)
  129.     >>> do_all_functions(2)
  130.     9
  131.     >>> do_more_than_a_cycle = my_cycle(4)
  132.     >>> do_more_than_a_cycle(2)
  133.     10
  134.     >>> do_two_cycles = my_cycle(6)
  135.     >>> do_two_cycles(1)
  136.     19
  137.     """
  138.     def ret_fn(n):
  139.         def ret(x):
  140.             i = 0
  141.             while i < n:
  142.                 if i % 3 == 0:
  143.                     x = f1(x)
  144.                 elif i % 3 == 1:
  145.                     x = f2(x)
  146.                 else:
  147.                     x = f3(x)
  148.                 i += 1
  149.             return x
  150.         return ret
  151.     return ret_fn

  152. # Q10
  153. def lambda_curry2(func):
  154.     """
  155.     Returns a Curried version of a two argument function func.
  156.     >>> from operator import add
  157.     >>> x = lambda_curry2(add)
  158.     >>> y = x(3)
  159.     >>> y(5)
  160.     8
  161.     """
  162.     "*** YOUR CODE HERE ***"
  163.     return ______

  164. # Q12
  165. def paths(m, n):
  166.     """Return the number of paths from one corner of an
  167.     M by N grid to the opposite corner.

  168.     >>> paths(2, 2)
  169.     2
  170.     >>> paths(5, 7)
  171.     210
  172.     >>> paths(117, 1)
  173.     1
  174.     >>> paths(1, 157)
  175.     1
  176.     """
  177.     if n==1 && m == 1:
  178.         return 1
  179.     else:
  180.         return paths(m-1,n)+paths(m,n-1)

  181. # Q13
  182. def gcd(a, b):
  183.     """Returns the greatest common divisor of a and b.
  184.     Should be implemented using recursion.

  185.     >>> gcd(34, 19)
  186.     1
  187.     >>> gcd(39, 91)
  188.     13
  189.     >>> gcd(20, 30)
  190.     10
  191.     >>> gcd(40, 40)
  192.     40
  193.     """
  194.     a, b = max(a, b), min(a, b)
  195.     if a % b == 0:
  196.         return b
  197.     else:
  198.         return gcd(b, a % b)



  199. lambda什么的……还得好好学学
复制代码

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-23 07:37:52 | 显示全部楼层
# Q2
def make_buzzer(n):
    """ Returns a function that prints numbers in a specified
    range except those divisible by n.

    >>> i_hate_fives = make_buzzer(5)
    >>> i_hate_fives(10)
    Buzz!
    1
    2
    3
    4
    Buzz!
    6
    7
    8
    9
    """
    "*** YOUR CODE HERE ***"
    def f(range):
        i = 0
        while i < range:
            if n % i == 0:
                print("Buzz!")
            else:
                print(i)
            i += 1
    return f



    return f

# Q4
def f1():
    """
    >>> f1()
    3
    """
    "*** YOUR CODE HERE ***"
    return 3

def f2():
    """
    >>> f2()()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda: 3

def f3():
    """
    >>> f3()(3)
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda x: x

def f4():
    """
    >>> f4()()(3)()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda: lambda x: lambda: x

# Q6
def sum(n):
    """Computes the sum of all integers between 1 and n, inclusive.
    Assume n is positive.

    >>> sum(1)
    1
    >>> sum(5)  # 1 + 2 + 3 + 4 + 5
    15
    """
    "*** YOUR CODE HERE ***"
    if n == 1:
        return 1
    else:
        return n + sum(n - 1)

# Q7

def sum_every_other_number(n):
    """Return the sum of every other natural number
    up to n, inclusive.

    >>> sum_every_other_number(8)
    20
    >>> sum_every_other_number(9)
    25
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)


def fibonacci(n):
    """Return the nth fibonacci number.
   
    >>> fibonacci(11)
    89
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# Q8
def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the
    number of elements in the sequence.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"
    print(n)
    if n == 1:
        return 1
    elif n % 2 == 1:
        return 1 + hailstone(n // 2)
    else:
        return 1 + hailstone(3 * n + 1)
回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-12 21:58:58 | 显示全部楼层
递归还是不难的,不过Q4的lambda想了好半天
上代码~

#lab03 Q2
def make_buzzer(n):
    """ Returns a function that prints numbers in a specified
    range except those divisible by n.

    >>> i_hate_fives = make_buzzer(5)
    >>> i_hate_fives(10)
    Buzz!
    1
    2
    3
    4
    Buzz!
    6
    7
    8
    9
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        i = 0
        while(i < x):
            if(i % n) == 0:
                print('Buzz!')
            else:
                print i
            i += 1
    return h
   
#Q4
def f1():
    """
    >>> f1()
    3
    """
    "*** YOUR CODE HERE ***"
    return 3

def f2():
    """
    >>> f2()()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda: 3

def f3():
    """
    >>> f3()(3)
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda x: x

def f4():
    """
    >>> f4()()(3)()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda: lambda x: lambda: x
   
#Q6
def sum(n):
    """Computes the sum of all integers between 1 and n, inclusive.
    Assume n is positive.

    >>> sum(1)
    1
    >>> sum(5)  # 1 + 2 + 3 + 4 + 5
    15
    """
    "*** YOUR CODE HERE ***"
    if n == 1:
        return 1
    return n + sum(n-1)
   
#Q7
def sum_every_other_number(n):
    """Return the sum of every other natural number
    up to n, inclusive.

    >>> sum_every_other_number(8)
    20
    >>> sum_every_other_number(9)
    25
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)
        
def fibonacci(n):
    """Return the nth fibonacci number.

    >>> fibonacci(11)
    89
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)
        
#Q8
def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the
    number of elements in the sequence.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"   
    print n
    if n == 1:
    #     print n
        return 1
    elif n % 2 == 0:
    #     print n
        return 1 + hailstone(n/2)
    elif n % 2 != 0:
    #    print n
        return 1 + hailstone(3 * n + 1)
回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-1-20 13:18:45 | 显示全部楼层
交作业!!!
lab03.png

代码:
  1. # Q2
  2. def make_buzzer(n):
  3.     """ Returns a function that prints numbers in a specified
  4.     range except those divisible by n.

  5.     >>> i_hate_fives = make_buzzer(5)
  6.     >>> i_hate_fives(10)
  7.     Buzz!
  8.     1
  9.     2
  10.     3
  11.     4
  12.     Buzz!
  13.     6
  14.     7
  15.     8
  16.     9
  17.     """
  18.     "*** YOUR CODE HERE ***"
  19.     def ran(limit):
  20.             for i in range(limit):
  21.                     if i % n == 0:
  22.                             print("Buzz!")
  23.                     else:
  24.                             print(i)
  25.     return ran


  26. # Q4
  27. def f1():
  28.     """
  29.     >>> f1()
  30.     3
  31.     """
  32.     "*** YOUR CODE HERE ***"
  33.     return 3


  34. def f2():
  35.     """
  36.     >>> f2()()
  37.     3
  38.     """
  39.     "*** YOUR CODE HERE ***"
  40.     return lambda: 3


  41. def f3():
  42.     """
  43.     >>> f3()(3)
  44.     3
  45.     """
  46.     "*** YOUR CODE HERE ***"
  47.     return lambda x: x


  48. def f4():
  49.     """
  50.     >>> f4()()(3)()
  51.     3
  52.     """
  53.     "*** YOUR CODE HERE ***"
  54.     return lambda: lambda x: lambda: x


  55. # Q6
  56. def sum(n):
  57.     """Computes the sum of all integers between 1 and n, inclusive.
  58.     Assume n is positive.

  59.     >>> sum(1)
  60.     1
  61.     >>> sum(5)  # 1 + 2 + 3 + 4 + 5
  62.     15
  63.     """
  64.     "*** YOUR CODE HERE ***"
  65.     if n <= 1:
  66.             return n
  67.     return sum(n - 1) + n

  68. # Q7

  69. def sum_every_other_number(n):
  70.     """Return the sum of every other natural number
  71.     up to n, inclusive.

  72.     >>> sum_every_other_number(8)
  73.     20
  74.     >>> sum_every_other_number(9)
  75.     25
  76.     """
  77.     if n < 2:
  78.         return n
  79.     else:
  80.         return n + sum_every_other_number(n - 2)


  81. def fibonacci(n):
  82.     """Return the nth fibonacci number.
  83.    
  84.     >>> fibonacci(11)
  85.     89
  86.     """
  87.     if n == 0:
  88.         return 0
  89.     elif n == 1:
  90.         return 1
  91.     else:
  92.         return fibonacci(n - 1) + fibonacci(n - 2)


  93. # Q8
  94. def hailstone(n):
  95.     """Print out the hailstone sequence starting at n, and return the
  96.     number of elements in the sequence.

  97.     >>> a = hailstone(10)
  98.     10
  99.     5
  100.     16
  101.     8
  102.     4
  103.     2
  104.     1
  105.     >>> a
  106.     7
  107.     """
  108.     "*** YOUR CODE HERE ***"
  109.     print(n)
  110.     if n == 1:
  111.             return 1
  112.     elif n % 2 == 0:
  113.             return 1 + hailstone(n // 2)
  114.     else:
  115.             return 1 + hailstone(n * 3 + 1)
复制代码
  1. # Q9
  2. def cycle(f1, f2, f3):
  3.     """ Returns a function that is itself a higher order function
  4.     >>> def add1(x):
  5.     ...     return x + 1
  6.     >>> def times2(x):
  7.     ...     return x * 2
  8.     >>> def add3(x):
  9.     ...     return x + 3
  10.     >>> my_cycle = cycle(add1, times2, add3)
  11.     >>> identity = my_cycle(0)
  12.     >>> identity(5)
  13.     5
  14.     >>> add_one_then_double = my_cycle(2)
  15.     >>> add_one_then_double(1)
  16.     4
  17.     >>> do_all_functions = my_cycle(3)
  18.     >>> do_all_functions(2)
  19.     9
  20.     >>> do_more_than_a_cycle = my_cycle(4)
  21.     >>> do_more_than_a_cycle(2)
  22.     10
  23.     >>> do_two_cycles = my_cycle(6)
  24.     >>> do_two_cycles(1)
  25.     19
  26.     """
  27.     "*** YOUR CODE HERE ***"
  28.     def iteration(n):
  29.         def ret(x):
  30.             i = 0
  31.             while i < n:
  32.                 if i % 3 == 0:
  33.                     x = f1(x)
  34.                 elif i % 3 == 1:
  35.                     x = f2(x)
  36.                 else:
  37.                     x = f3(x)
  38.                 i += 1
  39.             return x
  40.         return ret
  41.     return iteration

  42. # Q10
  43. def lambda_curry2(func):
  44.     """
  45.     Returns a Curried version of a two argument function func.
  46.     >>> from operator import add
  47.     >>> x = lambda_curry2(add)
  48.     >>> y = x(3)
  49.     >>> y(5)
  50.     8
  51.     """
  52.     "*** YOUR CODE HERE ***"
  53.     return lambda x: lambda y: func(x, y)

  54. # Q12
  55. def paths(m, n):
  56.     """Return the number of paths from one corner of an
  57.     M by N grid to the opposite corner.

  58.     >>> paths(2, 2)
  59.     2
  60.     >>> paths(5, 7)
  61.     210
  62.     >>> paths(117, 1)
  63.     1
  64.     >>> paths(1, 157)
  65.     1
  66.     """
  67.     "*** YOUR CODE HERE ***"
  68.     if m == 1 or n == 1:
  69.         return 1
  70.     return paths(m - 1, n) + paths(m, n - 1)

  71. # Q13
  72. def gcd(a, b):
  73.     """Returns the greatest common divisor of a and b.
  74.     Should be implemented using recursion.

  75.     >>> gcd(34, 19)
  76.     1
  77.     >>> gcd(39, 91)
  78.     13
  79.     >>> gcd(20, 30)
  80.     10
  81.     >>> gcd(40, 40)
  82.     40
  83.     """
  84.     "*** YOUR CODE HERE ***"
  85.     if a < b:
  86.         a, b = b, a
  87.     if a % b == 0:
  88.         return b
  89.     else:
  90.         return gcd(b, a % b)
复制代码
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-2-12 14:50:41 | 显示全部楼层
lambda的用法始终不理解,Q4里面f1和f2的区别,f3和f4的区别,还有它们的怎么来的,搞不清楚
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-2-12 14:50:58 | 显示全部楼层
# Q2
def make_buzzer(n):
    """ Returns a function that prints numbers in a specified
    range except those divisible by n.

    >>> i_hate_fives = make_buzzer(5)
    >>> i_hate_fives(10)
    Buzz!
    1
    2
    3
    4
    Buzz!
    6
    7
    8
    9
    """
    "*** YOUR CODE HERE ***"
    def buzz(m):
        for i in range(0, m):
            if i % n == 0:
                print('Buzz!')
            else:
                print(i)
    return buzz

# Q4
def f1():
    """
    >>> f1()
    3
    """
    "*** YOUR CODE HERE ***"
    return 3

def f2():
    """
    >>> f2()()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda: 3

def f3():
    """
    >>> f3()(3)
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda x: x

def f4():
    """
    >>> f4()()(3)()
    3
    """
    "*** YOUR CODE HERE ***"
    return lambda: lambda x: lambda: x

# Q6
def sum(n):
    """Computes the sum of all integers between 1 and n, inclusive.
    Assume n is positive.

    >>> sum(1)
    1
    >>> sum(5)  # 1 + 2 + 3 + 4 + 5
    15
    """
    "*** YOUR CODE HERE ***"
    if n == 0:
        total = 0
    else:
        total = sum(n-1) + n
    return total

# Q7

def sum_every_other_number(n):
    """Return the sum of every other natural number
    up to n, inclusive.

    >>> sum_every_other_number(8)
    20
    >>> sum_every_other_number(9)
    25
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return n + sum_every_other_number(n - 2)


def fibonacci(n):
    """Return the nth fibonacci number.
   
    >>> fibonacci(11)
    89
    """
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# Q8
def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the
    number of elements in the sequence.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"
    print (n)
    if n == 1:
        return 1
    elif n % 2 == 0:
        return hailstone(n // 2) + 1
    else:
        return hailstone(n * 3 + 1) + 1

# Q9
def cycle(f1, f2, f3):
    """ Returns a function that is itself a higher order function
    >>> def add1(x):
    ...     return x + 1
    >>> def times2(x):
    ...     return x * 2
    >>> def add3(x):
    ...     return x + 3
    >>> my_cycle = cycle(add1, times2, add3)
    >>> identity = my_cycle(0)
    >>> identity(5)
    5
    >>> add_one_then_double = my_cycle(2)
    >>> add_one_then_double(1)
    4
    >>> do_all_functions = my_cycle(3)
    >>> do_all_functions(2)
    9
    >>> do_more_than_a_cycle = my_cycle(4)
    >>> do_more_than_a_cycle(2)
    10
    >>> do_two_cycles = my_cycle(6)
    >>> do_two_cycles(1)
    19
    """
    "*** YOUR CODE HERE ***"
    def fn(n):
        def ret(x):
            for i in range(0, n):
                if i % 3 == 0:
                    x = f1(x)
                elif i % 3 == 1:
                    x = f2(x)
                else:
                    x = f3(x)
            return x
        return ret
    return fn

# Q10
def lambda_curry2(func):
    """
    Returns a Curried version of a two argument function func.
    >>> from operator import add
    >>> x = lambda_curry2(add)
    >>> y = x(3)
    >>> y(5)
    8
    """
    "*** YOUR CODE HERE ***"
    return lambda arg1: lambda arg2: func(arg1, arg2)

# Q12
def paths(m, n):
    """Return the number of paths from one corner of an
    M by N grid to the opposite corner.

    >>> paths(2, 2)
    2
    >>> paths(5, 7)
    210
    >>> paths(117, 1)
    1
    >>> paths(1, 157)
    1
    """
    "*** YOUR CODE HERE ***"
    if m == 1 or n == 1:
        return 1
    else:
        return paths(m, n-1) + paths(m-1, n)

# Q13
def gcd(a, b):
    """Returns the greatest common divisor of a and b.
    Should be implemented using recursion.

    >>> gcd(34, 19)
    1
    >>> gcd(39, 91)
    13
    >>> gcd(20, 30)
    10
    >>> gcd(40, 40)
    40
    """
    "*** YOUR CODE HERE ***"
    p = max(a, b)
    q = min(a, b)
    if p % q == 0:
        return  q
    else:
        return gcd(q, p % q)
回复 支持 反对

使用道具 举报

wkh279 发表于 2016-11-19 11:21:43 | 显示全部楼层
lab03.py
  1. # Q2
  2. def make_buzzer(n):
  3.     """ Returns a function that prints numbers in a specified
  4.     range except those divisible by n.

  5.     >>> i_hate_fives = make_buzzer(5)
  6.     >>> i_hate_fives(10)
  7.     Buzz!
  8.     1
  9.     2
  10.     3
  11.     4
  12.     Buzz!
  13.     6
  14.     7
  15.     8
  16.     9
  17.     """
  18.     def buzz(m):
  19.         i = 0
  20.         while i < m:
  21.             if i % n == 0:
  22.                 print('Buzz!')
  23.             else:
  24.                 print(i)
  25.             i += 1
  26.     return buzz

  27. # Q4
  28. def f1():
  29.     """
  30.     >>> f1()
  31.     3
  32.     """
  33.     return 3

  34. def f2():
  35.     """
  36.     >>> f2()()
  37.     3
  38.     """
  39.     return lambda:3

  40. def f3():
  41.     """
  42.     >>> f3()(3)
  43.     3
  44.     """
  45.     return lambda x:x

  46. def f4():
  47.     """
  48.     >>> f4()()(3)()
  49.     3
  50.     """
  51.     return lambda: lambda x: lambda:x
  52. # Q6
  53. def sum(n):
  54.     """Computes the sum of all integers between 1 and n, inclusive.
  55.     Assume n is positive.

  56.     >>> sum(1)
  57.     1
  58.     >>> sum(5)  # 1 + 2 + 3 + 4 + 5
  59.     15
  60.     """
  61.     if n==1:
  62.         return 1
  63.     else:
  64.         return n+sum(n-1)

  65. # Q7

  66. def sum_every_other_number(n):
  67.     """Return the sum of every other natural number
  68.     up to n, inclusive.

  69.     >>> sum_every_other_number(8)
  70.     20
  71.     >>> sum_every_other_number(9)
  72.     25
  73.     """
  74.     if n == 0:
  75.         return 0
  76.     elif n ==1:
  77.         return 1
  78.     else:
  79.         return n + sum_every_other_number(n - 2)


  80. def fibonacci(n):
  81.     """Return the nth fibonacci number.
  82.    
  83.     >>> fibonacci(11)
  84.     89
  85.     """
  86.     if n == 0:
  87.         return 0
  88.     elif n == 1:
  89.         return 1
  90.     else:
  91.         return fibonacci(n - 1) + fibonacci(n - 2)


  92. # Q8
  93. def hailstone(n):
  94.     """Print out the hailstone sequence starting at n, and return the
  95.     number of elements in the sequence.

  96.     >>> a = hailstone(10)
  97.     10
  98.     5
  99.     16
  100.     8
  101.     4
  102.     2
  103.     1
  104.     >>> a
  105.     7
  106.     """
  107.     print(n)
  108.     if n==1:
  109.         return 1
  110.     elif n%2 == 0:
  111.         return 1+hailstone(n//2)
  112.     else:
  113.         return hailstone(3*n+1)+1
复制代码


lab03_extra.py
  1. # Q9
  2. def cycle(f1, f2, f3):
  3.     """ Returns a function that is itself a higher order function
  4.     >>> def add1(x):
  5.     ...     return x + 1
  6.     >>> def times2(x):
  7.     ...     return x * 2
  8.     >>> def add3(x):
  9.     ...     return x + 3
  10.     >>> my_cycle = cycle(add1, times2, add3)
  11.     >>> identity = my_cycle(0)
  12.     >>> identity(5)
  13.     5
  14.     >>> add_one_then_double = my_cycle(2)
  15.     >>> add_one_then_double(1)
  16.     4
  17.     >>> do_all_functions = my_cycle(3)
  18.     >>> do_all_functions(2)
  19.     9
  20.     >>> do_more_than_a_cycle = my_cycle(4)
  21.     >>> do_more_than_a_cycle(2)
  22.     10
  23.     >>> do_two_cycles = my_cycle(6)
  24.     >>> do_two_cycles(1)
  25.     19
  26.     """
  27.     def f(n):
  28.         if n==0:
  29.             return lambda x:x
  30.         if n %3==0:
  31.             return lambda x:f3(f2(f1(f(n-3)(x))))
  32.         elif n %3==1:
  33.             return lambda x:f1(f(n-1)(x))
  34.         else:
  35.             return lambda x:f2(f1(f(n-2)(x)))
  36.     return f

  37. # Q10
  38. def lambda_curry2(func):
  39.     """
  40.     Returns a Curried version of a two argument function func.
  41.     >>> from operator import add
  42.     >>> x = lambda_curry2(add)
  43.     >>> y = x(3)
  44.     >>> y(5)
  45.     8
  46.     """
  47.     return lambda x: lambda y: func(x,y)

  48. # Q12
  49. def paths(m, n):
  50.     """Return the number of paths from one corner of an
  51.     M by N grid to the opposite corner.

  52.     >>> paths(2, 2)
  53.     2
  54.     >>> paths(5, 7)
  55.     210
  56.     >>> paths(117, 1)
  57.     1
  58.     >>> paths(1, 157)
  59.     1
  60.     """
  61.     if m==1 or n==1:
  62.         return 1
  63.     else:
  64.         return paths(m-1,n)+paths(m,n-1)

  65. # Q13
  66. def gcd(a, b):
  67.     """Returns the greatest common divisor of a and b.
  68.     Should be implemented using recursion.

  69.     >>> gcd(34, 19)
  70.     1
  71.     >>> gcd(39, 91)
  72.     13
  73.     >>> gcd(20, 30)
  74.     10
  75.     >>> gcd(40, 40)
  76.     40
  77.     """
  78.     x,y=min(a,b),max(a,b)
  79.     if y%x==0:
  80.         return x
  81.     else:
  82.         return gcd(x,y%x)
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-8 06:35

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表