Lab 7

Link 数据类型如下:

class Link:
    """A linked list.

    >>> s = Link(1, Link(2, Link(3, Link(4))))
    >>> len(s)
    4
    >>> s[2]
    3
    >>> s
    Link(1, Link(2, Link(3, Link(4))))
    """
    empty = ()

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

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

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

    def __repr__(self):
        if self.rest is not Link.empty:
            rest_str = ‘, ‘ + repr(self.rest)
        else:
            rest_str = ‘‘
        return ‘Link({0}{1})‘.format(repr(self.first), rest_str)

Question 2

Implement a function insert that takes a Link, a value, and an index, and inserts the value into the Link at the given index. You can assume the linked list already has at least one element. Do not return anything — insert should mutate the linked list.

def insert(link, value, index):
    """Insert a value into a Link at the given index.

    >>> link = Link(1, Link(2, Link(3)))
    >>> insert(link, 9001, 0)
    >>> link
    Link(9001, Link(1, Link(2, Link(3))))
    >>> insert(link, 100, 2)
    >>> link
    Link(9001, Link(1, Link(100, Link(2, Link(3)))))
    >>> insert(link, 4, 5)
    Index out of bounds!
    """
    "*** YOUR CODE HERE ***"

参考答案

# recursion
    if index == 0:
        link.rest = Link(link.first, link.rest)
        link.first = value
    elif link.rest is Link.empty:
        print ‘Index out of bounds!‘
    else:
        insert(link.rest, value, index-1)  

# iterative solution
    while index > 0 and link.rest is not Link.empty:
        link = link.rest
        index -= 1
    if index == 0:
        link.rest = Link(link.first, link.rest)
        link.first = value
    else:
        print(‘Index out of bounds!‘)

Question 7

1. Implement a function reverse that reverses a given Linkreverse should return a new Link that is the reverse of the original, without modifying the original.

def reverse(link):
    """Returns a Link that is the reverse of the original.

    >>> reverse(Link(1))
    Link(1)
    >>> link = Link(1, Link(2, Link(3)))
    >>> reverse(link)
    Link(3, Link(2, Link(1)))
    >>> link
    Link(1, Link(2, Link(3)))
    """
    "*** YOUR CODE HERE ***"

自己的答案

leng = len(link)
    link2 = Link.empty
    for i in range(leng):
        link2 = Link(link[i], link2)
    return link2

参考答案

new = Link(link.first)
    while link.rest is not Link.empty:
        link = link.rest
        new = Link(link.first, new)
    return new

2. Implement mutate_reverse, a recursive mutating version of reverse. Have it mutate the original Linkso that its elements are reversed.

def mutate_reverse(link):
    """Mutates the Link so that its elements are reversed.

    >>> link = Link(1)
    >>> mutate_reverse(link)
    >>> link
    Link(1)

    >>> link = Link(1, Link(2, Link(3)))
    >>> mutate_reverse(link)
    >>> link
    Link(3, Link(2, Link(1)))
    """
    "*** YOUR CODE HERE ***"

自己的答案

采用的是每个link.first赋相应的值

temp = link.first
    leng = len(link)
    while link.rest is not link.empty:
        link.first = link[leng-1]
        link = link.rest
        leng -= 2
    link.first = temp

参考答案

if link and link.rest:
        mutate_reverse(link.rest)
        while link.rest:
            link.first, link.rest.first = link.rest.first, link.first
            link = link.rest

2015-07-12

时间: 2024-08-08 03:13:37

Lab 7的相关文章

Simulation.Lab.Software.SimLab.Composer.2015.v6.1.MACOSX 1CD

CA Spectrum Linux VM 10.01.00.00.103 Linux 2DVD  Tahoe Design HYDROFLO v3.0.0.4 1CD  CA Spectrum Windows VM 10.01.00.00.103 Win64 2DVD  Delcam Exchange 2016 R2 CR 8.3.1005 Win64 1CD  Delcam PowerSHAPE 2016 SP2 Update only 1CD  ESI Group VA One 2015.0

x01.Lab.StoreApp: XP 停服,微软变脸

