Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Submitter: ****************ADDDDDDDDDDDD NAAAAAAAAAAAAME***************
- # Partner : ****************ADDDDDDDDDDDD NAAAAAAAAAAAAME***************
- # We certify that we worked cooperatively on this programming
- # assignment, according to the rules for pair programming
- from collections import defaultdict
- from goody import type_as_str
- class Bag:
- def __init__(self, k_list = ''):
- #self.bag_dict = iter_val
- self.bag_dict = defaultdict(int)
- self.bag_dict = {k: k_list.count(k) for k in k_list}
- print(self.bag_dict)
- def __repr__(self):
- #__repr__ method returns a string, which when passed to eval returns a newly
- #constructed bag with the same value (==) to the object __repr__ was called on.
- pass
- def __str__(self):
- #__str__ method returns a string that more compactly shows a bag.
- if len(self.bag_dict.keys()) != 0:
- print( type_as_str(self.bag_dict.items()))
- else:
- return 'Bag()'
- def __len__(self):
- #__len__ method returns the total number of values in the Bag.
- return sum(self.bag_dict.values())
- def unique(self):
- #unique method returns the number of different (unique) values in the Bag.
- return len(self.bag_dict.keys())
- def __contains__(self, key):
- #__contains__ method returns whether or not its argument is in the Bag (one or more times).
- if key in self.bag_dict.keys():
- return True
- return False
- def count(self, key):
- #count method returns the number of times its argument is in the Bag: 0 if the argument is not in the Bag.
- if key not in self.bag_dict.keys():
- return 0
- else:
- return self.bag_dict[key]
- def add(self, key):
- #add method adds its argument to the Bag: if that value is already in the Bag,
- #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.
- if key in self.bag_dict.keys():
- self.bag_dict[key] += 1
- else:
- self.bag_dict[key] = 1
- def __add__(self, bag1, bag2):
- #__add__ method unions its two Bag operands: it returns a new Bag with all the values in Bag operands.
- return_bag = dict(bag1)
- for key in bag2.keys():
- return_bag.add(key)
- def remove(self, key):
- #remove method that removes its argument from the Bag: if that value is already in the Bag, its count
- #is decremented by 1 (and if the count reduces to 0, the value is removed from the dictionary; if it is
- #not in the Bag, raise a ValueError exception, with an appropriate message that includes the value that
- #could not be removed.
- if key in self.bag_dict.keys():
- if self.bag_dict[key] == 1:
- self.bag_dict.pop(key)
- else:
- self.bag_dict[key] -= 1
- else:
- raise ValueError('Key is not in the bag')
- def __equality__(self):
- #__eq__/__ne__ methods that return whether one Bag is equal/not equal to another: contains the same
- #values the same number of times. A Bag is not equal to anything whose type is not a Bag This this method
- #should not change either Bag.
- pass
- def __iter__(self):
- #__iter__ method returns an object on which next can be called to produce every value in the Bag: all len of them.
- pass
- if __name__ == '__main__':
- #driver tests
- import driver
- driver.default_file_name = 'bscp21W18.txt'
- # driver.default_show_exception= True
- # driver.default_show_exception_message= True
- # driver.default_show_traceback= True
- driver.driver()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement