查看: 2015| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Disc05

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

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

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

x
Disc05 link: http://gaotx.com/cs61a/disc/disc05.pdf
My solution: http://gaotx.com/blogs/2015/05/21/cs61a-disc05/

上一篇:Introduction to Big Data with Apache Spark & Scalable Mac...
下一篇:[CS61A]Ants
🔗
wuxiaomin98 2015-6-4 04:29:28 | 只看该作者
全局:
# This is for cs61a discussion 5

def square_elements(lst):
        """
        2.1.1
        Squares every element in lst.
        >>> lst = [1, 2, 3]
        >>> square_elements(lst)
        >>> lst
        [1, 4, 9]
        """
        for i in range(len(lst)):
                lst[i] = lst[i] ** 2

def reverse_list(lst):
        """
        2.1.2
        Reverses lst in-place (mutating the original list).
        >>> lst = [1, 2, 3, 4]
        >>> reverse_list(lst)
        >>> lst
        [4, 3, 2, 1]
        >>> pi = [3, 1, 4, 1, 5]
        >>> reverse_list(pi)
        >>> pi
        [5, 1, 4, 1, 3]
        """
        for i in range(len(lst) // 2):
                lst[i], lst[len(lst)-i-1] = lst[len(lst)-i-1], lst[i]

def add_this_many(x, y, lst):
        """
        2.2.1
        Adds y to the end of lst the number of times x occurs.
        >>> lst = [1, 2, 4, 2, 1]
        >>> add_this_many(1, 5, lst)
        >>> lst
        [1, 2, 4, 2, 1, 5, 5]
        """
        count = 0
        for ele in lst:
                if ele == x:
                        count += 1
        while count > 0:
                lst.append(y)
                count -= 1

def remove_all(el, lst):
        """
        2.2.2
        Removes all instances of el from lst.
        >>> x = [3, 1, 2, 1, 5, 1, 1, 7]
        >>> remove_all(1, x)
        >>> x
        [3, 2, 5, 7]
        """
        while el in lst:
                lst.remove(el)

def replace_all(d, x, y):
        """
        3.3.1
        >>> d = {’foo’: 2, ’bar’: 3, ’garply’: 3, ’xyzzy’: 99}
        >>> replace_all(d, 3, ’poof’)
        >>> d
        {’foo’: 2, ’bar’: ’poof’, ’garply’: ’poof’, ’xyzzy’: 99}
    """
        for key in d:
                if d[key] == x:
                        d[key] = y

def replace_all_deep(d, x, y):
    """
    3.4.1
    >>> d = {1: {2: 3, 3: 4}, 2: {4: 4, 5: 3}}
    >>> replace_all_deep(d, 3, 1)
    >>> d
    {1: {2: 1, 3: 4}, 2: {4: 4, 5: 1}}
    """
    for key in d:
            if d[key] == x:
                    d[key] = y
            elif type(d[key]) == dict:
                    replace_all_deep(d[key], x, y)

def remove_all(d, x):
    """
    3.4.2
    >>> d = {1:2, 2:3, 3:2, 4:3}
    >>> remove_all(d, 2)
    >>> d
    {2: 3, 4: 3}
    """
    for key in d:
            if d[key] == x:
                    del d[key]
回复

使用道具 举报

🔗
reasonapp 2015-6-23 01:09:15 | 只看该作者
全局:
并没有做extra,一天做一个lab+hw+disc快要做瘫了。。。代码如下。。
#disc05
#Q2.1_1
def square_elements(lst):
    """Squares every element in lst.
    >>> lst = [1, 2, 3]
    >>> square_elements(lst)
    >>> lst
    [1, 4, 9]
    """
    for i in range(len(lst)):
        lst[i] = lst[i] ** 2
        
#Q2.1_2
def reverse_list(lst):
    """Reverses lst in-place (mutating the original list).
    >>> lst = [1, 2, 3, 4]
    >>> reverse_list(lst)
    >>> lst
    [4, 3, 2, 1]
    >>> pi = [3, 1, 4, 1, 5]
    >>> reverse_list(pi)
    >>> pi
    [5, 1, 4, 1, 3]
    """
    for i in range(len(lst) // 2):
                lst[i], lst[len(lst)-i-1] = lst[len(lst)-i-1], lst[i]
               
#Q3.3_1
def replace_all(d, x, y):
        """
        3.3.1
        >>> d = {’foo’: 2, ’bar’: 3, ’garply’: 3, ’xyzzy’: 99}
        >>> replace_all(d, 3, ’poof’)
        >>> d
        {’foo’: 2, ’bar’: ’poof’, ’garply’: ’poof’, ’xyzzy’: 99}
    """
        for key in d:
                if d[key] == x:
                        d[key] = y
回复

使用道具 举报

🔗
Liaeve 2016-4-26 19:58:02 | 只看该作者
全局:
#Q2.1.1
def square_elements(lst):
    for i in range(len(lst)):
        lst[i] = lst[i] ** 2
    return lst

#Q2.1.2
def reverse_list(lst):
    for i in range(len(lst)//2):
        lst[i], lst[len(lst)-i-1] = lst[len(lst)-i-1], lst[i]
    return lst

#Extra
#Q2.2.1
def add_this_many(x, y, lst):
        for el in lst:
                if el == x:
                        lst.append(y)
        return lst
l=[1,2,3,1,4,3,6,3]
print(add_this_many(3, 5, l))

#Q2.2.2
def remove_all(el, lst):
    while el in lst:
        lst.remove(el)
    return lst

#Q3.3.1
def replace_all(d, x, y):
    for key in d:
        if d[key] == x:
            d[key] = y
    return lst

#Q3.4.1
def replace_all_deep(d, x, y):
    for key in d:
        if type(d[key]) == dict:
            replace_all_deep(d[key], x, y)
        elif d[key] == x:
            d[key] = y
        return d

#Q3.4.2
def remove_all(d, x):
    keys = []
    for key in d:
        if d[key] == x:
            keys += [key]
    for k in keys:
        del d[k]
    return d
d = {1:2, 2:3, 3:2, 4:3}
remove_all(d, 2)
print(d)
回复

使用道具 举报

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

本版积分规则

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