一亩三分地论坛

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

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

[CS61A]Homework 3

[复制链接] |试试Instant~ |关注本帖
goldpanda 发表于 2015-5-14 02:49:51 | 显示全部楼层 |阅读模式

[其他]CS61A #2 - 2015-05-04@UCBerkely

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

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

x
作业连接http://gaotx.com/cs61a/hw/hw03/

 楼主| goldpanda 发表于 2015-5-14 02:50:36 | 显示全部楼层
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    if n <= 3:
        return n
    else:
        return g(n-1) + 2*g(n-2) +3*g(n-3)
def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    if n <= 3:
        return n
    else:
        first = 1
        second = 2
        third = 3
        for i in range(3, n):
            result = third + 2*second +3*first
            first = second
            second = third
            third = result
        return result


def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    if k == 0:
        return False
    if k%10 == 7:
        return True
    else:
        return has_seven(k//10)

def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
   
    if n <= 2:
        return n
    else:
        if (n-1)%7 == 0 or has_seven(n-1):
            return pingpong(n-2)
        else:
            return pingpong(n-1)*2 - pingpong(n-2)
   


def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    return count_using(1, amount)

def count_using(min_coin, amount):
    if amount < 0:
        return 0
    elif amount == 0:
        return 1
    elif min_coin > amount:
        return 0
    else:
        with_min = count_using(min_coin, amount - min_coin)
        without_min = count_using(2*min_coin, amount)
        return with_min + without_min

def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    if n == 1:
        print('Move the top disk from rod', start, 'to rod', end)
    else:
        towers_of_hanoi(n-1, start, 6-start-end)
        print('Move the top disk from rod', start, 'to rod', end)
        towers_of_hanoi(n-1, 6-start-end, end)

from operator import sub, mul

def make_anonymous_factorial():
    """Return the value of an expression that computes factorial.

    >>> make_anonymous_factorial()(5)
    120
    """
    return lambda x : 1 if x == 1 else x*make_anonymous_factorial()(x-1)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

geminiiiiiii 发表于 2015-5-14 21:27:52 | 显示全部楼层
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    else:
        return g(n-1) + 2 * g(n-2) + 3* g(n-3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    "*** YOUR CODE HERE ***"
    i = 4
    while n < 4:
        return n
    pre_one, pre_two, pre_three = 3, 2, 1
    while n >= 4 and i <= n:
        g = pre_one + 2 * pre_two + 3 * pre_three
        pre_one, pre_two, pre_three = g, pre_one, pre_two
        i += 1
    return g
        pass


def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    "*** YOUR CODE HERE ***"
    if k % 10 == 7:
        return True
    elif k % 10 != 7 and k // 10 ==0:
        return False
    else:
        return has_seven(k // 10)



def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    "*** YOUR CODE HERE ***"
    def pingpong_next(k, p, up):
        if k == n:
            return p
        if up:
            return pingpong_switch(k+1, p+1, up)
        else:
            return pingpong_switch(k+1, p-1, up)

    def pingpong_switch(k, p, up):
        if k % 7 == 0 or has_seven(k):
            return pingpong_next(k, p, not up)
        else:
            return pingpong_next(k, p, up)

    return pingpong_next(1, 1, True)



def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    def count_using(min_coin, amount):
    if amount < 0:
        return 0
    elif amount == 0:
        return 1
    elif min_coin > amount:
        return 0
    else:
        with_min = count_using(min_coin, amount - min_coin)
        without_min = count_using(2*min_coin, amount)
        return with_min + without_min


def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    "*** YOUR CODE HERE ***"
    if n == 1:
        print('Move the top of disk from rod ', start, 'to rod', end)
    else:
        mid = 6 - start - end
        towers_of_hanoi(n-1, start, mid)
        print('Move the top of disk from rod ', start, 'to rod', end)
        towers_of_hanoi(n-1, mid, end)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

go7going 发表于 2015-5-14 23:04:22 | 显示全部楼层
感觉作业好难。。。。
找个时间找同学一起讨论一下=。= 先交了

""""
def g(n):
    if n in (1, 2, 3):
        return n
    return g(n-1) + 2*g(n-2) + 3*g(n-3)

def g_iter(n):
    if n == 1 or n == 2 or n == 3:
        return n
    a, b, c = 1, 2, 3
    while n > 3:
        a, b, c = b, c, c + 2*b + 3*a
        n = n - 1
    return c
"""""
""""
def has_seven(k):
    if k % 10 == 7:
        return True
    elif k < 10:
        return False
    else:
        return has_seven(k // 10)

"""""
""""
def pingpong(n):
    def pingpong_next(k, p, up):
        if k == n:
            return p
        if up:
            return pingpong_switch(k+1, p+1, up)
        else:
            return pingpong_switch(k+1, p-1, up)

    def pingpong_switch(k, p, up):
        if k % 7 == 0 or has_seven(k):
            return pingpong_next(k, p, not up)
        else:
            return pingpong_next(k, p, up)

    return pingpong_next(1, 1, True)
    """""
""""
def count_change(amount):
    return count_using(1, amount)

def count_using(min_coin, amount):
    if amount < 0:
        return 0
    elif amount == 0:
        return 1
    elif min_coin > amount:
        return 0
    else:
        with_min = count_using(min_coin, amount - min_coin)
        without_min = count_using(2*min_coin, amount)
        return with_min + without_min
"""""
""""
def towers_of_hanoi(n, start, end):
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    if n == 1:
        print("Move the top disk from rod", start, "to rod", end)
    else:
        other = 6 - start - end
        towers_of_hanoi(n-1, start, other)
        towers_of_hanoi(1, start, end)
        towers_of_hanoi(n-1, other, end)
        """""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

mjtyumi 发表于 2015-5-15 13:23:36 | 显示全部楼层
Q1:
def g(n):
        if n<=3:
                return n
        else:
                return g(n-1)+2*g(n-2)+3*g(n-3)

def g_iter(n):
        if n<=3:
                return n
        a,b,c=1,2,3
        while(n>3):
                a,b,c=b,c,c+2*b+3*a
                n-=1
        return c


Q2:
def has_seven(k):
        if k%10==7:
                return True
        elif k<=10:
                return False
        else:
                return has_seven(k//10)
Q3:

def has_seven(k):
        if k%10==7:
                return True
        elif k<=10:
                return False
        else:
                return has_seven(k//10)


def pingpong(n):
        if n<=7:
                return n
        elif (n-1)%7==0 or has_seven(n-1):
                return pingpong(n-2)
        else:
                return 2*pingpong(n-1)- pingpong(n-2)

Q4:

def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    return count_using(1, amount)

def count_using(min_coin,amount):
        if amount < 0:
                return 0
        elif amount == 0:
                return 1
        elif min_coin>amount:
                return 0
        else:
                with_min = count_using(min_coin,amount - min_coin)
                without_min= count_using(2*min_coin, amount)
                return with_min + without_min

Q5:
def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    if n == 1:
        print('Move the top disk from rod', start, 'to rod', end)
    else:
        towers_of_hanoi(n-1, start, 6-start-end)
        print('Move the top disk from rod', start, 'to rod', end)
        towers_of_hanoi(n-1, 6-start-end, end)


越来越难了。。。。

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

karte_polo 发表于 2015-5-15 23:19:40 | 显示全部楼层
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n<=3:
        return n
    else:
        return g(n-1)+2*g(n-2)+3*g(n-3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    "*** YOUR CODE HERE ***"
    i =1
    g_1,g_2,g_3 = 1,2,3
    while i<n:
        if i<=3:
            g= i
        else:
            g = g_3+2*g_2+3*g_3
            g_3,g_2,g_1 = g, g_3,g_2
        i+=1
    return g


def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    "*** YOUR CODE HERE ***"
    if k>0:
        if k%10==7:
            return True
        else:
            return False or has_seven(k//10)
    else:
        return False


def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    "*** YOUR CODE HERE ***"
    if n==1 or n==2:
        return n
    elif (n-1)%7==0 or has_seven(n-1):
        return pingpong(n-2)
    else:
        return 2*pingpong(n-1)-pingpong(n-2)


def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    maxc =1
    while maxc*2<=amount:
        maxc*=2
    def count_u(amount,max_coin):
        if max_coin==1:
            return 1
        if max_coin>amount:
            return count_change(amount)
        with_max = count_u(amount-max_coin,max_coin)
        without_max = count_u(amount,max_coin/2)
        return with_max+without_max
    return count_u(amount,maxc)

def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    "*** YOUR CODE HERE ***"
    if n==1:
        print("Move the top disk from rod",start,"to rod",end)
    else:
        other = 6-start-end
        towers_of_hanoi(n-1,start,other)
        towers_of_hanoi(1,start,end)
        towers_of_hanoi(n-1,other,end)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

MarcoLiang 发表于 2015-5-16 20:59:22 | 显示全部楼层
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    if n <= 3 :
        return n
    else :
        return g(n-1) + 2* g(n-2) + 3 * g(n-3)
        
def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    if n <= 3:
        return n
    else:
        i = 2
        pre, curr, next = 1, 2, 3
        while i < n :
            pre, curr, next = curr, next, next + 2*curr + 3*pre
            i += 1
        return curr        

def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    if k==7 :
        return True
    elif k % 10 == 7 :
        return True
    elif k > 10:
        return has_seven(k // 10)
    return False

def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    def up(k,i):
        k, i = k + 1, i + 1
        if i % 7== 0 or has_seven(i):
            return down(k,i)
        elif i != n:
            return up(k, i)
        else:
            return k
   
    def down(k,i):
        k, i = k - 1, i + 1
        if i % 7== 0 or has_seven(i):
            return up(k,i)
        elif i != n:
            return down(k,i)
        else:
            return k

    return up(1,1)
        

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

FFFelix 发表于 2015-5-18 12:57:53 | 显示全部楼层
pingpong那个不会 就看了答案理解了一下
其他的代码:
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    else:
        return g(n-1) + 2 * g(n-2) + 3 * g(n-3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n == 1 or n == 2 or n == 3:
        return n
    while n > 3:
        a, b, c = b, c,  c + 2 * b + 3 * c
        n -= 1
    return c


def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    "*** YOUR CODE HERE ***"
    if k % 10 == 7:
        return True
    elif k < 10:
        return False
    else:
        return has_seven(k // 10)


def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    "*** YOUR CODE HERE ***"
    def pingpong_next(k, p, up):
        if k == n:
            return p
        if up:
            return pingpong_switch(k+1, p+1, up)
        else:
            return pingpong_switch(k+1, p-1, up)
    def pingpong_switch(k, p, up):
        if k % 7 == 0 or has_seven(k):
            return pingpong_next(k, p, not up)
        else:
            return pingpong_next(k, p, up)

    return pingpong_next(1,1,True)



def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    def return_money(min,amount):
        if amount < 0:
            return 0
        elif amount == 0:
            return 1
        elif min > amount:
            return 0
        else:
            with_min = return_money(min, amount - min)
            without_min = return_money(2*min, amount)
        return without_min + with_min
    return return_money(1,amount)



def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    "*** YOUR CODE HERE ***"
    if n == 1:
        print("Move the top of the disk from rod", start, "to rod", end)
    else:
        third = 6 - start - end
        towers_of_hanoi(n-1, start, third)
        towers_of_hanoi(1, start, end)
        towers_of_hanoi(n-1, third, end)

from operator import sub, mul

def make_anonymous_factorial():
    """Return the value of an expression that computes factorial.

    >>> make_anonymous_factorial()(5)
    120
    """
    return 'YOUR_EXPRESSION_HERE'

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

FFFelix 发表于 2015-5-18 12:58:47 | 显示全部楼层
FFFelix 发表于 2015-5-18 12:57
pingpong那个不会 就看了答案理解了一下
其他的代码:
def g(n):

赶进度challenge problem就没有做
回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-26 07:41:50 | 显示全部楼层
才会写三个怎么办。。。感觉后面两个都得看答案
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    else:
        return g(n - 1) + 2 * g(n - 2) + 3 * g(n - 3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    a, b, c = 1, 2, 3
    while n > 3:
        a, b, c = b, c, c + 2 * b + 3 * a
        n -= 1
    return c


def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    "*** YOUR CODE HERE ***"
    if k < 10:
        if k == 7:
            return True
        else:
            return False
    elif k % 10 == 7:
        return True
    else:
        return has_seven(k // 10)


def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    "*** YOUR CODE HERE ***"
    def pingpong_next(k, p, up):
        if k == n:
            return p
        if up:
            return pingpong_switch(k + 1, p + 1, up)
        else:
            return pingpong_switch(k + 1, p - 1, up)

    def pingpong_switch(k, p, up):
        if k % 7 == 0 or has_seven(k):
            return pingpong_next(k, p, not up)
        else:
            return pingpong_next(k, p, up)

    return pingpong_next(1, 1, True)



def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    return count_using(1, amount)

def count_using(min_coin, amount):
    if amount < 0:
        return 0
    elif amount == 0:
        return 1
    elif min_coin > amount:
        return 0
    else:
        with_min = count_using(min_coin, amount -  min_coin)
        without_min = count_using(2 * min_coin, amount)
        return with_min + without_min


def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    "*** YOUR CODE HERE ***"
    if n == 1:
        print("Move the top disk from rod", start, "to rod", end)
    else:
        other = 6 - start - end
        towers_of_hanoi(n-1, start, other)
        towers_of_hanoi(1, start, end)
        towers_of_hanoi(n-1, other, end)
回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-17 17:50:03 | 显示全部楼层
这次作业还是挺有难度的。pingpong那题想了我半天,hanoi写出来不难,把游戏规则想清楚也花了好久哈哈,看了群友写的代码之后发现确实还可以改进!上代码:
#hw03
#Q1
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    if n >= 4:
        return g(n-1) + (2 * g(n-2)) + (3 * g(n-3))
        
def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return
    a , b, c = 1, 2, 3
    if n >= 4:
        while n > 3:
            a, b, c = b, c, c + 2*b + 3*a
            n = n - 1
        return c
        
#Q2
def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    "*** YOUR CODE HERE ***"
    if k // 10 == 0:
        if k % 10 == 7:
            return True
        if k % 10 != 7:
            return False
    if k % 10 == 7:
        return True
    if k % 10 != 7:
        return has_seven(k//10)
        
#Q3
def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    def pingpong_next(k, p, up):
        if k == n:
            return p
        if up:
            return pingpong_switch(k+1, p+1, up)
        else:
            return pingpong_switch(k+1, p-1, up)

    def pingpong_switch(k, p, up):
        if k % 7 == 0 or has_seven(k):
            return pingpong_next(k, p, not up)
        else:
            return pingpong_next(k, p, up)

    return pingpong_next(1, 1, True)
   
#Q4
def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    return count_min(1, amount)
   
def count_min(n,m):
    if n == m:
        return 1
    if n > m:
        return 0
    if m == 0:
        return 0
    else:
        with_min = count_min(n, m-n)
        without_min = count_min(2*n, m)
        return with_min + without_min   

#Q5
def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    "*** YOUR CODE HERE ***"
    if n == 1:
        print "Move the top disk from rod " + str(start) + " to rod " + str(end)
    else:
        index =  start + end
        if index == 3:
            index = 3
        if index == 4:
            index = 2
        if index == 5:
            index = 1
        towers_of_hanoi(n-1, start, index)
        towers_of_hanoi(1, start, end)
        towers_of_hanoi(n-1, index, end)
回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-1-27 07:47:26 | 显示全部楼层
不愧是Berkeley,感觉一年级的第一门课难度就相当恐怖阿。。。。。我也学过好几门CS/CE课了,但是依然觉得作业有些难度,pingpong那题做了好久,count_change想不到好方法,就直接省事用DP做了,最后一个challenge是看了答案才理解的。。感觉lambda表达式确实学的一般。。。不过赶进度就不深究了,如果完全按照伯克利学生的标准,我的智商也不够。。
完成截图:
hw03.png

代码:
  1. def g(n):
  2.         """Return the value of G(n), computed recursively.

  3.         >>> g(1)
  4.         1
  5.         >>> g(2)
  6.         2
  7.         >>> g(3)
  8.         3
  9.         >>> g(4)
  10.         10
  11.         >>> g(5)
  12.         22
  13.         """
  14.         "*** YOUR CODE HERE ***"
  15.         if n <= 3:
  16.                 return n
  17.         return g(n-1) + 2 * g(n-2) + 3 * g(n-3)

  18. def g_iter(n):
  19.         """Return the value of G(n), computed iteratively.

  20.         >>> g_iter(1)
  21.         1
  22.         >>> g_iter(2)
  23.         2
  24.         >>> g_iter(3)
  25.         3
  26.         >>> g_iter(4)
  27.         10
  28.         >>> g_iter(5)
  29.         22
  30.         """
  31.         "*** YOUR CODE HERE ***"
  32.         g_list = (n + 1) * [0]
  33.         for i in range(n + 1):
  34.                 if i <= 3:
  35.                         g_list[i] = i
  36.                 else:
  37.                         g_list[i] = g_list[i-1] + 2 * g_list[i-2] + 3 * g_list[i-3]
  38.         return g_list[n]


  39. def has_seven(k):
  40.         """Returns True if at least one of the digits of k is a 7, False otherwise.

  41.         >>> has_seven(3)
  42.         False
  43.         >>> has_seven(7)
  44.         True
  45.         >>> has_seven(2734)
  46.         True
  47.         >>> has_seven(2634)
  48.         False
  49.         >>> has_seven(734)
  50.         True
  51.         >>> has_seven(7777)
  52.         True
  53.         """
  54.         "*** YOUR CODE HERE ***"
  55.         if k % 10 == 7:
  56.                 return True
  57.         if k < 10:
  58.                 return False
  59.         return has_seven(k // 10)


  60. def pingpong(n):
  61.         """Return the nth element of the ping-pong sequence.

  62.         >>> pingpong(7)
  63.         7
  64.         >>> pingpong(8)
  65.         6
  66.         >>> pingpong(15)
  67.         1
  68.         >>> pingpong(21)
  69.         -1
  70.         >>> pingpong(22)
  71.         0
  72.         >>> pingpong(30)
  73.         6
  74.         >>> pingpong(68)
  75.         2
  76.         >>> pingpong(69)
  77.         1
  78.         >>> pingpong(70)
  79.         0
  80.         >>> pingpong(71)
  81.         1
  82.         >>> pingpong(72)
  83.         0
  84.         >>> pingpong(100)
  85.         2
  86.         """
  87.         "*** YOUR CODE HERE ***"
  88.         def helper(k, increament, tmp):
  89.                 if k == n:
  90.                         return tmp
  91. #                print("test", n, increament)
  92.                 if k % 7 == 0 or has_seven(k):
  93.                         increament = -increament
  94.                 return helper(k+1, increament, tmp+increament)

  95.         return helper(1, 1, 1)


  96. def count_change(amount):
  97.         """Return the number of ways to make change for amount.

  98.         >>> count_change(7)
  99.         6
  100.         >>> count_change(10)
  101.         14
  102.         >>> count_change(20)
  103.         60
  104.         >>> count_change(100)
  105.         9828
  106.         """
  107.         "*** YOUR CODE HERE ***"
  108.         arr = []
  109.         i = 1
  110.         while i <= amount:
  111.                 arr.append(i)
  112.                 i *= 2

  113.         res = (amount + 1) * [0]
  114.         res[0] = 1

  115.         for i in range(len(arr)):
  116.                 for j in range(arr[i], amount + 1):
  117.                         res[j] = res[j] + res[j-arr[i]]                # sum arr[i] and including a[[i]]

  118.         return res[amount]
  119.        

  120. def towers_of_hanoi(n, start, end):
  121.         """Print the moves required to solve the towers of hanoi game, starting
  122.         with n disks on the start pole and finishing on the end pole.

  123.         The game is to assumed to have 3 poles.

  124.         >>> towers_of_hanoi(1, 1, 3)
  125.         Move the top disk from rod 1 to rod 3
  126.         >>> towers_of_hanoi(2, 1, 3)
  127.         Move the top disk from rod 1 to rod 2
  128.         Move the top disk from rod 1 to rod 3
  129.         Move the top disk from rod 2 to rod 3
  130.         >>> towers_of_hanoi(3, 1, 3)
  131.         Move the top disk from rod 1 to rod 3
  132.         Move the top disk from rod 1 to rod 2
  133.         Move the top disk from rod 3 to rod 2
  134.         Move the top disk from rod 1 to rod 3
  135.         Move the top disk from rod 2 to rod 1
  136.         Move the top disk from rod 2 to rod 3
  137.         Move the top disk from rod 1 to rod 3
  138.         """
  139.         assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
  140.         "*** YOUR CODE HERE ***"
  141.         def helper(n, start, end, empty):
  142.                 if n == 1:
  143.                         print("Move the top disk from rod %d to rod %d" % (start, end))
  144.                         return
  145.                 helper(n-1, start, empty, end)
  146.                 helper(1, start, end, empty)
  147.                 helper(n-1, empty, end, start)

  148.         return helper(n, start, end, 6 - start - end)


  149. from operator import sub, mul

  150. def make_anonymous_factorial():
  151.         """Return the value of an expression that computes factorial.

  152.         >>> make_anonymous_factorial()(5)
  153.         120
  154.         """
  155.         return lambda n: (lambda f, v: f(f, v))(lambda f, v: 1 if v == 1 else mul(v, f(f, sub(v, 1))), n)
复制代码
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-2-14 00:14:18 | 显示全部楼层
最后一道还是放弃了,不知道有没有人能推荐可以比较透彻学lambda用法的
def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    """
    "*** YOUR CODE HERE ***"
    if n == 1:
        return 1
    elif n == 2:
        return 2
    elif n == 3:
        return 3
    else:
        return g(n - 1) + 2 * g(n - 2) + 3 * g(n - 3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    """
    "*** YOUR CODE HERE ***"
    a = 1
    b = 2
    c = 3
    if n < 4:
        gn = n
    for i in range(4, n+1):
        gn = c + 2 * b + 3 * a
        a = b
        b = c
        c = gn
    return gn

def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    "*** YOUR CODE HERE ***"
    if (k % 10) % 7 == 0:
        return True
    elif k // 10 == 0:
        return False
    else:
        return has_seven(k // 10)


def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    """
    "*** YOUR CODE HERE ***"
    def count(n):
        if n == 1:
            return 0
        elif (n-1) % 7 == 0 or (n-1) % 10 == 7 or (n-1) // 10 == 7:
            return count(n-1) + 1
        else:
            return count(n-1)
    if n == 1:
        return 1
    else:
        return pingpong(n-1) + pow(-1, count(n))


def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    def count(amount , parts):
        if amount == 0:
            return 1
        elif amount < 0:
            return 0
        elif parts > amount:
            return 0
        else:
            with_parts = count(amount - parts, parts)
            without_parts = count(amount, parts * 2)
            return with_parts + without_parts
    return count(amount, 1)


def towers_of_hanoi(n, start, end):
    """Print the moves required to solve the towers of hanoi game, starting
    with n disks on the start pole and finishing on the end pole.

    The game is to assumed to have 3 poles.

    >>> towers_of_hanoi(1, 1, 3)
    Move the top disk from rod 1 to rod 3
    >>> towers_of_hanoi(2, 1, 3)
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 3
    >>> towers_of_hanoi(3, 1, 3)
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 1 to rod 2
    Move the top disk from rod 3 to rod 2
    Move the top disk from rod 1 to rod 3
    Move the top disk from rod 2 to rod 1
    Move the top disk from rod 2 to rod 3
    Move the top disk from rod 1 to rod 3
    """
    assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
    "*** YOUR CODE HERE ***"
    if n == 1:
        print ('Move the top disk from rod', start, 'to rod', end)
    else:
        other = 6 - start - end
        towers_of_hanoi(n-1, start, other)
        towers_of_hanoi(1, start, end)
        towers_of_hanoi(n-1, other, end)


from operator import sub, mul

def make_anonymous_factorial():
    """Return the value of an expression that computes factorial.

    >>> make_anonymous_factorial()(5)
    120
    """
    return 'YOUR_EXPRESSION_HERE'
回复 支持 反对

使用道具 举报

wkh279 发表于 2016-11-20 21:33:49 | 显示全部楼层
Question 6: Challenge Problem想了好久没想出来,看了答案才明白
这次作业难度有点呀
  1. def g(n):
  2.     """Return the value of G(n), computed recursively.

  3.     >>> g(1)
  4.     1
  5.     >>> g(2)
  6.     2
  7.     >>> g(3)
  8.     3
  9.     >>> g(4)
  10.     10
  11.     >>> g(5)
  12.     22
  13.     """
  14.     if n<=3:
  15.         return n
  16.     else:
  17.         return g(n-1)+g(n-2)*2+g(n-3)*3

  18. def g_iter(n):
  19.     """Return the value of G(n), computed iteratively.

  20.     >>> g_iter(1)
  21.     1
  22.     >>> g_iter(2)
  23.     2
  24.     >>> g_iter(3)
  25.     3
  26.     >>> g_iter(4)
  27.     10
  28.     >>> g_iter(5)
  29.     22
  30.     """
  31.     a,b,c,d=1,2,3,10
  32.     for _ in range(1,n):
  33.         a,b,c,d=b,c,d,3*b+2*c+d
  34.     return a

  35. def has_seven(k):
  36.     """Returns True if at least one of the digits of k is a 7, False otherwise.

  37.     >>> has_seven(3)
  38.     False
  39.     >>> has_seven(7)
  40.     True
  41.     >>> has_seven(2734)
  42.     True
  43.     >>> has_seven(2634)
  44.     False
  45.     >>> has_seven(734)
  46.     True
  47.     >>> has_seven(7777)
  48.     True
  49.     """
  50.     if k<10:
  51.         if k==7:
  52.             return True
  53.         return False
  54.     else:
  55.         return has_seven(k//10) or has_seven(k%10)

  56. def pingpong(n):
  57.     """Return the nth element of the ping-pong sequence.

  58.     >>> pingpong(7)
  59.     7
  60.     >>> pingpong(8)
  61.     6
  62.     >>> pingpong(15)
  63.     1
  64.     >>> pingpong(21)
  65.     -1
  66.     >>> pingpong(22)
  67.     0
  68.     >>> pingpong(30)
  69.     6
  70.     >>> pingpong(68)
  71.     2
  72.     >>> pingpong(69)
  73.     1
  74.     >>> pingpong(70)
  75.     0
  76.     >>> pingpong(71)
  77.     1
  78.     >>> pingpong(72)
  79.     0
  80.     >>> pingpong(100)
  81.     2
  82.     """
  83.     if n<8:
  84.         return n
  85.     # function up_down returns a value, which equals to pingpong(m+1)-pingpong(m)
  86.     def up_down(m):
  87.         if m<7:
  88.             return 1
  89.         if m%7==0 or has_seven(m):
  90.             return -1*up_down(m-1)
  91.         else:
  92.             return up_down(m-1)
  93.     return pingpong(n-1)+up_down(n-1)


  94.    
  95. def count_change(amount):
  96.     """Return the number of ways to make change for amount.

  97.     >>> count_change(7)
  98.     6
  99.     >>> count_change(10)
  100.     14
  101.     >>> count_change(20)
  102.     60
  103.     >>> count_change(100)
  104.     9828
  105.     """
  106.     def count(min_change,amount):
  107.         if amount<0:
  108.             return 0
  109.         elif amount==0:
  110.             return 1
  111.         elif amount<min_change:
  112.             return 0
  113.         else:
  114.             return count(min_change,amount-min_change)+count(min_change*2,amount)
  115.     return count(1,amount)
  116.    

  117. def towers_of_hanoi(n, start, end):
  118.     """Print the moves required to solve the towers of hanoi game, starting
  119.     with n disks on the start pole and finishing on the end pole.

  120.     The game is to assumed to have 3 poles.

  121.     >>> towers_of_hanoi(1, 1, 3)
  122.     Move the top disk from rod 1 to rod 3
  123.     >>> towers_of_hanoi(2, 1, 3)
  124.     Move the top disk from rod 1 to rod 2
  125.     Move the top disk from rod 1 to rod 3
  126.     Move the top disk from rod 2 to rod 3
  127.     >>> towers_of_hanoi(3, 1, 3)
  128.     Move the top disk from rod 1 to rod 3
  129.     Move the top disk from rod 1 to rod 2
  130.     Move the top disk from rod 3 to rod 2
  131.     Move the top disk from rod 1 to rod 3
  132.     Move the top disk from rod 2 to rod 1
  133.     Move the top disk from rod 2 to rod 3
  134.     Move the top disk from rod 1 to rod 3
  135.     """
  136.     assert 0 < start <= 3 and 0 < end <= 3 and start != end, "Bad start/end"
  137.     if n==1:
  138.             print("Move the top disk from rod "+str(start)+" to rod "+str(end))
  139.     else:
  140.             _= towers_of_hanoi(n-1,start,6-start-end)
  141.             _= towers_of_hanoi(1,start,end)
  142.             _= towers_of_hanoi(n-1,6-start-end,end)

  143. from operator import sub, mul

  144. def make_anonymous_factorial():
  145.     """Return the value of an expression that computes factorial.

  146.     >>> make_anonymous_factorial()(5)
  147.     120
  148.     """
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2016-12-11 11:13

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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