Advertisement
superpawko

Untitled

Oct 5th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. class intSet(object):
  2. """An intSet is a set of integers
  3. The value is represented by a list of ints, self.vals.
  4. Each int in the set occurs in self.vals exactly once."""
  5.  
  6. def __init__(self):
  7. """Create an empty set of integers"""
  8. self.vals = []
  9.  
  10. def insert(self, e):
  11. """Assumes e is an integer and inserts e into self"""
  12. if not e in self.vals:
  13. self.vals.append(e)
  14.  
  15. def member(self, e):
  16. """Assumes e is an integer
  17. Returns True if e is in self, and False otherwise"""
  18. return e in self.vals
  19.  
  20. def remove(self, e):
  21. """Assumes e is an integer and removes e from self
  22. Raises ValueError if e is not in self"""
  23. try:
  24. self.vals.remove(e)
  25. except:
  26. raise ValueError(str(e) + ' not found')
  27.  
  28. def __str__(self):
  29. """Returns a string representation of self"""
  30. self.vals.sort()
  31. return '{' + ','.join([str(e) for e in self.vals]) + '}'
  32.  
  33. def intersect(self, other):
  34.  
  35. return set(self.vals) & set(other.vals)
  36.  
  37. def __len__(self):
  38. #self.vals
  39. result = []
  40. for i in set(self.vals):
  41. i += 1
  42. return i
  43.  
  44.  
  45. set1 = intSet()
  46. set2 = intSet()
  47.  
  48. for i in range(10,15):
  49. set1.insert(i)
  50.  
  51. for i in range(13,18):
  52. set2.insert(i)
  53.  
  54.  
  55. print(set1)
  56. print(set2)
  57. print(set2.intersect(set1))
  58. print(len(set1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement