Question: Why does the following code print this ( < _ _ main _ _ . List 1 3 5 object at 0 x 0 0
Why does the following code print this mainList object at xDACD I want the function firstN to return the first n elements of a list object xs as a new list object and output in this example
class List:
# create new list node. Defaults to a node representing the empty list
def initself initdataNone, initnextNone:
self.data initdata
self.next initnext
# is the list beginning with self the end of the list?
def emptyself:
return self.next None
# return the first element of the list that starts with self
def firstself:
return self.data
# return the list that begins after the list that starts with self
def restself:
return self.next
# return a list beginning with data, followed by list beginning with self
def addself data:
return Listdata self
# tailrecursively accumulates strcurdata for each remaining item
def strself cur, acc:
if cur.next None:
return acc
else:
nextacc acc strcurdata
nextcur cur.next
return self.strnextcur nextacc
def strself:
if self.next None:
return
else:
acc strselfdata # init accumulator with first item
cur self.next # continue accumulation with next
return self.strcur acc
def firstNxs n:
if n :
return temp
else:
temp List
temp.addxsfirst
return xsrest n
if namemain:
xs Listaddaddadd
zs firstNxs
printstrzs
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
