📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3024| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

[cs61a]lab11

全局:
公开课
学校名称: UCB
Unit号: 7
开课时间: 4
课程全名: CS61A
平台: 其他

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

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
LAB11:http://gaotx.com/cs61a/lab/lab11/
My solution:http://gaotx.com/blogs/2015/06/13/cs61a-lab11/

上一篇:[cs61a]HW08
下一篇:[cs61a]disc10
🔗
西风吹草 2015-6-30 23:42:18 | 只看该作者
全局:
Question 2:
class IteratorRestart:
        def __init__(self, start, end):
                self.start = start
                self.end = end
                self.current = start
        def __next__(self):
                if self.current > self.end:
                        raise StopIteration
                self.current += 1
                return self.current - 1
        def __iter__(self):
                self.current = self.start
                return self

Question 3:
def countdown(n):
        while n >= 0:
                yield n
                n -= 1

class Countdown:
        def __init__(self, n):
                self.number = n
        def __next__(self):
                if self.number < 0:
                        raise StopIteration
                self.number -= 1
                return self.number + 1
        def __iter__(self):
                return self

Question 4:
def hailstone(n):
        yield n
        while n > 1:
                if n % 2 == 0:
                        yield n // 2
                        n = n // 2
                else:
                        yield n * 3 + 1
                        n = n * 3 + 1

Question 5:
def add_streams(s1, s2):
        def compute_rest():
                return add_streams(s1.rest, s2.rest)
        return Stream(s1.first + s2.first, compute_rest)

Question 6:
def make_fib_stream():
        def compute_rest():
                return add_streams(make_fib_stream(), make_fib_stream().rest)
        return Stream(0, lambda: Stream(1, compute_rest))

Question 8:
def interleave(stream1, stream2):
        if stream1 is Stream.empty:
                return Stream.empty
        return Stream(stream1.first, lambda: interleave(stream2, stream1.rest))
回复

使用道具 举报

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

本版积分规则

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