📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: sky420
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Homework02

🔗
goldpanda 2015-5-12 10:20:16 | 只看该作者
全局:
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    if n == 1:
        return f
    else:
        return compose1(f, repeated(f, n-1))


def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return lambda x : f(x)

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return lambda x : f(f(x))

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    return n(lambda x : x+1)(0)

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    return lambda f : lambda x : m(f)(n(f)(x))

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    return lambda f : m(n(f))

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    return n(m)
回复

使用道具 举报

🔗
LuckyBamboo 2015-5-12 11:35:56 | 只看该作者
全局:
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x < b:
            return f
        else:
            return g
    return h


def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        i = 0
        while i < n:
            x, i = f(x), k + 1
        return x
    return h



def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    "*** YOUR CODE HERE ***"
    return lambda x: f(x)


def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    "*** YOUR CODE HERE ***"
    return successor(two)


three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    "*** YOUR CODE HERE ***"
    return n(lambda x: x + 1)(0)



def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    "*** YOUR CODE HERE ***"
    return lambda: lambda x: m(f)(n(f)(x))

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    "*** YOUR CODE HERE ***"
    return lambda f: m(n(f))


def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    "*** YOUR CODE HERE ***"
    retun n(m)

回复

使用道具 举报

🔗
jy_121 2015-5-12 21:32:54 | 只看该作者
全局:
Question1:

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
        def h(x):
            if x < b:
                return f(x)
            return g(x)
        return h
   


Question2:

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    g = f
    while n > 1:
        g = compose1(f, g)
        n = n - 1
    return g

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h



第三题真心不会,求讲解。。。
回复

使用道具 举报

🔗
wuxiaomin98 2015-5-23 05:51:00 | 只看该作者
全局:
def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    g = f
    while n > 1:
        g = compose1(f, g)
        n -= 1
    return g


def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

第三题真心看不懂successor怎么定义的
回复

使用道具 举报

无效楼层,该帖已经被删除
全局:
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h
   
def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    def h(x):
        i=0
        while i < n:
            x=f(x)
            i=i+1
        return x
    return h

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################
def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return lambda x: f(x)

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return lambda x: f(f(x))

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    return n(lambda x:x+1)(0)

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    return lambda f: lambda x: m(f)((n(f)(x)))

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    return lambda f: m(n(f))

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    return lambda n(m)
回复

使用道具 举报

🔗
reasonapp 2015-6-3 22:49:05 | 只看该作者
全局:
lambda到底是个啥,第三题跪了
#hw02
#Q1
def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def back(x):
            if x < b:
                    return f(x)
            else:
                    return g(x)
    return back
   
#Q2
def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        i = 1
        while i <= n:
            x, i = f(x), i + 1
        return x
    return h
        

Q3:
。。。。。
回复

使用道具 举报

🔗
wkh279 2015-7-17 20:12:00 | 只看该作者
全局:
本帖最后由 wkh279 于 2015-7-17 20:13 编辑

最近才开始跟课程,贴一下代码哈
最后几问看了下答案,的确没有答案简

def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
        if x<b: return f(x)
        else: return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    g=f
    for i in range(n-1):
        g=compose1(f,g)
    return g

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return f

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return compose1(f,f)

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    return n(increment)(0)

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    x,y=church_to_int(m),church_to_int(n)
    def g(f):
        return repeated(f,x+y)
    return g

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    x,y=church_to_int(m),church_to_int(n)
    def g(f):
        return repeated(f,x*y)
    return g

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    x,y=church_to_int(m),church_to_int(n)
    def g(f):
        return repeated(f,x**y)
    return g

回复

使用道具 举报

