Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT.
RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods.
Below is the Bag ADT starter code and the Dynamic Array program. Basic testing below shows how the code might be used.
Bag Methods to implement:
add (self, value: object) -> None: This method adds a new element to the bag.remove()
remove (self, value: object) -> bool: This method removes any one element from the bag that matches the provided “value” object. Method returns True if some object was actually removed from the bag. Otherwise it returns False.
count (self, value: object) -> int: This method counts the number of elements in the bag that match the provided “value” object.
clear (self) -> None: This method clears the content of the bag.
size (self) -> int: This method returns the number of elements currently in the bag.
equal (self, second_bag: object) -> bool: This method compares the content of the bag with the content of the second bag provided by the user. Method returns True if the bags are equal (have the same number of elements and contain the same elements without regards to the order of elements). Otherwise it returns False. Empty bag is only considered equal to another empty bag. This method should not change the contents of either bag.
#Dynamic Array Code
class DynamicArrayException(Exception):
"""
Custom exception class to be used by Dynamic Array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
pass
class DynamicArray:
def __init__(self, start_array=None):
"""
Initialize new dynamic array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.size = 0
self.capacity = 4
self.data = [None] * self.capacity
# populate dynamic array with initial values (if provided)
# before using this feature, implement append() method
if start_array is not None:
for value in start_array:
self.append(value)
def __str__(self) -> str:
"""
Return content of dynamic array in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = "DYN_ARR Size/Cap: "
out += str(self.size) + "/"+ str(self.capacity)
out += " " + str(self.data[:self.size])
return out
def resize(self, new_capacity: int) -> None:
if (new_capacity == 0) or (new_capacity < self.size):
return
new_data = [None] * new_capacity
for i in range(self.size):
new_data[i] = self.data[i]
self.capacity = new_capacity
self.data = new_data
return
def append(self, value: object) -> None:
if self.is_full():
self.resize(self.capacity * 2)
self.data[self.size] = value
self.size += 1
return
def insert_at_index(self, index: int, value: object) -> None:
if (index < 0) or (index > self.size):
raise DynamicArrayException
if self.is_full():
self.resize(self.size * 2)
for current_index in range(self.size, index, -1):
self.data[current_index] = self.data[current_index - 1]
self.data[index] = value
self.size += 1
return
def get_at_index(self, index: int) -> object:
if (index < 0) or (index > self.size - 1):
raise DynamicArrayException
return self.data[index]
def remove_at_index(self, index: int) -> None:
if (index < 0) or (index > self.size - 1):
raise DynamicArrayException
if (self.size < (0.25 * self.capacity)) and self.capacity > 10:
if self.size < 5:
self.resize(10)
else:
self.resize(self.size * 2)
for i in range(index, self.size):
if i == (self.size - 1):
self.data[i] = None
else:
self.data[i] = self.data[i + 1]
self.size -= 1
return
def is_empty(self) -> bool:
return self.size == 0
def length(self) -> int:
return self.size
def slice(self, start_index: int, quantity: int) -> object:
if (start_index < 0) or (start_index > self.size - 1):
raise DynamicArrayException
if (quantity < 0) or (quantity > (self.size - start_index)):
raise DynamicArrayException
new_array = DynamicArray()
for i in range(start_index, (start_index + quantity)):
new_array.append(self.data[i])
return new_array
def reverse(self) -> None:
if self.size <= 1:
return
front_index = 0
back_index = self.size - 1
while front_index < (self.size / 2):
front_val = self.get_at_index(front_index)
back_val = self.get_at_index(back_index)
self.remove_at_index(front_index)
self.insert_at_index(front_index, back_val)
self.remove_at_index(back_index)
if back_index == self.size:
self.append(front_val)
else:
self.insert_at_index(back_index, front_val)
front_index += 1
back_index -= 1
return
def sort(self) -> None:
if self.size <= 1:
return
current_index = 1
while current_index < self.size:
comparison_index = current_index - 1
sorting = True
while sorting:
current_val = self.get_at_index(current_index)
if current_val < self.get_at_index(comparison_index):
if comparison_index == 0:
self.remove_at_index(current_index)
self.insert_at_index(0, current_val)
sorting = False
else:
comparison_index -= 1
else:
self.remove_at_index(current_index)
self.insert_at_index(comparison_index + 1, current_val)
sorting = False
current_index += 1
return
def merge(self, another_list: object) -> None:
# Iterate the given list values and append
for i in range(another_list.length()):
self.append(another_list.get_at_index(i))
return
def is_full(self) -> bool:
if self.size == self.capacity:
return True
return False
#Bag Starter Code
from dynamic_array import *
class Bag:
def __init__(self, start_bag=None):
"""
Init new bag based on Dynamic Array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.da = DynamicArray()
# populate bag with initial values (if provided)
# before using this feature, implement add() method
if start_bag is not None:
for value in start_bag:
self.add(value)
def __str__(self) -> str:
"""
Return content of bag in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = "BAG: " + str(self.da.size) + " elements. "
out += str(self.da.data[:self.da.size])
return out
def add(self, value: object) -> None:
"""
TODO: Write this implementation
"""
return
def remove(self, value: object) -> bool:
"""
TODO: Write this implementation
"""
return False
def count(self, value: object) -> int:
"""
TODO: Write this implementation
"""
return 0
def clear(self) -> None:
"""
TODO: Write this implementation
"""
return
def size(self) -> int:
"""
TODO: Write this implementation
"""
return 0
def equal(self, second_bag: object) -> bool:
"""
TODO: Write this implementation
"""
return False
##Basic Testing
if __name__ == "__main__":
pass
##Add Example Test:
bag = Bag()
print(bag)
values = [10, 20, 30, 10, 20, 30]
for value in values:
bag.add(value)
print(bag)
#Output:
#BAG: 0 elements. []
#BAG: 6 elements. [10, 20, 30, 10, 20, 30]
#########
##Remove Example Test:
bag = Bag([1, 2, 3, 1, 2, 3, 1, 2, 3])
print(bag)
print(bag.remove(7), bag)
print(bag.remove(3), bag)
print(bag.remove(3), bag)
print(bag.remove(3), bag)
print(bag.remove(3), bag)
#Output:
#BAG: 9 elements. [1, 2, 3, 1, 2, 3, 1, 2, 3]
#False BAG: 9 elements. [1, 2, 3, 1, 2, 3, 1, 2, 3]
#True BAG: 8 elements. [1, 2, 1, 2, 3, 1, 2, 3]
#True BAG: 7 elements. [1, 2, 1, 2, 1, 2, 3]
#True BAG: 6 elements. [1, 2, 1, 2, 1, 2]
#False BAG: 6 elements. [1, 2, 1, 2, 1, 2]
#########
#Count Example Testing:
bag = Bag([1, 2, 3, 1, 2, 2])
print(bag, bag.count(1), bag.count(2), bag.count(3), bag.count(4))
#Output:
#BAG: 6 elements. [1, 2, 3, 1, 2, 2] 2 3 1 0
#########
#Clear Example Testing:
bag = Bag([1, 2, 3, 1, 2, 3])
print(bag)
bag.clear()
print(bag)
#Output:
#BAG: 6 elements. [1, 2, 3, 1, 2, 3]
#BAG: 0 elements. []
#########
#Size Example Testing:
bag = Bag([10, 20, 30, 40])
print(bag.size(), bag.remove(30), bag.size())
bag.clear()
print(bag.size())
#Output:
#4 True 3
#0
#########
#Equal Example Testing:
bag1 = Bag([1, 2, 3, 4, 5, 6])
bag2 = Bag([6, 5, 4, 3, 2, 1])
bag3 = Bag([1, 2, 3, 4, 5])
bag_empty = Bag()
print(bag1, bag2, bag3, bag_empty, sep="\n")
print(bag1.equal(bag2), bag2.equal(bag1))
print(bag1.equal(bag3), bag3.equal(bag1))
print(bag2.equal(bag3), bag3.equal(bag2))
print(bag1.equal(bag_empty), bag_empty.equal(bag1))
print(bag_empty.equal(bag_empty))
print(bag1, bag2, bag3, bag_empty, sep="\n")
#Output:
#BAG: 6 elements. [1, 2, 3, 4, 5, 6]
#BAG: 6 elements. [6, 5, 4, 3, 2, 1]
#BAG: 5 elements. [1, 2, 3, 4, 5]
#BAG: 0 elements. []
#True True
#False False
#False False
#False False
#True
#BAG: 6 elements. [1, 2, 3, 4, 5, 6]
#BAG: 6 elements. [6, 5, 4, 3, 2, 1]
#BAG: 5 elements. [1, 2, 3, 4, 5]
#BAG: 0 elements. []
In: Computer Science
The following production budget for the four quarters of 2019:
|
Quarter 1 |
Quarter 2 |
Quarter 3 |
Quarter 4 |
|
|
Units |
3,000 |
4,000 |
5,000 |
8,000 |
Each units requires 4 kg of raw materials costing $6 per kilogram. On December 31, 2018, the ending inventory of raw materials was 3,000 kg. Management wants to have a raw materials inventory at the beginning of each quarter equal to 25% of the current quarter's production requirements in units. The production budget for the first quarter of 2020 will be 10,000 units.
Use the information provided to answer the following
questions:
a) What is the BEGINNING raw materials inventory
in kilograms of material for each quarter?
Quarter 1: Answer kilograms
Quarter 2: Answer kilograms
Quarter 3: Answer kilograms
Quarter 4: Answer kilograms
b) What is the ENDING raw materials inventory in kilograms of material for each quarter?
Quarter 1: Answer kilograms
Quarter 2: Answer kilograms
Quarter 3: Answer kilograms
Quarter 4: Answer kilograms
Prepare direct materials purchases budget for Quarter 2:
c) What are the total kilograms of raw material that are needed for
production in Quarter 4?
Answer kilograms
d) What are the total kilograms of raw material that need to be
purchased in Quarter 2?
Answer kilograms
e) What is the total cost of the raw materials purchased in
Quarter 3?
$ Answer
In: Accounting
Which of the following types of taxes are deductible in 2018? Which are deductible FOR and which FROM AGI?
1. Property tax on real estate
2. Real property taxes on residence
3. Interest on home mortgage
4. Interest paid on a personal residence
5. interest paid on a note to the bank 3/4 of which was used to buy state and municipal bonds, the rest 1/4 to buy stock
In: Accounting
Consider a binomial experiment with n=13 and p=0.3
a. Compute f(0) (to 4 decimals).
b. Compute f(8) (to 4 decimals).
c. Compute P(x<=2) (to 4 decimals).
d. Compute P(x>=4) (to 4 decimals).
e. Compute E(x) (to 1 decimal).
f. Compute Var(x) and ó.
In: Statistics and Probability
A movie website wants to know which movie was the best among the three parts of The Lord of The Rings and surveyed a simple random sample of 40 adults. The results are presented according to 1 = first part, 2 = second part, and 3 =third part.
2 2 3 1 1 3 3 2 3 1 1 3 3 2 2 3 1 3 3 1 2 2 1 3 1 3 3 2 1 1 2 3 2 3 3 1 1 3 2 3'
Let ? = the proportion of adults who prefer the third one.
(1) Find the sample proportion ?̂
(2) Calculate the z-statistic for the test ?0:? ≤ 0.4,??:? > 0.4.
(3) Calculate the p-value
(4) What is the conclusion of the hypothesis test? Use ? = 0.05
do step by step include original formula.
In: Statistics and Probability
Project A requires an original investment of $62,000. The project will yield cash flows of $18,600 per year for 4 years. Project B has a computed net present value of $3,690 over a 4-year life. Project A could be sold at the end of 4 years for a price of $14,900.
Following is a table for the present value of $1 at compound interest:
| Year | 6% | 10% | 12% | |||
| 1 | 0.943 | 0.909 | 0.893 | |||
| 2 | 0.890 | 0.826 | 0.797 | |||
| 3 | 0.840 | 0.751 | 0.712 | |||
| 4 | 0.792 | 0.683 | 0.636 | |||
| 5 | 0.747 | 0.621 | 0.567 |
Following is a table for the present value of an annuity of $1 at compound interest:
| Year | 6% | 10% | 12% | |||
| 1 | 0.943 | 0.909 | 0.893 | |||
| 2 | 1.833 | 1.736 | 1.690 | |||
| 3 | 2.673 | 2.487 | 2.402 | |||
| 4 | 3.465 | 3.170 | 3.037 | |||
| 5 | 4.212 | 3.791 | 3.605 |
Use the tables above.
a. Determine the net present value of Project A
over a 4-year life with salvage value assuming a minimum rate of
return of 12%. Round your answer to two decimal
places.
$
b. Which project provides the greatest net present value?
In: Finance
a)Fill in the network diagram below
|
Task |
Time (wk) |
Predecessor |
Min Time with Crashing |
Crashing cost/wk |
ES |
EF |
LS |
LF |
Slack |
Critical (Y/N) |
Free Slack |
|
A |
5 |
- |
4 |
$50 |
|||||||
|
B |
3 |
- |
2 |
$10 |
|||||||
|
C |
6 |
A |
3 |
$2,500 |
|||||||
|
D |
3 |
A |
1 |
$10 |
|||||||
|
E |
8 |
C,B |
7 |
$200 |
|||||||
|
F |
3 |
D,E |
1 |
$1,000 |
|||||||
|
G |
4 |
F |
4 |
- |
|||||||
|
Task |
Time (wk) |
Predecessor |
Min Time with Crashing |
Crashing cost/wk |
ES |
EF |
LS |
LF |
Slack |
Critical (Y/N) |
Free Slack |
|
A |
5 |
- |
4 |
$50 |
|||||||
|
B |
3 |
- |
2 |
$10 |
|||||||
|
C |
6 |
A |
3 |
$2,500 |
|||||||
|
D |
3 |
A |
1 |
$10 |
|||||||
|
E |
8 |
C,B |
7 |
$200 |
|||||||
|
F |
3 |
D,E |
1 |
$1,000 |
|||||||
|
G |
4 |
F |
4 |
- |
b-Clearly List your critical path
c-What is the shortest time need to complete the project (without expediting)?
d-To expedite the project by two weeks, which activities will you crash? Crashing cost?
In: Operations Management
Find the absolute maximum value and the absolute minimum value of the function
f(x,y) = (1+x^2)(1−y^2)
on the disk D = {(x,y) | x2+y2⩽1}?
In: Math
Assume a Cobb-douglas utility function of 2 goods x and y given by U = x 0.5y 0.5 and an initial income I of 100. Let initial price be px = 4 and py = 1. Now vary the price of x from 1 to 7 in steps of 1. So you have 7 prices for x. px = {1, 2, 3, 4, 5, 6, 7} For each of these px, py REMAINS the SAME at 1. In the excel sheet fill columns F (demand for x), G(demand for y), H(utility from x, y), K (marshallian demand x), L (hicksian demand x, xh), M (Income for hicksian demand), N (demand for y under hicksian case yh) and O (utility from xh, yh) For x
(a) compute and plot the Marshallian Demand
(b) compute and plot the Hicksian Demand assuming the base price of x, px = 4. or ensuring that utility is computed and kept constant when initial prices were px = 4, py = 1 and Income I = 100 Fill
In: Economics
Classify each of the following reactions as one of the four possible types summarized in Table 19.3: (i) spontanous at all temperatures; (ii) not spontaneous at any temperature; (iii) spontaneous at low T but not spontaneous at high T ; (iv) spontaneous at high T but not spontaneous at low T . (a) N 2 1 g 2 + 3 F 2 1 g 2 ¡ 2 NF 3 1 g 2 ∆ H ° = - 249 kJ; ∆ S ° = - 278 J > K
(b) N 2 1 g 2 + 3 Cl 2 1 g 2 ¡ 2 NCl 3 1 g 2 ∆ H ° = 460 kJ; ∆ S ° = - 275 J > K
(c) N 2 F 4 1 g 2 ¡ 2 NF 2 1 g 2 ∆ H ° = 85 kJ; ∆ S ° = 198 J > K
In: Chemistry