楼主: lh6210
跳转到指定楼层
上一主题 下一主题
收起左侧

跟课 CS61A- Fall 2015

🔗
 楼主| lh6210 2015-11-4 16:54:19 | 只看该作者
全局:
Fri  9/18, Midterm后的第一个视频中,John DeNero老师说到 考试被 fire alarm打断,同学不得不更换了考场,另外有同学share了考试题目答案(this is cheating),为了更公平,在 fire alarm之前未交卷的同学们需要重新参加考试。
最重要的是, John DeNero 老师 give an advice about life, 能听到大牛老师的指点真是极其宝贵的,下面是听写,有不对的地方欢迎指正:
“A big part of living a happy life is holding yourself in high esteem, you're very bright and you worked hard, that's why you're here, but it's not enough. And you would likely be very successful, regardless of your major, especially if you've taken some computer science courses that prepare you for the world ahead. But that's not enough. However, knowing you have acted with integrity that you've made difference in the world that you build relationships based on trust instead of mutual benefits. Well that feels really really good. It's not too late, but college is an excellent time to start leading an honest life, if you haven't already. You won't regret it.”
回复

使用道具 举报

🔗
 楼主| lh6210 2015-11-9 16:53:49 | 只看该作者
全局:
2.3.3  Sequence Processing

divisors_of(n)的不同表示:
  1. def divisors1(n):
  2.     return [x for x in range(1,n) if n%x==0]
复制代码
  1. def keep_if(filter_fn, s):
  2.     return [x for x in s if filter_fn(x)]

  3. def divisors2(n):
  4.     def filter_fn(x):
  5.         return n%x==0
  6.     return keep_if(filter_fn, range(1,n))

  7. def divisors3(n):
  8.     filter_fn = lambda x: n%x==0
  9.     return keep_if(filter_fn, range(1,n))
复制代码
perfect_numbers(n) 的不同表示:
  1. def keep_if(filter_fn, s):
  2.     return [x for x in s if filter_fn(x)]

  3. def perfect_numbers1(n):
  4.     def perfect(x):
  5.         return sum(divisors1(x)) == x
  6.     return keep_if(perfect, range(1,n))
复制代码
  1. from operator import add
  2. def reduce(map_fn , s, initial=0):
  3.     reduced = initial
  4.     for x in s:
  5.         reduced = map_fn(reduced, x)
  6.     return reduced

  7. def sum_of_divisors(n):
  8.     return reduce(add, divisors1(n), 0)

  9. def perfect_numbers2(n):
  10.     def perfect(x):
  11.         return sum_of_divisors(x) == x
  12.     return keep_if(perfect, range(1,n))
复制代码
回复

使用道具 举报

🔗
 楼主| lh6210 2015-11-12 13:10:05 | 只看该作者
全局:
hw02:
  1. def piecewise(f, g, b):
  2.     """Returns the piecewise function h where:

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

  5.     >>> def negate(x):
  6.     ...     return -x
  7.     >>> abs_value = piecewise(negate, identity, 0)
  8.     >>> abs_value(6)
  9.     6
  10.     >>> abs_value(-1)
  11.     1
  12.     """
  13.     "*** YOUR CODE HERE ***"
  14.     def h(x):
  15.       if x < b:
  16.         return f(x)
  17.       else:
  18.         return g(x)
  19.     return h
复制代码
  1. def product(n, term):
  2.     """Return the product of the first n terms in a sequence.

  3.     n    -- a positive integer
  4.     term -- a function that takes one argument

  5.     >>> product(3, identity) # 1 * 2 * 3
  6.     6
  7.     >>> product(5, identity) # 1 * 2 * 3 * 4 * 5
  8.     120
  9.     >>> product(3, square)   # 1^2 * 2^2 * 3^2
  10.     36
  11.     >>> product(5, square)   # 1^2 * 2^2 * 3^2 * 4^2 * 5^2
  12.     14400
  13.     """
  14.     "*** YOUR CODE HERE ***"
  15.     total, k = 1,1
  16.     while k<=n:
  17.       total = total * term(k)
  18.       k = k+1
  19.     return total
复制代码
  1. def accumulate(combiner, base, n, term):
  2.     """Return the result of combining the first n terms in a sequence.

  3.     >>> accumulate(add, 0, 5, identity)  # 0 + 1 + 2 + 3 + 4 + 5
  4.     15
  5.     >>> accumulate(add, 11, 5, identity) # 11 + 1 + 2 + 3 + 4 + 5
  6.     26
  7.     >>> accumulate(add, 11, 0, identity) # 11
  8.     11
  9.     >>> accumulate(add, 11, 3, square)   # 11 + 1^2 + 2^2 + 3^2
  10.     25
  11.     >>> accumulate(mul, 2, 3, square)   # 2 * 1^2 * 2^2 * 3^2
  12.     72
  13.     """
  14.     "*** YOUR CODE HERE ***"
  15.     k=1
  16.     while k<=n:
  17.         base = combiner(base, term(k))
  18.         k +=1
  19.     return base
复制代码
  1. def summation_using_accumulate(n, term):
  2.     """An implementation of summation using accumulate.

  3.     >>> summation_using_accumulate(5, square)
  4.     55
  5.     >>> summation_using_accumulate(5, triple)
  6.     45
  7.     """
  8.     "*** YOUR CODE HERE ***"
  9.     return accumulate(add, 0, n, term)
