# sets -- some operations and examples # # Matt Bishop, ECS 36A, Winter 2019 # # create the sets a = [ 1, 2, 3, 4, 5, 5 ] b = [ 4, 5, 6, 6, 7, 8 ] c = [ 7, 7, 8, 9, 10, 11] # build the sets from the lists sa = set(a) sb = set(b) sc = set(c) # show the two print("List a:", a, " set sa:", sa) print("List b:", b, " set sb:", sb) print("List c:", c, " set sc:", sc) # pause input("\nHit return to continue"); print("\n", end='') # now a set from a word s = "supercalifragilisticexpealadocious" ls = list(s) ss = set(ls) # show them print("String:", s, "\nList:", ls, "\nset:", ss) # pause input("\nHit return to continue"); print("\n", end='') # print the size of our sets print("Length of list a:", len(list(a)), "and size of set sa:", len(sa)) print("Length of list b:", len(list(b)), "and size of set sb:", len(sb)) print("Length of list c:", len(list(c)), "and size of set sc:", len(sc)) print("Length of string s:", len(s), "and size of set ss:", len(ss)) # pause input("\nHit return to continue"); print("\n", end='') # and set membership print("3 in set sa (3 in sa):", 3 in sa) print("9 in set sa (9 in sa):", 9 in sa) # pause input("\nHit return to continue"); print("\n", end='') # union print("Set sa:", sa, "and set sb:", sb) su1 = sa | sb su2 = sa.union(sb) print("Union: sa | sb:", su1) print("Union: sa.union(sb):", su2) # pause input("\nHit return to continue"); print("\n", end='') # intersection print("Set sa:", sa, "and set sb:", sb) si1 = sa & sb si2 = sa.intersection(sb) print("Intersection: sa & sb:", si1) print("Intersection: sa.intersection(sb):", si2) # pause input("\nHit return to continue"); print("\n", end='') # difference print("Set sa:", sa, "and set sb:", sb) sd1 = sa - sb sd2 = sa.difference(sb) print("Difference: sa - sb:", sd1) print("Difference: sa.difference(sb):", sd2) # pause input("\nHit return to continue"); print("\n", end='') # isdisjoint print("Set sa:", sa, "and set sb:", sb, "and set sc:", sc) sid1 = sa.isdisjoint(sb) sid2 = sa.isdisjoint(sc) print("Are they disjoint? sa.isdisjoint(sb):", sid1) print("Are they disjoint? sa.isdisjoint(sc):", sid2) # pause input("\nHit return to continue"); print("\n", end='') # add elements print("Set sa:", sa, "and set sb:", sb) sa.add(100) print("Add an element (sa.add(100)):", sa) sa.update(sb) print("Add a bunch of elements (sa.update(sb)):", sa) # pause input("\nHit return to continue"); print("\n", end='') # example of frozensets -- they are immutable fa = frozenset(a) print("Frozen set fa:", fa, "-- now add 100 (or try to)") try: fa.add(100) except Exception as msg: print("fa.add(100) failed --", msg) else: print("Add an element (fa.add(100)):", fa) # pause input("\nHit return to continue"); print("\n", end='') # #now we're going to generate a word list -- we don't care how many # times a word appears, though # # we also look for duplicate words in the test # # return True if there are duplicates in the list t, False if not # do this with sets -- if the number of words in the list is the # same as the number of elements in the set, there are no duplicates # otherwise there are def hasduplicates(t): return len(set(t)) != len(t) # here's where we keep the words -- it's a set as we just care # about the words, not how many times they occur words = set() # # now we read the input, check each line for duplicates, and put # the words in the line in the set (which is cumulative) while True: try: # read the input and split it into words s = input("% ").split() # quit on EOF except EOFError: break # any other error -- print error message and quit except Exception as msg: print("Error --", msg) break # now report whether there are any dulicate words in the line # this is so we can see how sets handle duplicates if hasduplicates(s): print("Has duplicates") else: print("No duplicates") # now add the words words.update(s) # this also works for w in s: words.add(w) # # now print the words # # ... in the order they are in the set print("Now the words in the order they are in the set") for i in words: print(i, end=' ') print("\n\nNow the words in sorted order") # ... in sorted order for i in sorted(words): print(i, end=' ') print("")