homework2

docx

School

Cypress College *

*We aren’t endorsed by this school

Course

275

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

2

Uploaded by DeanInternet13057

Report
Yen Phuong Lam – CIS 275 – Homework2 Array.py class Array: def __init__(self, capacity): """ Capacity is the static size of the array. Each index is initialized to None """ self._items = [] self.logical_size = 0 for i in range(capacity): self._items.append(None) def __getter__(self): return self.logical_size def __len__(self): """ Returns the capacity of this Array """ return len(self._items) def __str__(self): """ Returns a string representation of this Array """ return str(self._items) def __iter__(self): """ Returns an iterator over the Array """ return iter(self._items) def __getitem__(self, index): """ Return the item at the given index """ return self._items[index] def __setitem__(self, index, new_item): """ Adds the value 'new_item' to the array at the given index """ if index == self.logical_size: try: self._items[index] = new_item if index == self.logical_size: self.logical_size += 1 except IndexError: raise IndexError(f"Array's index is out of range") elif index > self.logical_size and self._items[index] == None: raise ValueError("Cannot skip index like that! ") elif index < self.logical_size and self._items[index] != None: raise ValueError("You are not allowed to update logical value") def __eq__(self, other): return type(self._items) == type(other._items) and self.logical_size == other.logical_size and self._items == other._items main.py from Array import Array
Yen Phuong Lam – CIS 275 – Homework2 def main(): a1 = Array(5) a1[0] = 12 a1[1] = 32 a1[2] = 45 print(a1) a2 = Array(5) a2[0] = 52 a2[1] = 33 a2[2] = 32 print(a2) if a1 == a2: print("Two arrays are similar") else: print("Two arrays are not similar ") main()
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