复制代码
  1. def repeated(f, n):
  2.     """Return the function that computes the nth application of f.

  3.     >>> add_three = repeated(increment, 3)
  4.     >>> add_three(5)
  5.     8
  6.     >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
  7.     243
  8.     >>> repeated(square, 2)(5) # square(square(5))
  9.     625
  10.     >>> repeated(square, 4)(5) # square(square(square(square(5))))
  11.     152587890625
  12.     """
  13.     "*** YOUR CODE HERE ***"
  14.     def h(x):
  15.         k, answer = 0, x
  16.         while k<n:
  17.             answer = f(answer)
  18.             k +=1
  19.         return answer
  20.     return h
复制代码
  1. def compose1(f, g):
  2.     """Return a function h, such that h(x) = f(g(x))."""
  3.     def h(x):
  4.         return f(g(x))
  5.     return h

  6. def compose1_plus(f,g):
  7.     return lambda x: f(g(x))
复制代码
回复

使用道具 举报

🔗
 楼主| lh6210 2015-11-12 15:35:18 | 只看该作者
全局:
本帖最后由 lh6210 于 2015-11-12 15:57 编辑

hw02 extra:
Church numerals:
  1. def zero(f):
  2.     return lambda x:x

  3. def one(f):
  4.     return lambda x:f(x)

  5. def two(f):
  6.     return lambda x:f(f(x))

  7. def successor(n):
  8.     return lambda f:lambda x: f(n(f)(x))

  9. three = successor(two)
  10. four = successor(three)
复制代码
这道题,首先定义 zero, one, two, three, four 这5个 church numerals,他们会作为argument  放入后续定义的函数中。 successor(church numeral)函数 input/output都是 function,是一个 higher-order function.
  1. def church_to_int(n):
  2.     return n(lambda x: x+1)(0)
复制代码
church_to_int (church numeral)函数 input是 function, return value是 integer。之前定义的 zero, one, two ,three, four都可以 input 进去验证一下。
接下来是  church numerals 的运算:
  1. def add_church(m,n):
  2.     return lambda f: lambda x: m(f)(n(f)(x))

  3. def mul_church(m,n):
  4.     return lambda f: m(n(f))

  5. def pow_church(m,n):
  6.     return n(m)
复制代码
验证一下   three == add_church(one, two):


比较   mul_church()  与  pow_church() 的区别:
  1. mul_church  =>   m(n(f))
  2. m =3, n=2
  3.                         =>   3(2(f))
  4.                         =>   (\f.\x.f(f(fx)))((\f.\x.f(fx))f)
  5.                         =>   ........        (\x.f(fx))
  6.                         =>   (\x.f(fx))(\x.f(fx))(\x.f(fx))
  7.                         =>   6(f)

  8.                         
  9. pow_church  =>   m(n)
  10. m =3, n=2
  11.                         =>   3(2)
  12.                         =>   (\f.\x.f(f(fx)))(\f.\x.f(fx))
  13.                         =>   (\f.\x.f(fx))(\f.\x.f(fx))(\f.\x.f(fx))
  14.                         =>   ......                  (\f.\x.f(f(f(fx))))
  15.                         =>   (\f.\x.f(f(f(fx))))(\f.\x.f(f(f(fx))))
  16.                         =>   8
复制代码

add_church .png (6.44 KB, 下载次数: 1)

add_church

add_church
回复

使用道具 举报

🔗
 楼主| lh6210 2015-11-13 14:49:49 | 只看该作者
全局:
discussion02:

请教一下, 这道题大家看出结果了吗?
Draw the environment diagram for the following code.
  1. y= "y"
  2. h =y
  3. def y(y):
  4.     h = "h"
  5.     if y ==h:
  6.         return y + "i"
  7.     y = lambda y : y(h)
  8.     return lambda h:y(h)
  9. y =y(y)(y)
复制代码



回复

使用道具 举报

🔗
if19870314 2015-11-16 00:27:53 | 只看该作者
全局:
楼主你没有用那个GITBASH吗?
之前在学C和JAVA,都是有平台编译然后运行出结果。
这个捣鼓了半天没明白到底在哪编程序哪里看结果,都晕掉了。
貌似分开的?又貌似一边编译一边出结果?
回复

使用道具 举报

🔗
 楼主| lh6210 2015-11-16 10:09:00 | 只看该作者

调试时的图片

全局:
如题,调试时的图片

practise.png (58.53 KB, 下载次数: 1)

practise.png
回复

使用道具 举报

🔗
 楼主| lh6210 2015-11-16 10:12:31 | 只看该作者
全局:
if19870314 发表于 2015-11-16 00:27
楼主你没有用那个GITBASH吗?
之前在学C和JAVA,都是有平台编译然后运行出结果。
这个捣鼓了半天没明白到 ...

我也用 Gitbash的,不过都是在 命令行调试完成后,觉得没有问题了,然后再在 Gitbash 上面用验证命令确认下。 验证方法是 python ok -q xxx。

在平时练习时,我一直用的是 text editor 和 cmd
回复

使用道具 举报

🔗
if19870314 2015-11-16 11:37:16 | 只看该作者
全局:
lh6210 发表于 2015-11-16 10:09
如题,调试时的图片

谢谢楼主,终于明白了,
那个python -i xxx
的指令是额外看书学到的吗?
我跟视频和网站提供的资料貌似没有提到?
导致一直不知道怎么测试代码。。、
不过我只看到第一周。文档里是直接 python ok
回复

使用道具 举报

🔗
if19870314 2015-11-16 11:38:23 | 只看该作者
全局:
lh6210 发表于 2015-11-16 10:12
我也用 Gitbash的,不过都是在 命令行调试完成后,觉得没有问题了,然后再在 Gitbash 上面用验证命令确认 ...

还有python ok -q xxx
也在课里貌似没看到过
回复

使用道具 举报

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

本版积分规则

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