变脸,川剧的一种表演形式,除了哄哄小孩,似乎别无用处.而川剧变脸从业者何其多也,存在时间何其长也.以如此多的从业者,如此长的时间,来进行科研,其成果一定是斐然吧.推而广之,试问天下谁能敌! 微软变脸,足以改变世界.这次变脸,不仅是形式上的,而且是骨子里的.为适应手机.平板的性能苛求,其应用商店程序是建立在 WinRT 的基础上,而 WinRT 是建立在 COM 基础上.即面向对象,又拥抱底层,可谓鱼与熊掌兼得.不仅如此,其开发者许可,应用商店成立,无一不在显示微软的掌控能力."天下英雄,尽入毂中

Citrix Provisioning Services LAB時出現Unable to contact th database Server

今天在執行PVS LAB時出現下圖 後來發現為SQL連入的要調整如下圖1.Machine Account2.對應DB3.DB權限 如果是用SA帳戶作為本機的SQL,就是調這組帳戶 調完後對應就可以使用PVS 感謝

Lab颜色空间进行颜色提取 及其实现

这段时间在做车灯检测,晚上有些尾灯偏黄色亮度偏弱,仅用灰度度是不够的,经比较了在RGB.HSV.Lab颜色空间下进行颜色提取,发现Lab颜色模型的效果是最好的.下面介绍Lab的原理及其代码实现. Lab颜色模型由三个要素组成,一个要素是亮度(L),a 和b是两个颜色通道.a包括的颜色是从深绿色(低亮度值)到灰色(中亮度值)再到亮粉红色(高亮度值):b是从亮蓝色(低亮度值)到灰色(中亮度值)再到黄色(高亮度值).因此,这种颜色混合后将产生具有明亮效果的色彩.(这段百度的,哈哈 ) RGB转换到La

Lab - Hot Standby Router Protocol

Topology Objective Configure inter-VLAN routing with HSRP to provide redundant, fault-tolerant routing to the internal network. Background Hot Standby Router Protocol (HSRP) is a Cisco-proprietary redundancy protocol for establishing a fault-tolerant

MIT OS lab Makefile 分析

lab1 中的Makefile主要是根目录下的GNUMakefile, kern/Makefrag, boot/Makefrag, 后两者通过include直接包含到GNUMakefile中. 1 # 2 # This makefile system follows the structuring conventions 3 # recommended by Peter Miller in his excellent paper: 4 # 5 # Recursive Make Considere

CSAPP 3e : Data lab

/* * CS:APP Data Lab * * <Please put your name and userid here> * * bits.c - Source file with your solutions to the Lab. * This is the file you will hand in to your instructor. * * WARNING: Do not include the <stdio.h> header; it confuses the

CSAPP 3e: Bomb lab (phase_1)

这一个lab拖了好久才开始慢慢完成,花的时间比较多,我也是没想到会需要这么多时间来折腾.考虑到如果把所有关卡的内容都一次发出来,太长了.所以分开操作吧. 然后,有一点是,在开始解题前的确该好好认识一下GDB,因为要使用这个工具.虽然我也感觉有些东西是可以等需要的时候再查的,但是后来找到了一篇介绍gdb命令的,写的比较详细,就下载了打印出来,占用了几节课的时间好好看了一下,就感觉特别有用,比之前更加熟悉了GDB.大概是在网页上看的时候比较急躁,所以吸收不好吧23333.还有,在解题过程中参考了不少

CSAPP 3e: Bomb lab (phase_2) + lab中的GDB 使用

这里是第二关,在这里分享一些在解题过程中用到的GDB功能. 首先,要进行断点,比如,在第二关中,断点操作有: 81: input = read_line(); 82: phase_2(input); 83: phase_defused(); break 81; break 82; break 83; 分别在read_line,phase_2,phase_defused,三个函数入口进行了断点. 另外,还有一个地方需要断点,那就是explode_bomb:操作:break explode_bomb

x01.Lab.StreetApp: MVVM

Store App 特别适用于 MVVM 模式.由于要用到 SQLite,Bing Maps,所以第一步从网上搜索并安装这两个扩展.很难想象在智能手机上运行 SQL Server 这种巨无霸型数据库,SQLite 便成为首选.运行 SQLite,需要 C++ 扩展,而解决方案属性 => 配置管理中,CPU 也不能是 Any,应改为 x86.安装后,其添加引用窗口的 扩展 项如下: 第二步,当然是下载源代码了.可到我的置顶随笔 x01.Lab.Download 中获取.解压后,双击 Lab.sln