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

[CS61A]Lab08

全局:
公开课
学校名称: UCBerkely
Unit号: 05
开课时间: 04
课程全名: CS61A
平台: 其他

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

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

x
Lab address: http://gaotx.com/cs61a/lab/lab08/
My Solution: http://gaotx.com/blogs/2015/06/01/cs61a-lab08/

上一篇:[CS61A]Disc07
下一篇:[CS61A]Lab09
🔗
西风吹草 2015-6-9 21:37:26 | 只看该作者
全局:
Question 1:
def slice_link(link, start, end):
        if end == 0:
                return Link.empty
        elif start == 0:
                return Link(link.first, slice_link(link.rest, 0, end - 1))
        else:
                return slice_link(link.rest, start - 1, end - 1)

Question 2:
def union(s1, s2):
        union_set = set()
        {union_set.add(element) for element in s1}
        {union_set.add(element) for element in s2}
        return union_set

Question 3:
def intersection(s1, s2):
        intersection_set = set()
        for element in s2:
                if element in s1:
                        intersection_set.add(element)
        return intersection_set

Question 4:
def extra_elem(a,b):
        i = 0
        while i < len(a):
                b.remove(a[i])
                i += 1
        return b[0]

Question 5:
def find_duplicates(lst):
        return len(lst) != len(set(lst))

Question 6:
def add_up(n, lst):
        new_set = set()
        for element in lst:
                if n != 2 * element:
                        new_set.add(n - element)
        return bool(intersection(new_set, set(lst)))

Question 7:
def pow(n, k):
        if k == 1:
                return n
        elif k % 2 == 0:
                return pow(n * n, k // 2)
        else:
                return n * pow(n * n, k // 2)

Question 8:
def missing_no(lst):
        ordered_lst = list(set(lst))
        length = len(ordered_lst)
        if ordered_lst[length - 1] == length - 1:
                return length
        else:
                i = 0
                while i < length:
                        if ordered_lst[i] != i:
                                return i
                        i += 1

Question 9:
def find_duplicates_k(k, lst):
        i, n = 0, len(lst)
        while i <= n - k - 1:
                temp_lst = lst[i : i + k + 1]
                if len(temp_lst) != len(list(set(temp_lst))):
                        return True
                i += 1
        return False
回复

使用道具 举报

🔗
rockleecsu 2016-2-24 08:11:00 | 只看该作者
全局:
交一下作业,只做了必做部分:
  1. ## Linked Lists and Sets ##

  2. # Linked Lists

  3. class Link:
  4.         """A linked list.

  5.         >>> s = Link(1, Link(2, Link(3, Link(4))))
  6.         >>> len(s)
  7.         4
  8.         >>> s[2]
  9.         3
  10.         >>> s
  11.         Link(1, Link(2, Link(3, Link(4))))
  12.         """
  13.         empty = ()

  14.         def __init__(self, first, rest=empty):
  15.                 assert rest is Link.empty or isinstance(rest, Link)
  16.                 self.first = first
  17.                 self.rest = rest

  18.         def __getitem__(self, i):
  19.                 if i == 0:
  20.                         return self.first
  21.                 else:
  22.                         return self.rest[i-1]

  23.         def __len__(self):
  24.                 return 1 + len(self.rest)

  25.         def __repr__(self):
  26.                 if self.rest is not Link.empty:
  27.                         rest_str = ', ' + repr(self.rest)
  28.                 else:
  29.                         rest_str = ''
  30.                 return 'Link({0}{1})'.format(repr(self.first), rest_str)

  31. def slice_link(link, start, end):
  32.         """Slices a Link from start to end (as with a normal Python list).

  33.         >>> link = Link(3, Link(1, Link(4, Link(1, Link(5, Link(9))))))
  34.         >>> slice_link(link, 1, 4)
  35.         Link(1, Link(4, Link(1)))
  36.         """
  37.         "*** YOUR CODE HERE ***"
  38.         if start > 0:
  39.                 return slice_link(link.rest, start - 1, end - 1)
  40.         elif start == end:
  41.                 return Link.empty
  42.         else:
  43.                 res = Link(link.first)
  44.                 res.rest = slice_link(link.rest, start, end - 1)
  45.                 return res


  46. # Sets

  47. def union(s1, s2):
  48.         """Returns the union of two sets.

  49.         >>> r = {0, 6, 6}
  50.         >>> s = {1, 2, 3, 4}
  51.         >>> t = union(s, {1, 6})
  52.         >>> t
  53.         {1, 2, 3, 4, 6}
  54.         >>> union(r, t)
  55.         {0, 1, 2, 3, 4, 6}
  56.         """
  57.         "*** YOUR CODE HERE ***"
  58.         union_set = set()
  59.         for element in s1:
  60.                 union_set.add(element)
  61.         for element in s2:
  62.                 if element not in s1:
  63.                         union_set.add(element)
  64.         return union_set
  65.        

  66. def intersection(s1, s2):
  67.         """Returns the intersection of two sets.

  68.         >>> r = {0, 1, 4, 0}
  69.         >>> s = {1, 2, 3, 4}
  70.         >>> t = intersection(s, {3, 4, 2})
  71.         >>> t
  72.         {2, 3, 4}
  73.         >>> intersection(r, t)
  74.         {4}
  75.         """
  76.         "*** YOUR CODE HERE ***"
  77.         intersection_set = set()
  78.         for elem in s1:
  79.                 if elem in s2:
  80.                         intersection_set.add(elem)
  81.         return intersection_set

  82. def extra_elem(a,b):
  83.         """B contains every element in A, and has one additional member, find
  84.         the additional member.

  85.         >>> extra_elem(['dog', 'cat', 'monkey'],  ['dog',  'cat',  'monkey',  'giraffe'])
  86.         'giraffe'
  87.         >>> extra_elem([1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6])
  88.         6
  89.         """
  90.         "*** YOUR CODE HERE ***"
  91.         return list(set(b) - set(a))[0]

  92. def find_duplicates(lst):
  93.         """Returns True if lst has any duplicates and False if it does not.

  94.         >>> find_duplicates([1, 2, 3, 4, 5])
  95.         False
  96.         >>> find_duplicates([1, 2, 3, 4, 2])
  97.         True
  98.         """
  99.         "*** YOUR CODE HERE ***"
  100.         return len(set(lst)) != len(lst)
复制代码
回复

使用道具 举报

🔗
pirateshadow 2016-4-25 15:08:01 | 只看该作者
全局:
本帖最后由 pirateshadow 于 2016-4-25 15:09 编辑

原来是61A,发错地儿了。。

Screen Shot 2016-04-25 at 3.04.01 PM.png (154.7 KB, 下载次数: 3)

Screen Shot 2016-04-25 at 3.04.01 PM.png
回复

使用道具 举报

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

本版积分规则

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