一亩三分地论坛

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

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

[CS61A]Lab08

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-6-9 12:36:42 | 显示全部楼层 |阅读模式

[其他]CS61A #05 - 04@UCBerkely

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

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

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

评分

1

查看全部评分

西风吹草 发表于 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

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

liyimeng 发表于 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
回复 支持 反对

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2016-12-11 05:23

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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