ANSWER IN PYTHON EVERY QUESTION:.....CODE IN PYTHON.....CODE IN PYTHON 1. Determine the running time of the == operation

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

ANSWER IN PYTHON EVERY QUESTION:.....CODE IN PYTHON.....CODE IN PYTHON 1. Determine the running time of the == operation

Post by answerhappygod »

ANSWER IN PYTHON EVERY QUESTION:.....CODE IN PYTHON.....CODE IN
PYTHON
1. Determine the running time of the == operation for the two
bag implementations. Be forewarned that there are several cases to
analyze.
2. Determine the running time of the + operator for the two bag
implementations.
3. Complete the code for the ArrayBag method add, so that the
array is resized when necessary.
4. Complete the code for the ArrayBag method remove, so
that the array is resized when necessary.
5. Add the method clone to the ArrayBag and LinkedBag classes.
This method expects no arguments when called, and returns an exact
copy of the type of bag on which it is called. For example, the
variable bag2 would contain the numbers 2, 3, and 4 at the end of
the following code segment:
bag1 = ArrayBag([2,3,4])
bag2 = bag1.clone()
bag1 == bag2 # Returns True
bag1 is bag2 # Returns False
6. A set is an unordered collection with the same interface as a
bag. However, the items in a set are unique, whereas a bag can
contain duplicate items. Define a new class named ArraySet that
implements an array-based set type. The add method simply ignores
the item if it’s already in the set.
7. Define a new class named LinkedSet that implements a set type
using linked nodes. The add method simply ignores the item if it’s
already in the set.
8. A sorted bag behaves just like a regular bag but allows the
user to visit its items in ascending order with the for loop.
Therefore, the items added to this type of bag must have a
natural ordering and recognize the comparison operators. Some
examples of such items are strings and integers. Define a new class
named ArraySortedBag that supports this capability. Like ArrayBag,
this new class is array based, but its in operation can now run in
logarithmic time. To accomplish this, ArraySortedBag must place new
items into its array in sorted order. The most efficient way to do
this is to modify the add method to insert new items in their
proper places. You also have to include a __contains__ method to
implement the new, more efficient search. Finally, you must change
all references to ArrayBag to be ArraySortedBag. (Another hint:
copy the code from the ArrayBag class to a new file and begin
making your changes from there.)
9. Determine the running time of the add method of
ArraySortedBag.
10. Python’s for loop allows the programmer to add or remove
items in the collection over which the loop is iterating. Some
designers worry that changing the structure of a collection during
iteration might cause program crashes. The remedy is to make the
for loop read-only, by disallowing mutations to the collection
during iteration. You can detect such mutations by keeping a count
of them and determining if this count goes up at any point within
the collection’s __iter__ method. When this happens, you can raise
an exception to prevent the computation from going forward. Add
this mechanism to the ArrayBag class. Include a new instance
variable named modCount, which is set to 0 in the __init__ method.
Each mutator method then increments this variable. Finally, the
__iter__ method includes its own temporary variable named modCount,
which is set initially to the value of the instance variable
self.modCount. Immediately after an item is yielded within the
__iter__ method, you raise an exception if the values of the two
mod counts are not equal. Be sure to test your changes with a
program that commits the relevant offenses.
CODE IN PYTHON
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply