Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3 Implement clip_list(s, index), where s is an instance of the Link class and index is a non-negative integer. The clip_list function mutates s

Python 3 Implement clip_list(s, index), where s is an instance of the Link class and index is a non-negative integer. The clip_list function mutates s so that all elements that occur after the index are removed. The clip_list function should also return a linked list of all of the removed elements. If the index exceeds the linked list, raise an IndexError. Your solution must use recursion; you may not use iteration. You may NOT assume that __getitem__ and __len__ have been implemented. See the doctest for expected behavior. Your solution should not require more than 8 lines, and you do not need to use all 8 lines.

def clip_list (s , index ): """ >>> s1 = Link (1 , Link (2 , Link (3))) >>> clip_list ( s1 , 0) Link(2, Link(3)) >>> s1 Link(1) >>> s2 = Link (1 , Link (2 , Link (3))) >>> clip_list ( s2 , 2) == Link . empty # No elements after index 2 True >>> s2 Link(1, Link(2, Link(3))) >>> s3 = Link (1 , Link (2 , Link (3))) >>> try:clip_list ( s3 , 3) #no index 3 available ... except(IndexError): ... print("good job") ... good job """ #Your Code Here# image text in transcribed

--------------------------------------------------------------------------------------------

class Link:

"""A linked list.

>>> s = Link(1, Link(2, Link(3))) >>> s.first 1 >>> s.rest Link(2, Link(3)) """ empty = ()

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

def __repr__(self): if self.rest is Link.empty: return 'Link({})'.format(self.first) else: return 'Link({}, {})'.format(self.first, repr(self.rest))

def __str__(self): """Returns a human-readable string representation of the Link

>>> s = Link(1, Link(2, Link(3, Link(4)))) >>> str(s) '' >>> str(Link(1)) '' >>> str(Link.empty) # empty tuple '()' """ string = '' image text in transcribed

def clip_list (s, index): >>> s1 = Link (1 , Link (2 , Link (3))) >>>clip_list ( s1, 0 Link (2 Link (3)) S1 Link (1) >>> s2 = Link (1 , Link (2 , Link (3))) >>> clip-list ( s2 , 2) Link . empty # No elementsafterindex 2 True >>s2 Link (1 Link (2Link(3))) >3Link (1 Link (2Link (3))) >>> clip-list ( s3 , 3) #s3 has no index 3 Traceback (most recent call last): IndexError kokok YOUR CODE HERE ok

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions