Homework 7: Object Oriented Programming, Linked Lists
Instructions
hw07.zip (hw07.zip)
Required Questions
Linked Lists
.stniop 2 fo tuo si krowemoh sihT .suballys eht ni detats sa ycilop yrevocer krowemoh a si erehT .tniop eno yb erocs latot eht esaercedlliwmelborptcerrocnihcaE.ssentcerrocnodesabdedargsikrowemoH:gnidarG
)lmth.stcejbo-evisrucer-92/segap/moc.smargorpgnisopmoc//:ptth(9.2noitceS :lufesusecnerefergniwollofehtdn thgimuoY:sgnidaeR
)ko-gnisu/selcitra/( .ediugsihtotreferesaelp,kOgnisutuobasnoitseuqynaevahuoyfI:kOgnisU
.stnemngissa gnittimbus nosnoitcurtsnieromrof)tnemngissa-eht-gnittimbus#00bal/bal/(0baLeeS.epocsedarG noedocruoydettimbusyllufsseccusevahuoytahtkcehC.derocseblliwnoissimbus lan ehtylno;enildaedehterofebecnonahteromtimbusyamuoY.epocsedarGotdetide ev’uoysel edocllagnidaolpuybtnemngissaehttimbus,enoderauoynehW:noissimbuS
.redargotua eht fo ypoc a htiw gnola ,)yp.70wh( yp.70whdellacel adn lliwuoy,evihcraehtedisnI.)piz.70wh(piz.70whdaolnwoD
soediV detratS gnitteG
6lirpA,yadsruhTnomp95:11ybeuD
Q1: Store Digits
store_digits
str reversed
def store_digits(n):
“””Stores the digits of a positive number n in a linked list.
>>> s = store_digits(1)
>>> store_digits(2345)
Link(2, Link(3, Link(4, Link(5))))
>>> store_digits(876)
Link(8, Link(7, Link(6)))
>>> store_digits(2450)
Link(2, Link(4, Link(5, Link(0))))
>>> # a check for restricted functions
>>> import inspect, re
>>> cleaned = re.sub(r”#.*\\n”, ”, re.sub(r'”{3}[\s\S]*?”{3}’, ”, inspect.getsour
>>> print(“Do not use str or reversed!”) if any([r in cleaned for r in [“str”, “rev
>>> link1 = Link(3, Link(Link(4), Link(5, Link(6))))
“*** YOUR CODE HERE ***”
python3 ok -q store_digits
Q2: Mutable Mapping
deep_map_mut(func, link)
.stsil deknilwenynaetaerctonoD.tsildeknillanigiroehtetatumdluohsnoitatnemelpmiruoY
.no os dna ,stnemele sti fo hcae ot ylppa ,tsil deknil a flesti si tnemele na fI . tsil deknil nevig eht nistnemelellaotnonoitcnufaseilppahcihw, tnemelpmI
. dna ekilsnoitcnufnoitalupinamgnirtsynaesutonoD:tnatropmI
. fo tigid a si tsil eht fo tnemele hcae erehwtsildeknilasnruterdnaregetninanisekattahtnoitcnufaetirW
:edoc ruoy tset ot kO esU
CS Help, Email: tutorcs@163.com
isinstance
>>> s = Link(1, Link(2, Link(3, Link(4))))
>>> isinstance(s, Link)
>>> isinstance(s, int)
s = Link(1)
def deep_map_mut(func, lnk):
“””Mutates a deep link lnk by replacing each item found with the
result of calling func on the item. Does NOT create new Links (so
no use of Link’s constructor).
Does not return the modified Link object.
>>> link1 = Link(3, Link(Link(4), Link(5, Link(6))))
>>> # Disallow the use of making new Links before calling deep_map_mut
>>> Link.__init__, hold = lambda *args: print(“Do not create any new Links.”), Link
… deep_map_mut(lambda x: x * x, link1)
… finally:
… Link.__init__ = hold
>>> print(link1)
<9 <16> 25 36>
“*** YOUR CODE HERE ***”
python3 ok -q deep_map_mut
.e.i ,rotcurtsnoc eht gnillac yb stsil knilgnitaerctonerauoytahterusne,tsetcodsihtgniliaferauoyfI.stsildeknilwen etaerctonoduoytahtserusnenoitseuqsihtfotsetcodtsalehT:kcehCtcurtsnoC
.lufesuebyamnoitcnuf ni-tliubehT:tniH
:edoc ruoy tset ot kO esU
Q3: Two List
def two_list(vals, counts):
Returns a linked list according to the two lists that were passed in. Assume
vals and counts are the same size. Elements in vals represent the value, and the
corresponding element in counts represents the number of this value desired in the
final linked list. Assume all elements in counts are greater than 0. Assume both
lists have at least one element.
>>> a = [1, 3]
>>> b = [1, 1]
>>> c = two_list(a, b)
Link(1, Link(3))
>>> a = [1, 3, 2]
>>> b = [2, 2, 1]
>>> c = two_list(a, b)
Link(1, Link(1, Link(3, Link(3, Link(2)))))
“*** YOUR CODE HERE ***”
python3 ok -q two_list
Mutable Trees
Q4: Add Leaves
add_d_leaves
.sehcnarbfotsiltahtfodneehtotsevaelesehtddadluohsuoy,sehcnarb gnitsixesahhtpedsihttaedonehtfI. folebalaevahdluohsfaeldeddayrevE.edon ehtfohtpedehtsi erehw,tiotsevael ddadluohsuoy,eertehtniedonhcaeroF
.0 erofereht si toor fo htped ehT .edontahtottoorehtmorfsegdeforebmunehtebot niedonafohtpedehten edeW
. rebmunadna ecnatsni anisekattahtnoitcnufa, tnemelpmI
.0nahtretaergeratsildnocesehtnistnemelellaemussA.retaergro1fohtgnel a evah dna ezis emas eht era stsil htob emussA .eulav gnidnopserroc hcae fo rebmun eht sniatnoc tsil dnoces eht dna ,tsil deknil eht ni tup ot tnaw ew taht seulav eht sniatnoc tsil tsr ehT.tsildeknilasnruterdnastsilowtnisekattahtnoitcnufatnemelpmI
:edoc ruoy tset ot kO esU
add_d_leaves
!htpedehtfokcartpeekotnoitcnufreplehaesU:tniH
. gnitatumsinoitcnufehtwohezilausivottsetcoddnocesehttuogniwardyrT
.5 sa htiw deilppa si retfatluserehtdna)tfelehtnonwohs( eertafoelpmaxenasiereH
.noosdna,2htpedtaedonhcae otsevael2,1htpedtaedonhcaeot lebalhtiwfael1gniddaebdluohsuoy,elpmaxeroF
程序代写 CS代考 加微信: cstutorcs
def add_d_leaves(t, v):
“””Add d leaves containing v to each node at every depth d.
>>> t_one_to_four = Tree(1, [Tree(2), Tree(3, [Tree(4)])])
>>> print(t_one_to_four)
>>> add_d_leaves(t_one_to_four, 5)
>>> print(t_one_to_four)
>>> t1 = Tree(1, [Tree(3)])
>>> add_d_leaves(t1, 4)
Tree(1, [Tree(3, [Tree(4)])])
>>> t2 = Tree(2, [Tree(5), Tree(6)])
>>> t3 = Tree(3, [t1, Tree(0), t2])
>>> print(t3)
>>> add_d_leaves(t3, 10)
>>> print(t3)
“*** YOUR CODE HERE ***”
python3 ok -q add_d_leaves
.)epocsedarg-htiw-timbus#/00bal/bal/gro.a16sc//:sptth( 00baLotrefer,sihtodotwohnorehserferaroF.tnemngissaepocsedarGetairporppa eht ot detide ev’uoy sel yna gnidaolpu yb tnemngissa siht timbus ot erus ekaM
:edoc ruoy tset ot kO esU
Code Help, Add WeChat: cstutorcs
Exam Practice
)7=egap#fdp.2tm -81ps-a16/2tm/81ps/maxe/gro.a16sc//:sptth( seerT :5Q 2TM 8102 gnirpS
)7=egap#fdp.2tm -91ps-a16/2tm/91ps/maxe/gro.a16sc//:sptth( siht eirT :6Q 2TM 9102 gnirpS
)61=egap#fdp.lan -12af-a16/lan /12af/maxe/gro.a16sc//:sptth( nadalaC :3Q laniF 1202 llaF
)7=egap#fdp.1tm -71ps-a16/1tm/71ps/maxe/gro.a16sc//:sptth( tresnI :5Q 1TM 7102 gnirpS
)6=egap#fdp.2tm -81af-a16/2tm/81af/maxe/gro.a16sc//:sptth( knilneknarF .rD :6Q 2TM 8102 llaF
)9=egap#fdp.lan -02af-a16/lan /02af/maxe/gro.a16sc//:sptth( ytraP egelloC :3Q laniF 0202 llaF
!ecitcarp emosekild’uoyfimehttpmettaoteerfleef;tnenopmocnoissimbusonevahsnoitseuq esehT.yrtotuoyrofsnoitseuqmaxeroirpniatnocoslalliwstnemngissakrowemoH
seerT elbatuM
stsiL dekniL