def linearset_examples(): smith = Set() smith.add("CSCI-112") smith.add("MATH-121") smith.add("HIST-340") smith.add("ECON-101") roberts = Set() roberts.add("POL-101") roberts.add("ANTH-230") roberts.add("CSCI-112") roberts.add("ECON-101") if (smith == roberts): print "Smith and Roberts are taking the same courses" else: sameCoures = smith.intersect(roberts) if sameCoures.isEmpty(): print "Smith and Roberts are not taking the same courses." else: print "Smith and Roberts are taking some same courses." print sameCoures uniqueCourses = smith.difference(roberts) print "Smith are taking some different courses compared to Roberts." print uniqueCourses
def test_mul(self): set = Set(1, 2) otherSet = Set(1, 3) newSet = set * otherSet self.assertEqual(newSet.__contains__(1), True) self.assertEqual(newSet.__contains__(2), False) self.assertEqual(newSet.__contains__(3), False)
def test__init__(self): set = Set() # print(set._theElements) self.assertEqual(set._theElements, []) set = Set(1, 2, 3) # print(set._theElements) self.assertEqual(set._theElements, [1, 2, 3])
def test_difference(self): set = Set() set.add(1) set.add(2) otherSet = Set() otherSet.add(2) self.assertEqual(1 in set.difference(otherSet), True) self.assertEqual(2 in set.difference(otherSet), False)
def test_intersect(self): set = Set() set.add(1) set.add(2) otherSet = Set() otherSet.add(2) otherSet.add(3) self.assertEqual(2 in set.intersect(otherSet), True)
def test_properSubset(self): set = Set() otherSet = Set() set.add(1) otherSet.add(1) otherSet.add(2) self.assertEqual(set.properSubset(otherSet), True) set.add(2) self.assertEqual(set.properSubset(otherSet), False)
def linearset_test(): # test init function s1 = Set() s2 = Set() s3 = Set() # test len function for i in range(16): s1.add(i) print 'len = ', len(s1) print "s1:" for elem in s1: print elem, print "" for i in range(16): s2.add(i) print 'len = ', len(s2) print "s2:" for elem in s2: print elem, print "" print "s2: using print function" print s2 for i in range(0, 32, 2): s3.add(i) print 'len = ', len(s3) print "s3:" for elem in s3: print elem, print "" print "s1 == s2", s1 == s2 print "s1 == s3", s1 == s3 print "s3 is the SubsetOf s2 ?shuould be False: ", s3.isSubsetOf(s2) print "s2 is the SubsetOf s1 ?shuould be True: ", s2.isSubsetOf(s1) print "s2 is the isProperSubsetOf s1 ?shuould be False: ", s2.isProperSubsetOf( s1) s4 = s1.union(s3) print "s4 is the union of s1 and s3" for elem in s4: print elem, print "" s5 = s1.intersect(s3) print "s5 is the intersect of s1 and s3" for elem in s5: print elem, print "" s6 = s1.difference(s3) print "s6 is the difference of s1 and s3" for elem in s6: print elem, print "" s7 = s1 + s3 print "s7 is the union of s1 and s3,using add " for elem in s7: print elem, print "" s8 = s1 * s3 print "s8 is the intersect of s1 and s3,using mul" for elem in s8: print elem, print "" s9 = s1 - s3 print "s9 is the difference of s1 and s3,using sub" for elem in s9: print elem, print "" # test s10 = Set(1, 2, 3) print "s10 test init" for elem in s10: print elem, print "" print "s10 : test _str" s10._str() print s10
def test___add__(self): set = Set(1) otherSet = Set(2) newSet = set + otherSet self.assertEqual(newSet.__contains__(1), True) self.assertEqual(newSet.__contains__(2), True)
def test___str__(self): set = Set() set.add(1) set.add(2) print(set)
def test_isEmpty(self): set = Set() self.assertEqual(set.isEmpty(), True)
def test__contains__(self): set = Set() self.assertEqual(0 in set, False)
def test_sub(self): set = Set(1, 2) otherSet = Set(2) newSet = set - otherSet self.assertEqual(newSet.__contains__(1), True) self.assertEqual(newSet.__contains__(2), False)
def test_nion(self): set = Set() set.add(1) otherSet = Set() otherSet.add(2) self.assertEqual(2 in set.union(otherSet), True)
def test__eq__(self): set = Set() set.add(1) otherSet = Set() otherSet.add(1) self.assertEqual(set == otherSet, True)
def test_isSubsetOf(self): set = Set() set.add(1) otherSet = Set() otherSet.add(1) self.assertEqual(set.isSubsetOf(otherSet), True)
def test_remove(self): set = Set() set.add(1) self.assertEqual(1 in set, True) set.remove(1) self.assertEqual(1 in set, False)
def test_add(self): set = Set() set.add(1) self.assertEqual(1 in set, True)
def test_isSubsetOf(self): set = Set(1, 2) otherSet = Set(1, 2, 3) self.assertEqual(set < otherSet, True)
from linearset import Set smith = Set() smith.add("CSCI-112") smith.add("MATH-121") smith.add("HIST-340") smith.add("ECON-101") roberts = Set() roberts.add("POL-101") roberts.add("ANTH-230") roberts.add("CSCI-112") roberts.add("ECON-101") if smith == roberts: print("Smith and Roberts are taking the same courses.") else: sameCourses = smith.intersect(roberts) if sameCourses.isEmpty(): print( "Smith and Roberts are not taking any of " \ + "the same courses." ) else: print( "Smith and Roberts are taking some of the " \ + "same courses:" ) for course in sameCourses: print(course) uniqueCourses = smith.difference(roberts) for course in uniqueCourses: print(course)
def test__len__(self): set = Set() self.assertEqual(len(set), 0)