Advertisement
aribahaz

Untitled

Jan 28th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. # Submitter: ****************ADDDDDDDDDDDD NAAAAAAAAAAAAME***************
  2. # Partner : ****************ADDDDDDDDDDDD NAAAAAAAAAAAAME***************
  3. # We certify that we worked cooperatively on this programming
  4. # assignment, according to the rules for pair programming
  5.  
  6.  
  7. from collections import defaultdict
  8. from goody import type_as_str
  9.  
  10. class Bag:
  11. def __init__(self, k_list = ''):
  12. #self.bag_dict = iter_val
  13. self.bag_dict = defaultdict(int)
  14. self.bag_dict = {k: k_list.count(k) for k in k_list}
  15. print(self.bag_dict)
  16.  
  17. def __repr__(self):
  18. #__repr__ method returns a string, which when passed to eval returns a newly
  19. #constructed bag with the same value (==) to the object __repr__ was called on.
  20. pass
  21.  
  22. def __str__(self):
  23. #__str__ method returns a string that more compactly shows a bag.
  24. if len(self.bag_dict.keys()) != 0:
  25. print( type_as_str(self.bag_dict.items()))
  26. else:
  27. return 'Bag()'
  28.  
  29. def __len__(self):
  30. #__len__ method returns the total number of values in the Bag.
  31. return sum(self.bag_dict.values())
  32.  
  33. def unique(self):
  34. #unique method returns the number of different (unique) values in the Bag.
  35. return len(self.bag_dict.keys())
  36.  
  37. def __contains__(self, key):
  38. #__contains__ method returns whether or not its argument is in the Bag (one or more times).
  39. if key in self.bag_dict.keys():
  40. return True
  41. return False
  42.  
  43. def count(self, key):
  44. #count method returns the number of times its argument is in the Bag: 0 if the argument is not in the Bag.
  45. if key not in self.bag_dict.keys():
  46. return 0
  47. else:
  48. return self.bag_dict[key]
  49.  
  50. def add(self, key):
  51. #add method adds its argument to the Bag: if that value is already in the Bag,
  52. #its count is incremented by 1; if it is not already in the Bag, it is added to the Bag with a count of 1.
  53. if key in self.bag_dict.keys():
  54. self.bag_dict[key] += 1
  55. else:
  56. self.bag_dict[key] = 1
  57.  
  58. def __add__(self, bag1, bag2):
  59. #__add__ method unions its two Bag operands: it returns a new Bag with all the values in Bag operands.
  60. return_bag = dict(bag1)
  61. for key in bag2.keys():
  62. return_bag.add(key)
  63.  
  64. def remove(self, key):
  65. #remove method that removes its argument from the Bag: if that value is already in the Bag, its count
  66. #is decremented by 1 (and if the count reduces to 0, the value is removed from the dictionary; if it is
  67. #not in the Bag, raise a ValueError exception, with an appropriate message that includes the value that
  68. #could not be removed.
  69. if key in self.bag_dict.keys():
  70. if self.bag_dict[key] == 1:
  71. self.bag_dict.pop(key)
  72. else:
  73. self.bag_dict[key] -= 1
  74. else:
  75. raise ValueError('Key is not in the bag')
  76.  
  77. def __equality__(self):
  78. #__eq__/__ne__ methods that return whether one Bag is equal/not equal to another: contains the same
  79. #values the same number of times. A Bag is not equal to anything whose type is not a Bag This this method
  80. #should not change either Bag.
  81. pass
  82.  
  83. def __iter__(self):
  84. #__iter__ method returns an object on which next can be called to produce every value in the Bag: all len of them.
  85. pass
  86.  
  87.  
  88.  
  89. if __name__ == '__main__':
  90. #driver tests
  91.  
  92. import driver
  93. driver.default_file_name = 'bscp21W18.txt'
  94. # driver.default_show_exception= True
  95. # driver.default_show_exception_message= True
  96. # driver.default_show_traceback= True
  97. driver.driver()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement