""" Dictionary class, and two implementations >>> d = Dictionary() >>> d.put('Dave', 4973) >>> d.put('J.D.', 4993) >>> d.put('CS lab', 1202) >>> d.lookup('Dave') 4973 >>> d.put('Dave', 1202) # when Dave is working in the lab >>> d.lookup('Dave') 1202 >>> d.lookup('CS lab') 1202 >>> d.lookup('Steven') 'No entry' >>> d2 = d.withEntry('Steven', 1203) >>> d.lookup('Steven') 'No entry' >>> d2.lookup('Steven') 1203 # Add some tests of "merge" here """ # make Python look in the right place for logic.py, or complain if it doesn't try: import sys sys.path.append('/home/courses/python') from logic import * except: print "Can't find logic.py; if this happens in the CS teaching lab, tell your instructor" print " If you are using a different computer, add logic.py to your project" print " (You can download logic.py from http://www.cs.haverford.edu/resources/software/logic.py)" sys.exit(1) class Dictionary1: """ A dictionary represented with a Python list. Each list element is a tuple of two parts, the key and the value """ def __init__(self): precondition(True) self.__listOfPairs = [] # "extending" constructor method: 'with' # d.withEntry(k, v) has all the key/value pairs of d, together with a new entry def withEntry(self, key, value): precondition(True) result = Dictionary1() result.__listOfPairs = [ (key, value) ] + self.__listOfPairs return result # axioms: # Dictionary().lookup(x) === "No entry" # d.withEntry(k, v).lookup(x) === v, if k==x # or d.lookup(x), otherwise def lookup(self, key): precondition(True) for pair in self.__listOfPairs: if pair[0] == key: return pair[1] return "No entry" # axioms: # d.put(k, v) --> new d is d.withEntry(k, v) def put(self, key, value): precondition(True) self.__listOfPairs = [ (key, value) ] + self.__listOfPairs def __eq__(self,otherDictionary): precondition( isinstance(otherDictionary,Dictionary1) ) if len(self.rep) != len(otherDictionary.rep): return False for x in self.rep, y in otherDictionary.rep: if x != y or self.rep[x] != otherDictionary.rep[y]: return False return True def __eq__(self,otherDictionary): precondition( isinstance(otherDictionary,Dictionary1) ) if self.__repr__() != otherDictionary.__repr__(): return False return True # axioms: # fill these in def merge(self, otherDictionary): precondition( isinstance(otherDictionary,Dictionary1) ) dict = {} for x in self.rep: dict[(x[0])] = x[1] for y in otherDictionary.rep: dict[(y[0])] = y[1] return dict def merge(self, otherDictionary): precondition( isinstance(otherDictionary,Dictionary1) ) return self.rep + otherDictionary.rep def __repr__(self): precondition(???) return str(self.rep) def __repr__(self): precondition(???) dict = {} for x in self.rep: dict[(x[0])] = x[1] return dict # by default, use the first representation, but this is changed in DocTest below Dictionary = Dictionary1 # mostly copied from http://docs.python.org/lib/module-doctest.html def _test(): import doctest global Dictionary whatever_dictionary_was = Dictionary print "=========================== Running doctest tests for Dictionary1 ===========================\n" Dictionary = Dictionary1 result = doctest.testmod() if result[0] == 0: print "Wahoo! Passed all", result[1], __file__.split('/')[-1], "tests!" else: print "Rats!" print "\n\n\n\n" print "=========================== Running doctest tests for Dictionary2 ===========================\n" Dictionary = Dictionary2 result = doctest.testmod() if result[0] == 0: print "Wahoo! Passed all", result[1], __file__.split('/')[-1], "tests!" else: print "Rats!" Dictionary = whatever_dictionary_was if __name__ == "__main__": _test()
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