def __le__(self,other):
if self._isEmpty():
return True # empty list is less than or equal to anything
elif other._isEmpty():
return False # cannot otherwise less than or equal to empty list
else:
# both lists are non empty; compare the heads
if self._head < other._head:
return True
elif self._head > other._head
return False
else:
# heads are the same; revert to rest
return self._rest < other._rest
def index(self, value, start=0):
if self._isEmpty():
raise ValueError('OurList.index(x): x not in list')
elif start == 0 and self._head == value:
return 0
else:
return 1 + self._rest.index(value, max(start-1,0))
def binary(n):
answer = str(n % 2) # rightmost bit, for now
if n >= 2:
answer = binary(n // 2) + answer
return answer
As a simple example, consider a call to
def __getslice__(self,i,j):
"""
Returns new list representing self[i:j]
Assumes that i and j are non-negative.
"""
if self._isEmpty() or i>=j:
return OurList() # a new empty list
elif i>0:
return self.rest.__getslice__(i-1,j-1)
else:
result = OurList()
result.head = self.head
result.rest = self.rest.__getslice__(0,j-1)
return result