lab2_listbag

.py

School

Carleton University *

*We aren’t endorsed by this school

Course

2100

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

4

Uploaded by BarristerElk2701

Report
# SYSC 2100 Winter 2024 Lab 2 # An implementation of ADT Bag that uses an instance of Python's built-in # list type as the underlying data structure. import random __author__ = 'Belal Alobieda' __student_number__ = '101151327' class ListBag: def __init__(self, iterable=[]) -> None: """Initialize this ListBag. If no iterable is provided, the new ListBag is empty. Otherwise, initialize the ListBag by adding the values provided by the iterable. >>> bag = ListBag() >>> bag ListBag([]) >>> bag = ListBag([1, 4, 3, 6, 3]) >>> bag ListBag([1, 4, 3, 6, 3]) """ self._elems = [] for item in iterable: self._elems.append(item) # or, self.add(item) def __str__(self) -> str: """Return a string representation of this ListBag. >>> bag = ListBag() >>> str(bag) '{}' >>> bag = ListBag([1, 4, 3, 6, 3]) >>> str(bag) '{1, 4, 3, 6, 3}' Note: Depending on how __str__ is implemented, the order of the elements in the returned string may be different. """ # Use the string representation of the bag's underlying list, # with the enclosing '[]' replaced by '{}'. s = str(self._elems) return '{' + s[1:len(s) - 1] + '}' def __repr__(self) -> str: """Return the canonical string representation of this ListBag. >>> bag = ListBag() >>> repr(bag) 'ListBag([])' >>> bag = ListBag([3, 1, 2, 3, 4]) >>> repr(bag) 'ListBag([3, 1, 2, 3, 4])'
Note: Depending on how __repr__ is implemented, the order of the list elements in the returned string may be in a different. """ # For a ListBag object, obj, the expression eval(repr(obj)) # returns a new ListBag that is equal to obj. return "{0}({1})".format(self.__class__.__name__, self._elems) # or, return self.__class__.__name__ + '(' + str(self._elems) + ')' def __len__(self) -> int: """Return the number of items in this ListBag. >>> bag = ListBag() >>> len(bag) 0 >>> bag = ListBag([1, 4, 3, 6]) >>> len(bag) 4 """ return len(self._elems) def __iter__(self) -> 'list_iterator': """Return an iterator for this ListBag. >>> bag = ListBag([3, 6, 3]) >>> for x in bag: ... print(x) ... 3 6 3 """ return iter(self._elems) def __contains__(self, item: any) -> bool: """Return True if item is in this ListBag; otherwise False. >>> bag = ListBag() >>> 2 in bag False >>> bag = ListBag([1, 4, 3, 6]) >>> 4 in bag True >>> 7 in bag False """ return item in self._elems def add(self, item: any) -> None: """Add item to this ListBag. >>> bag = ListBag([1, 4, 3, 6]) >>> bag.add(3) >>> len(bag) 5 >>> str(bag) '{1, 4, 3, 6, 3}'
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help