Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 image text in transcribed

def remove_last_occurrence(lst, elem):

"""

>>> l2 = Link('a', Link('b', Link('c', Link('d'))))

>>> remove_last_occurrence(l2, 'b')

>>> l2

Link(a, Link(c, Link(d)))

>>> remove_last_occurrence(l2, 'd')

>>> l2

Link(a, Link(c))

>>> l3 = Link.empty

>>> remove_last_occurrence(l3, 'delete_me')

>>> l3 == Link.empty

True

>>> l4 = Link('b', Link('a', Link('b', Link('a', Link('b')))))

>>> remove_last_occurrence(l4, 'a')

>>> l4

Link(b, Link(a, Link(b, Link(b))))

"""

"""YOUR CODE GOES HERE"""

PYTHON 3

class Link: A linked list Link(1, Link(2, Link(3) s.first S.rest Link(2, ink(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 Link(1, Link(2, Link(3, Link(4) str(s) 41 2 3 4>' strCLink(1)) # tuple >>> str(Link . empty) O' empty string

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_2

Step: 3

blur-text-image_3

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

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions