Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. def oneBits(n):
  2.   i = 0
  3.   s = set()
  4.   while n > 0:
  5.     if n % 2 == 1:
  6.       s.add(i)
  7.     n = n / 2
  8.     i += 1
  9.   return s
  10.  
  11. def powerSet(n):
  12.   p = []
  13.   for i in range(0, 2 ** n):
  14.      p.append(oneBits(i))
  15.   return p
  16.  
  17.  
  18. p = powerSet(3)
  19. # print all the elements
  20. for s in p:
  21.   print s
  22.  
  23. print ""
  24. print "few examples"
  25. print ""
  26.  
  27. # print the intersection between the last set in P and the one before.
  28. print "intersection ", p[-1] , " with ", p[-2]
  29. print p[-1] & p[-2]
  30.  
  31. # print the diff between
  32. print "diff ", p[-1] , "-", p[-2]
  33. print p[-1] - p[-2]
  34.  
  35. # print the union
  36. print "union of", p[-1], " U ", p[-2]
  37. print p[-1] | p[-2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement