一亩三分地论坛

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

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

[CS61A] Lab 04 List

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-5-20 11:23:53 | 显示全部楼层 |阅读模式

[其他]CS61A #1 - 4@UCBerkely

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

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

x
Lab 04 对list的学习,详细内容请看链接 http://gaotx.com/cs61a/lab/lab04/
我的解答 http://gaotx.com/blogs/2015/05/14/cs61a-lab04/

评分

1

查看全部评分

karte_polo 发表于 2015-5-25 18:23:45 | 显示全部楼层
# Q4
def get_seven_a(x):
    """
    >>> x = [1, 3, [5, 7], 9]
    >>> get_seven_a(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[2][1]

def get_seven_b(x):
    """
    >>> x = [[7]]
    >>> get_seven_b(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[0][0]

def get_seven_c(x):
    """
    >>> x = [1, [2, [3, [4, [5, [6, [7]]]]]]]
    >>> get_seven_c(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[1][1][1][1][1][1][0]

# Q5
def reverse_recursive(lst):
    """Returns the reverse of the given list.

    >>> reverse_recursive([1, 2, 3, 4])
    [4, 3, 2, 1]
    """
    "*** YOUR CODE HERE ***"
    if not lst:
        return []
    return reverse_recursive(lst[1:])+[lst[0]]

# Q6
def merge(lst1, lst2):
    """Merges two sorted lists recursively.

    >>> merge([1, 3, 5], [2, 4, 6])
    [1, 2, 3, 4, 5, 6]
    >>> merge([], [2, 4, 6])
    [2, 4, 6]
    >>> merge([1, 2, 3], [])
    [1, 2, 3]
    >>> merge([5, 7], [2, 4, 6])
    [2, 4, 5, 6, 7]
    """
    "*** YOUR CODE HERE ***"
    if not lst1:
        return lst2
    if not lst2:
        return lst1
    if lst1[0]<lst2[0]:
        return [lst1[0]]+merge(lst1[1:],lst2)
    else:
        return [lst2[0]]+merge(lst1,lst2[1:])

# Q8
from math import sqrt

def is_square(n):
    return float(sqrt(n)) == int(sqrt(n))

def squares(seq):
    """Returns a new list containing elements of the original list that are
    perfect squares.

    >>> seq = [49, 8, 2, 1, 102]
    >>> squares(seq)
    [49, 1]
    >>> seq = [500, 30]
    >>> squares(seq)
    []
    """
    "*** YOUR CODE HERE ***"
    return [n for n in seq if is_square(n)]

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-28 08:08:16 | 显示全部楼层
关于list的这个lab不算太难,不过感觉recursive和iterative理解不透彻,每次都得去复习一下它俩的区别
# Q4
def get_seven_a(x):
    """
    >>> x = [1, 3, [5, 7], 9]
    >>> get_seven_a(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[2][1]

def get_seven_b(x):
    """
    >>> x = [[7]]
    >>> get_seven_b(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[0][0]

def get_seven_c(x):
    """
    >>> x = [1, [2, [3, [4, [5, [6, [7]]]]]]]
    >>> get_seven_c(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[1][1][1][1][1][1][0]

# Q5
def reverse_recursive(lst):
    """Returns the reverse of the given list.

    >>> reverse_recursive([1, 2, 3, 4])
    [4, 3, 2, 1]
    """
    "*** YOUR CODE HERE ***"
    if lst == []:
        return []
    return reverse_recursive(lst[1:]) + [lst[0]]

# Q6
def merge(lst1, lst2):
    """Merges two sorted lists recursively.

    >>> merge([1, 3, 5], [2, 4, 6])
    [1, 2, 3, 4, 5, 6]
    >>> merge([], [2, 4, 6])
    [2, 4, 6]
    >>> merge([1, 2, 3], [])
    [1, 2, 3]
    >>> merge([5, 7], [2, 4, 6])
    [2, 4, 5, 6, 7]
    """
    "*** YOUR CODE HERE ***"
    if lst1 == []:
        return lst2
    elif lst2 == []:
        return lst1
    else:
        if lst1[0] < lst2[0]:
            return [lst1[0]] + merge(lst1[1:], lst2)
        else:
            return [lst2[0]] + merge(lst1, lst2[1:])


# Q8
from math import sqrt

def is_square(n):
    return float(sqrt(n)) == int(sqrt(n))

def squares(seq):
    """Returns a new list containing elements of the original list that are
    perfect squares.

    >>> seq = [49, 8, 2, 1, 102]
    >>> squares(seq)
    [49, 1]
    >>> seq = [500, 30]
    >>> squares(seq)
    []
    """
    "*** YOUR CODE HERE ***"
    return [i for i in seq if is_square(i)]

from lab04 import *

# Q9
def reverse_iter(lst):
    """Returns the reverse of the given list.

    >>> reverse_iter([1, 2, 3, 4])
    [4, 3, 2, 1]
    """
    "*** YOUR CODE HERE ***"
    length = len(lst)
    i = length - 1
    new_lst = []
    while i >= 0:
        new_lst += [lst[i]]
        i -= 1
    return new_lst

# Q10
def mergesort(seq):
    """Mergesort algorithm.

    >>> mergesort([4, 2, 5, 2, 1])
    [1, 2, 2, 4, 5]
    >>> mergesort([])     # sorting an empty list
    []
    >>> mergesort([1])   # sorting a one-element list
    [1]
    """
    "*** YOUR CODE HERE ***"
    if len(seq) == 0 or len(seq) == 1:
        return seq
    else:
        mid = len(seq) // 2
        return merge(mergesort(seq[:mid]), mergesort(seq[mid:]))

   

# Q11
def coords(fn, seq, lower, upper):
    """
    >>> seq = [-4, -2, 0, 1, 3]
    >>> fn = lambda x: x**2
    >>> coords(fn, seq, 1, 9)
    [[-2, 4], [1, 1], [3, 9]]
    """
    "*** YOUR CODE HERE ***"
    return [[ele, fn(ele)] for ele in seq if fn(ele) in range(lower, upper+1)]

# Q12
def deck(suits, numbers):
    """Creates a deck of cards (a list of 2-element lists) with the given
    suits and numbers. Each element in the returned list should be of the form
    [suit, number].

    >>> deck(['S', 'C'], [1, 2, 3])
    [['S', 1], ['S', 2], ['S', 3], ['C', 1], ['C', 2], ['C', 3]]
    >>> deck(['S', 'C'], [3, 2, 1])
    [['S', 3], ['S', 2], ['S', 1], ['C', 3], ['C', 2], ['C', 1]]
    >>> deck([], [3, 2, 1])
    []
    >>> deck(['S', 'C'], [])
    []
    """
    "*** YOUR CODE HERE ***"
    return [[suit, number] for suit in suits for number in numbers]

# Q13
def add_matrices(x, y):
    """
    >>> matrix1 = [[1, 3],
    ...            [2, 0]]
    >>> matrix2 = [[-3, 0],
    ...            [1, 2]]
    >>> add_matrices(matrix1, matrix2)
    [[-2, 3], [3, 2]]
    """
    "*** YOUR CODE HERE ***"
    return [[x[i][j] + y[i][j] for j in range(len(x[i]))] for i in range(len(x))]

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-23 11:43:04 | 显示全部楼层
关于list的lab,挺简单的,没碰到啥障碍。就是看了Q5的答案之后发现自己智商欠费。代码如下:
# Q4
def get_seven_a(x):
    """
    >>> x = [1, 3, [5, 7], 9]
    >>> get_seven_a(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return x[1][0]

def get_seven_b(x):
    """
    >>> x = [[7]]
    >>> get_seven_b(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return [0][0]

def get_seven_c(x):
    """
    >>> x = [1, [2, [3, [4, [5, [6, [7]]]]]]]
    >>> get_seven_c(x)
    7
    """
    "*** YOUR CODE HERE ***"
    return [1][1][1][1][1][1][0]

# Q5
def reverse_recursive(lst):
    """Returns the reverse of the given list.

    >>> reverse_recursive([1, 2, 3, 4])
    [4, 3, 2, 1]
    """
    "*** YOUR CODE HERE ***"
    if len(lst) == 1:
        return [lst[0]]
    else:
        l = len(lst)
        return [lst[l-1]] + reverse_recursive(lst[:l-1])
        
# Q6
def merge(lst1, lst2):
    """Merges two sorted lists recursively.

    >>> merge([1, 3, 5], [2, 4, 6])
    [1, 2, 3, 4, 5, 6]
    >>> merge([], [2, 4, 6])
    [2, 4, 6]
    >>> merge([1, 2, 3], [])
    [1, 2, 3]
    >>> merge([5, 7], [2, 4, 6])
    [2, 4, 5, 6, 7]
    """
    "*** YOUR CODE HERE ***"
    lst = []
    for item in lst1:
        lst = lst + [item]
    for item in lst2:
        lst = lst + [item]
    lst.sort()
    print lst
   
# Q8
from math import sqrt

def is_square(n):
    return float(sqrt(n)) == int(sqrt(n))

def squares(seq):
    """Returns a new list containing elements of the original list that are
    perfect squares.

    >>> seq = [49, 8, 2, 1, 102]
    >>> squares(seq)
    [49, 1]
    >>> seq = [500, 30]
    >>> squares(seq)
    []
    """
    "*** YOUR CODE HERE ***"
    return [item for item in seq if is_square(item)]
回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-1-28 13:46:22 | 显示全部楼层
这次lab感觉难度不高阿。。 lab04.png

然后是代码:
  1. # Q4
  2. def get_seven_a(x):
  3.     """
  4.     >>> x = [1, 3, [5, 7], 9]
  5.     >>> get_seven_a(x)
  6.     7
  7.     """
  8.     "*** YOUR CODE HERE ***"
  9.     return x[2][1]


  10. def get_seven_b(x):
  11.     """
  12.     >>> x = [[7]]
  13.     >>> get_seven_b(x)
  14.     7
  15.     """
  16.     "*** YOUR CODE HERE ***"
  17.     return x[0][0]


  18. def get_seven_c(x):
  19.     """
  20.     >>> x = [1, [2, [3, [4, [5, [6, [7]]]]]]]
  21.     >>> get_seven_c(x)
  22.     7
  23.     """
  24.     "*** YOUR CODE HERE ***"
  25.     return x[1][1][1][1][1][1][0]


  26. # Q5
  27. def reverse_recursive(lst):
  28.     """Returns the reverse of the given list.

  29.     >>> reverse_recursive([1, 2, 3, 4])
  30.     [4, 3, 2, 1]
  31.     """
  32.     "*** YOUR CODE HERE ***"
  33.     if len(lst) < 1:
  34.         return []
  35.     return lst[-1:] + reverse_recursive(lst[:-1])


  36. # Q6
  37. def merge(lst1, lst2):
  38.     """Merges two sorted lists recursively.

  39.     >>> merge([1, 3, 5], [2, 4, 6])
  40.     [1, 2, 3, 4, 5, 6]
  41.     >>> merge([], [2, 4, 6])
  42.     [2, 4, 6]
  43.     >>> merge([1, 2, 3], [])
  44.     [1, 2, 3]
  45.     >>> merge([5, 7], [2, 4, 6])
  46.     [2, 4, 5, 6, 7]
  47.     """
  48.     "*** YOUR CODE HERE ***"
  49.     ret_list = []
  50.     i = 0
  51.     j = 0
  52.     while i < len(lst1) and j < len(lst2):
  53.         if lst1[i] < lst2[j]:
  54.             ret_list.append(lst1[i])
  55.             i += 1
  56.         else:
  57.             ret_list.append(lst2[j])
  58.             j += 1
  59.     while i < len(lst1):
  60.         ret_list.append(lst1[i])
  61.         i += 1
  62.     while j < len(lst2):
  63.         ret_list.append(lst2[j])
  64.         j += 1
  65.     return ret_list


  66. # Q8
  67. from math import sqrt

  68. def is_square(n):
  69.     """
  70.     >>> is_square(49)
  71.     True
  72.     """
  73.     return float(sqrt(n)) == int(sqrt(n))

  74. def squares(seq):
  75.     """Returns a new list containing elements of the original list that are
  76.     perfect squares.

  77.     >>> seq = [49, 8, 2, 1, 102]
  78.     >>> squares(seq)
  79.     [49, 1]
  80.     >>> seq = [500, 30]
  81.     >>> squares(seq)
  82.     []
  83.     """
  84.     "*** YOUR CODE HERE ***"
  85.     return [n for n in seq if is_square(n)]
复制代码
  1. from lab04 import *

  2. # Q9
  3. def reverse_iter(lst):
  4.     """Returns the reverse of the given list.

  5.     >>> reverse_iter([1, 2, 3, 4])
  6.     [4, 3, 2, 1]
  7.     """
  8.     "*** YOUR CODE HERE ***"
  9.     ret_lst = []
  10.     for i in range(len(lst)):
  11.         ret_lst.append(lst[-1])
  12.         lst = lst[:-1]
  13.     return ret_lst


  14. # Q10
  15. def mergesort(seq):
  16.     """Mergesort algorithm.

  17.     >>> mergesort([4, 2, 5, 2, 1])
  18.     [1, 2, 2, 4, 5]
  19.     >>> mergesort([])     # sorting an empty list
  20.     []
  21.     >>> mergesort([1])   # sorting a one-element list
  22.     [1]
  23.     """
  24.     "*** YOUR CODE HERE ***"
  25.     if len(seq) == 0 or len(seq) == 1:
  26.         return seq
  27.     else:
  28.         mid = len(seq) // 2
  29.         return merge(mergesort(seq[:mid]), mergesort(seq[mid:]))


  30. # Q11
  31. def coords(fn, seq, lower, upper):
  32.     """
  33.     >>> seq = [-4, -2, 0, 1, 3]
  34.     >>> fn = lambda x: x**2
  35.     >>> coords(fn, seq, 1, 9)
  36.     [[-2, 4], [1, 1], [3, 9]]
  37.     """
  38.     "*** YOUR CODE HERE ***"
  39.     return [[x, fn(x)] for x in seq if fn(x) >= lower and fn(x) <= upper]


  40. # Q12
  41. def deck(suits, numbers):
  42.     """Creates a deck of cards (a list of 2-element lists) with the given
  43.     suits and numbers. Each element in the returned list should be of the form
  44.     [suit, number].

  45.     >>> deck(['S', 'C'], [1, 2, 3])
  46.     [['S', 1], ['S', 2], ['S', 3], ['C', 1], ['C', 2], ['C', 3]]
  47.     >>> deck(['S', 'C'], [3, 2, 1])
  48.     [['S', 3], ['S', 2], ['S', 1], ['C', 3], ['C', 2], ['C', 1]]
  49.     >>> deck([], [3, 2, 1])
  50.     []
  51.     >>> deck(['S', 'C'], [])
  52.     []
  53.     """
  54.     "*** YOUR CODE HERE ***"
  55.     return [[x, y] for x in suits for y in numbers]


  56. # Q13
  57. def add_matrices(x, y):
  58.     """
  59.     >>> matrix1 = [[1, 3],
  60.     ...            [2, 0]]
  61.     >>> matrix2 = [[-3, 0],
  62.     ...            [1, 2]]
  63.     >>> add_matrices(matrix1, matrix2)
  64.     [[-2, 3], [3, 2]]
  65.     """
  66.     "*** YOUR CODE HERE ***"
  67.     return [[x[i][j] + y[i][j] for j in range(len(x[0]))] for i in range(len(x))]
复制代码
回复 支持 反对

使用道具 举报

silence_shadow 发表于 2016-2-13 19:38:28 | 显示全部楼层
没明白为啥这次作业的变量是protected的 求解求解 非常感谢
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-2-19 16:18:44 | 显示全部楼层
# Q4
def get_seven_a(x):
    """
    >>> x = [1, 3, [5, 7], 9]
    >>> get_seven_a(x)
    7
    """
    "*** YOUR CODE HERE ***"
    for i in x:
        if type(i) == list:
            return get_seven_a(i)
        elif i == 7:
            return i


def get_seven_b(x):
    """
    >>> x = [[7]]
    >>> get_seven_b(x)
    7
    """
    "*** YOUR CODE HERE ***"
    for i in x:
        if type(i) == list:
            return get_seven_a(i)
        elif i == 7:
            return i

def get_seven_c(x):
    """
    >>> x = [1, [2, [3, [4, [5, [6, [7]]]]]]]
    >>> get_seven_c(x)
    7
    """
    for i in x:
        if type(i) == list:
            return get_seven_a(i)
        elif i == 7:
            return i

# Q5
def reverse_recursive(lst):
    """Returns the reverse of the given list.

    >>> reverse_recursive([1, 2, 3, 4])
    [4, 3, 2, 1]
    """
    "*** YOUR CODE HERE ***"
    if not lst:
        return []
    return [lst[-1]] + reverse_recursive(lst[0:-1])

# Q6
def merge(lst1, lst2):
    """Merges two sorted lists recursively.

    >>> merge([1, 3, 5], [2, 4, 6])
    [1, 2, 3, 4, 5, 6]
    >>> merge([], [2, 4, 6])
    [2, 4, 6]
    >>> merge([1, 2, 3], [])
    [1, 2, 3]
    >>> merge([5, 7], [2, 4, 6])
    [2, 4, 5, 6, 7]
    """
    "*** YOUR CODE HERE ***"
    if not lst1 or not lst2:
        return lst1 + lst2
    elif lst1[0] < lst2[0]:
        return [lst1[0]] + merge(lst1[1:], lst2)
    else:
        return [lst2[0]] + merge(lst1, lst2[1:])

# Q8
from math import sqrt

def is_square(n):
    return float(sqrt(n)) == int(sqrt(n))

def squares(seq):
    """Returns a new list containing elements of the original list that are
    perfect squares.

    >>> seq = [49, 8, 2, 1, 102]
    >>> squares(seq)
    [49, 1]
    >>> seq = [500, 30]
    >>> squares(seq)
    []
    """
    "*** YOUR CODE HERE ***"
    perfect_squares = [x for x in seq if is_square(x)]
    return perfect_squares

from lab04 import *

# Q9
def reverse_iter(lst):
    """Returns the reverse of the given list.

    >>> reverse_iter([1, 2, 3, 4])
    [4, 3, 2, 1]
    """
    "*** YOUR CODE HERE ***"
    reverse = []
    i = 0
    while i < len(lst):
        reverse = [lst[i]] + reverse
        i = i + 1
    return reverse

# Q10
def mergesort(seq):
    """Mergesort algorithm.

    >>> mergesort([4, 2, 5, 2, 1])
    [1, 2, 2, 4, 5]
    >>> mergesort([])     # sorting an empty list
    []
    >>> mergesort([1])   # sorting a one-element list
    [1]
    """
    "*** YOUR CODE HERE ***"
    if len(seq) < 2:
        return seq
    mid = len(seq) // 2
    return merge(mergesort(seq[:mid]), mergesort(seq[mid:]))
def merge(lst1, lst2):
    if not lst1 or not lst2:
        return lst1 + lst2
    elif lst1[0] < lst2[0]:
        return [lst1[0]] + merge(lst1[1:], lst2)
    else:
        return [lst2[0]] + merge(lst1, lst2[1:])

# Q11
def coords(fn, seq, lower, upper):
    """
    >>> seq = [-4, -2, 0, 1, 3]
    >>> fn = lambda x: x**2
    >>> coords(fn, seq, 1, 9)
    [[-2, 4], [1, 1], [3, 9]]
    """
    "*** YOUR CODE HERE ***"
    return [[x, fn(x)] for x in seq if lower <= fn(x) <= upper]

# Q12
def deck(suits, numbers):
    """Creates a deck of cards (a list of 2-element lists) with the given
    suits and numbers. Each element in the returned list should be of the form
    [suit, number].

    >>> deck(['S', 'C'], [1, 2, 3])
    [['S', 1], ['S', 2], ['S', 3], ['C', 1], ['C', 2], ['C', 3]]
    >>> deck(['S', 'C'], [3, 2, 1])
    [['S', 3], ['S', 2], ['S', 1], ['C', 3], ['C', 2], ['C', 1]]
    >>> deck([], [3, 2, 1])
    []
    >>> deck(['S', 'C'], [])
    []
    """
    "*** YOUR CODE HERE ***"
    return [[x, y] for x in suits
                   for y in numbers]

# Q13
def add_matrices(x, y):
    """
    >>> matrix1 = [[1, 3],
    ...            [2, 0]]
    >>> matrix2 = [[-3, 0],
    ...            [1, 2]]
    >>> add_matrices(matrix1, matrix2)
    [[-2, 3], [3, 2]]
    """
    "*** YOUR CODE HERE ***"
    return [[x[i][j] + y[i][j] for j in range(len(x[0]))] for i in range(len(x))]

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2016-12-7 03:48

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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