码迷,mamicode.com
首页 > 其他好文 > 详细

Lab 7

时间:2015-07-12 21:30:29      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:

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

Lab 7

标签:

原文地址:http://www.cnblogs.com/whuyt/p/4641461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!