🔗
rockleecsu 2015-12-30 13:17:41 | 只看该作者
全局:
这次那道challenge题好难阿。。现在仍旧没有理解,尤其是后面三问。。不过用取巧的方法解出来了。。
代码和图片如下,求加分!!!
  1. def square(x):
  2.         return x * x

  3. def triple(x):
  4.         return 3 * x

  5. def identity(x):
  6.         return x

  7. def increment(x):
  8.         return x + 1

  9. def piecewise(f, g, b):
  10.         """Returns the piecewise function h where:

  11.         h(x) = f(x) if x < b,
  12.                    g(x) otherwise

  13.         >>> def negate(x):
  14.         ...     return -x
  15.         >>> abs_value = piecewise(negate, identity, 0)
  16.         >>> abs_value(6)
  17.         6
  18.         >>> abs_value(-1)
  19.         1
  20.         """
  21.         "*** YOUR CODE HERE ***"
  22.         def compare(x):
  23.                 if x < b:
  24.                         return f(x)
  25.                 else:
  26.                         return g(x)

  27.         return compare

  28. def repeated(f, n):
  29.         """Return the function that computes the nth application of f.

  30.         >>> add_three = repeated(increment, 3)
  31.         >>> add_three(5)
  32.         8
  33.         >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
  34.         243
  35.         >>> repeated(square, 2)(5) # square(square(5))
  36.         625
  37.         >>> repeated(square, 4)(5) # square(square(square(square(5))))
  38.         152587890625
  39.         """
  40.         "*** YOUR CODE HERE ***"
  41.         i = 1
  42.         g = f
  43.         while i < n:
  44.                 g = compose1(f, g)
  45.                 i += 1

  46.         return g

  47. def compose1(f, g):
  48.         """Return a function h, such that h(x) = f(g(x))."""
  49.         def h(x):
  50.                 return f(g(x))
  51.         return h

  52. ###################
  53. # Church Numerals #
  54. ###################

  55. def zero(f):
  56.         return lambda x: x

  57. def successor(n):
  58.         return lambda f: lambda x: f(n(f)(x))

  59. def one(f):
  60.         """Church numeral 1: same as successor(zero)"""
  61.         "*** YOUR CODE HERE ***"
  62.         return lambda x: f(x)
  63.        

  64. def two(f):
  65.         """Church numeral 2: same as successor(successor(zero))"""
  66.         "*** YOUR CODE HERE ***"
  67.         return lambda x: f(f(x))

  68. three = successor(two)

  69. def church_to_int(n):
  70.         """Convert the Church numeral n to a Python integer.

  71.         >>> church_to_int(zero)
  72.         0
  73.         >>> church_to_int(one)
  74.         1
  75.         >>> church_to_int(two)
  76.         2
  77.         >>> church_to_int(three)
  78.         3
  79.         """
  80.         "*** YOUR CODE HERE ***"
  81.         return n(increment)(0)


  82. def add_church(m, n):
  83.         """Return the Church numeral for m + n, for Church numerals m and n.

  84.         >>> church_to_int(add_church(two, three))
  85.         5
  86.         """
  87.         "*** YOUR CODE HERE ***"
  88.         x = church_to_int(m)
  89.         y = church_to_int(n)
  90.         return lambda f: repeated(f, x + y)                                                                                                                                                                                               

  91. def mul_church(m, n):
  92.         """Return the Church numeral for m * n, for Church numerals m and n.

  93.         >>> four = successor(three)
  94.         >>> church_to_int(mul_church(two, three))
  95.         6
  96.         >>> church_to_int(mul_church(three, four))
  97.         12
  98.         """
  99.         "*** YOUR CODE HERE ***"
  100.         x = church_to_int(m)
  101.         y = church_to_int(n)
  102.         return lambda f: repeated(f, x * y)       

  103. def pow_church(m, n):
  104.         """Return the Church numeral m ** n, for Church numerals m and n.

  105.         >>> church_to_int(pow_church(two, three))
  106.         8
  107.         >>> church_to_int(pow_church(three, two))
  108.         9
  109.         """
  110.         "*** YOUR CODE HERE ***"
  111.         x = church_to_int(m)
  112.         y = church_to_int(n)
  113.         return lambda f: repeated(f, x ** y)       
复制代码
回复

使用道具 举报

🔗
Liaeve 2016-2-5 22:51:25 | 只看该作者
全局:
def square(x):
    return x * x

def triple(x):
    return 3 * x
第3题写到一半放弃了,lambda始终不太明白


def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def nthf(x):
         i = 1
         result = f(x)
         while i < n:
            result = f(result)
            i = i + 1
         return result
    return nthf

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    "*** YOUR CODE HERE ***"
    return lambda x: f(x)

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    "*** YOUR CODE HERE ***"
    return lambda x: f(f(x))

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    "*** YOUR CODE HERE ***"
    return n(lambda x : x + 1)( 0 )

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    "*** YOUR CODE HERE ***"

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    "*** YOUR CODE HERE ***"

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    "*** YOUR CODE HERE ***"
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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