""" my_list class (template for Lab 3 Sp 2015): A simple list class with operations that can be performed quickly. By John Dougherty, possibly Kris Brower, Dave Wonnacott Examples: >>> print my_list() [] >>> print my_list().prepend(5) [5] >>> print my_list().prepend(5).prepend(3).prepend(16) [16, 3, 5] >>> print my_list().empty() True >>> print my_list().prepend(5).prepend(3).prepend(16).empty() False >>> print my_list().prepend(5).prepend(3).prepend(16).head() 16 >>> print my_list().prepend(5).prepend(3).prepend(16).rest() [3, 5] >>> print my_list().prepend(5).prepend(3).prepend(16).rest().head() 3 >>> print my_list().prepend(5).prepend(3).prepend(16).size() 3 >>> print my_list().prepend(5).prepend(3).prepend(16).rest().size() 2 >>> sample = my_list().prepend(5).prepend(3).prepend(16) >>> print sample [16, 3, 5] >>> print sample.index(1) 3 >>> print sample.index(0) 16 >>> sample.remove(1) >>> print sample [16, 5] >>> sample = my_list().prepend(5).prepend(3).prepend(16) >>> sample.remove(2) >>> print sample [16, 3] >>> sample = my_list().prepend(5).prepend(3).prepend(16) >>> sample.remove(0) >>> print sample [3, 5] >>> sample2 = my_list() >>> print sample2.remove(1) None >>> print sample2.index(0) None """ import sys sys.path.append('/home/courses/python') from logic import * class my_list: # First, the constructors: # A list is either empty, or an item followed by a list # constructor: primitive (my_list()) # Complexity: O(1) or constant def __init__(self): self.rep = [] # constructor: prepending (my_list(l, x)) # precondition: self must be a my_list (this has to be true in Python) # Complexity: O(1) or constant def prepend(self, x): t = my_list() t.rep = [x] + self.rep return t # Now the accessor functions: # empty, head, rest, and equality checking # Also, for convenience, a printing function #empty #axioms: # empty(my_list()) === True # empty(my_list(h, r)) === False # Complexity: O(1) or constant def empty(self): return len(self.rep) == 0 # head(my_list()) === undefined (precondition out) # head(my_list(h, r)) === h # Complexity: O(1) or constant def head(self): precondition(not self.empty()) return self.rep[0] # rest(my_list()) === undefined (precondition out) # rest(my_list(h, r)) === r # Complexity # UPDATE: complexity should be O(slicing) def rest(self): precondition(not self.empty()) r = my_list() r.rep = self.rep[1:] return r # equals # Complexity: O(length of the shorter list), or linear # eq(my_list(), my_list()) === True # eq(my_list(), my_list(h,r)) === False WLOG # eq(my_list(h1,r1), my_list(h2,r2)) # === (h1 == h2) and eq(r1, r2) # or can be stated ... # === (h1 == h2) and (r1 == r2) def __eq__(self, other): return self.rep == other.rep # display (just uses built-in python operation) # Complexity: O(length of the list) def __str__(self): return str(self.rep) # abstraction (just uses built-in python operation) # Complexity: O(length of the list) def __repr__(self): return repr(self.rep) # size of a list # size(my_list()) === 0 # size(my_list(h, r)) === 1 + size(r) # Complexity: accept either constant or linear, ideally # complexity of the Python len() function :) def size(self): return len(self.rep) #index(my_list(), where) === undefined #index(my_list(h, r), where) #=== undefined if (where < 0) or (where is not an integer) #=== h if (where == 0) #=== index(r, where-1) else # Complexity: linear, n (worst case) def index(self, where): x = 0 for node in self.rep: if x == where: return node else: x += 1 # Complexity: constant, assuming that slicing has constant complexity time # https://wiki.python.org/moin/TimeComplexity def remove(self, where): # mutate the list such that the item in original position # where is no longer in list self.rep = self.rep[:where] + self.rep[(where+1):] # The following gets the "doctest" system to check test cases in the documentation comments def _test(): import doctest return doctest.testmod() if __name__ == "__main__": if _test()[0] == 0: print "Congratulations! You have passed all the tests"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question