Question: Can someone help me with this please. Python . Same question, but Two part , ADD and REMOVE when i paste the code it doesn't

Can someone help me with this please. Python . Same question, but Two part , ADD and REMOVE

when i paste the code it doesn't indent

Can you please let me know which line i need to place the solution OR write the whole solution on coyyable format

Exercise 5.3

The add() method resizes the array properly when items are added to the array.

In the arraybag.py file define the add() method in the ArrayBag class by completing the following:

  1. Check array memory and increase it if necessary

To test your program run the test() method in the testbag.py file. ( i will post code for this too so you can see how it looks) This is to run test() only

from arraybag import ArrayBag

from linkedbag import LinkedBag

def test(bagType):

"""Expects a bag type as an argument and runs some tests

on objects of that type."""

print("Testing", bagType)

lyst = list(range(1, 11))

print("The list of items added is:", lyst)

b = bagType(lyst)

print("Expect the bag's string:", b)

print("Add 5 more items to test increasing the array size:")

for i in range(11, 16):

b.add(i)

print("Expect the bag's string:", b)

test(ArrayBag)

#test(LinkedBag)

Your program's output should look like the following:

Testing

The list of items added is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Expect the bag's string: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Add 5 more items to test increasing the array size:

Expect the bag's string: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

( Here is the Arrybag.py code Which need to be modified so test(ArrayBag) can run in testbag.py file)

from arrays import Array

class ArrayBag(object):

"""An array-based bag implementation."""

# Class variable

DEFAULT_CAPACITY = 10

# Constructor

def __init__(self, sourceCollection = None):

"""Sets the initial state of self, which includes the

contents of sourceCollection, if it's present."""

self.items = Array(ArrayBag.DEFAULT_CAPACITY)

self.size = 0

if sourceCollection:

for item in sourceCollection:

self.add(item)

# Accessor methods

def isEmpty(self):

"""Returns True if len(self) == 0, or False otherwise."""

return len(self) == 0

def __len__(self):

"""Returns the number of items in self."""

return self.size

def __str__(self):

"""Returns the string representation of self."""

return "{" + ", ".join(map(str, self)) + "}"

def __iter__(self):

"""Supports iteration over a view of self."""

cursor = 0

while cursor < len(self):

yield self.items[cursor]

cursor += 1

def __add__(self, other):

"""Returns a new bag containing the contents

of self and other."""

result = ArrayBag(self)

for item in other:

result.add(item)

return result

def __eq__(self, other):

"""Returns True if self equals other,

or False otherwise."""

if self is other: return True

if type(self) != type(other) or \

len(self) != len(other):

return False

for item in self:

if self.count(item) != other.count(item):

return False

return True

def count(self, item):

"""Returns the number of instances of item in self."""

total = 0

for nextItem in self:

if nextItem == item:

total += 1

return total

# Mutator methods

def clear(self):

"""Makes self become empty."""

self.size = 0

self.items = Array(ArrayBag.DEFAULT_CAPACITY)

def add(self, item):

"""Adds item to self."""

# Check array memory here and increase it if necessary

Exercise 5.4

( use above solution to answer this one )

The remove() method resizes the array when necessary by removing an item from the array if it exist

Be sure to reuse your solution from Programming Exercise 5.3 as your starter file for the arraybag.py file.

In the arraybag.py file define the remove() method in the ArrayBag class by completing the following:

1 Check the precondition and raise a KeyError if necessary

  • Precondition: item is in self.
  • Raises: KeyError if item in not in self.
  • Postcondition: item is removed from self.

2

  1. Search for the index of the target item
  2. Shift items to the left of target up by one position
  3. Decrement logical size
  4. Check array memory here and decrease it if necessary

To test your program run the testResize() method in the testbag.py file. ( i will post code for this on too like 5-3 ) this code is to run file only

from arraybag import ArrayBag

def test(bagType):

"""Expects a bag type as an argument and runs some tests

on objects of that type."""

print("Testing", bagType)

lyst = list(range(1, 11))

print("The list of items added is:", lyst)

b = bagType(lyst)

print("Expect the bag's string:", b)

print("Add 5 more items to test increasing the array size:")

for i in range(11, 16):

b.add(i)

print("Expect the bag's string:", b)

def testResize():

"""Tests the resizing of an array-based bag,

when space is wasted."""

bag = ArrayBag(range(100))

print("Added 100 items, length of bag =", len(bag))

print("Expect 160 as length of array =", len(bag.items))

print(bag)

for item in range(76):

bag.remove(item)

print("Removed 76 items, expect 24 as length of bag:", len(bag))

print("Expect 80 as length of array =", len(bag.items))

for item in range(76, 100):

bag.remove(item)

print("Removed remaining items, length of bag =", len(bag))

print("Expect 10 as length of array =", len(bag.items))

testResize()

(Below part was in question, I'm not sure what it's for)

Added 100 items, length of bag = 100 Expect 160 as length of array = 160 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99} Removed 76 items, expect 24 as length of bag: 24 Expect 80 as length of array = 80 Removed remaining items, length of bag = 0 Expect 10 as length of array = 10

( Here is the code Which need to be modified)

"""

Project 5.4

File: arraybag.py

Reuse your solution from Programming Exercise 5.3 as your starter file

"""

from arrays import Array

class ArrayBag(object):

"""An array-based bag implementation."""

# Reuse your solution from Programming Exercise 5.3 as your starter file

# Class variable

DEFAULT_CAPACITY = 10

# Constructor

def __init__(self, sourceCollection = None):

"""Sets the initial state of self, which includes the

contents of sourceCollection, if it's present."""

self.items = Array(ArrayBag.DEFAULT_CAPACITY)

self.size = 0

if sourceCollection:

for item in sourceCollection:

self.add(item)

# Accessor methods

def isEmpty(self):

"""Returns True if len(self) == 0, or False otherwise."""

return len(self) == 0

def __len__(self):

"""Returns the number of items in self."""

return self.size

def __str__(self):

"""Returns the string representation of self."""

return "{" + ", ".join(map(str, self)) + "}"

def __iter__(self):

"""Supports iteration over a view of self."""

cursor = 0

while cursor < len(self):

yield self.items[cursor]

cursor += 1

def __add__(self, other):

"""Returns a new bag containing the contents

of self and other."""

result = ArrayBag(self)

for item in other:

result.add(item)

return result

def __eq__(self, other):

"""Returns True if self equals other,

or False otherwise."""

if self is other: return True

if type(self) != type(other) or \

len(self) != len(other):

return False

for item in self:

if self.count(item) != other.count(item):

return False

return True

def count(self, item):

"""Returns the number of instances of item in self."""

total = 0

for nextItem in self:

if nextItem == item:

total += 1

return total

# Mutator methods

def clear(self):

"""Makes self become empty."""

self.size = 0

self.items = Array(ArrayBag.DEFAULT_CAPACITY)

In Summery, there are two exercises, 5-3 and 5-4. 5-3 solution can be used in 5-4 were it says # Reuse your solution from Programming Exercise 5.3 as your starter file. Both 5-3 and 5-4 have testbag.py file to test solutions only. i have included code for both fo you to see. Thank you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!