Question: i need to pass these two test cases: Search Infix: Trees, Count: 1 5 and Search Infix: WIII, Count: 2 5 Tested searchBookByInfix ( Trees

i need to pass these two test cases: Search Infix: "Trees", Count: 15 and Search Infix: "WIII", Count: 25
Tested searchBookByInfix(Trees,15).Expected 15
Tested searchBookByInfix(WIII,25).Expected 1
import Book
import ArrayList
import ArrayQueue
import RandomQueue
import DLList
import SLLQueue
import ChainedHashTable
import BinarySearchTree
import BinaryHeap
import AdjacencyList
import time
class BookStore:
'''
BookStore: It simulates a book system such as Amazon. It allows searching,
removing and adding in a shopping cart.
'''
def __init__(self):
self.bookCatalog = None
self.shoppingCart = ArrayQueue.ArrayQueue()
def loadCatalog(self, fileName: str):
'''
loadCatalog: Read the file filenName and creates the array list with all books.
book records are separated by ^. The order is key,
title, group, rank (number of copies sold) and similar books
'''
self.bookCatalog = ArrayList.ArrayList()
with open(fileName, encoding="utf8") as f:
# The following line is the time that the computation starts
start_time = time.time()
for line in f:
(key, title, group, rank, similar)= line.split("^")
s = Book.Book(key, title, group, rank, similar)
self.bookCatalog.append(s)
# The following line is used to calculate the total time
# of execution
elapsed_time = time.time()- start_time
print(f"Loading {self.bookCatalog.size()} books in {elapsed_time} seconds")
def setRandomShoppingCart(self):
q = self.shoppingCart
start_time = time.time()
self.shoppingCart = RandomQueue.RandomQueue()
while q.size()>0:
self.shoppingCart.add(q.remove())
elapsed_time = time.time()- start_time
print(f"Setting radomShoppingCart in {elapsed_time} seconds")
def setShoppingCart(self):
q = self.shoppingCart
start_time = time.time()
self.shoppingCart = ArrayQueue.ArrayQueue()
while q.size()>0:
self.shoppingCart.add(q.remove())
elapsed_time = time.time()- start_time
print(f"Setting radomShoppingCart in {elapsed_time} seconds")
def removeFromCatalog(self, i: int):
'''
removeFromCatalog: Remove from the bookCatalog the book with the index i
input:
i: positive integer
'''
# The following line is the time that the computation starts
start_time = time.time()
self.bookCatalog.remove(i)
# The following line is used to calculate the total time
# of execution
elapsed_time = time.time()- start_time
print(f"Remove book {i} from books in {elapsed_time} seconds")
def addBookByIndex(self, i: int):
'''
addBookByIndex: Inserts into the playlist the song of the list at index i
input:
i: positive integer
'''
# Validating the index. Otherwise it crashes
if i >=0 and i < self.bookCatalog.size():
start_time = time.time()
s = self.bookCatalog.get(i)
self.shoppingCart.add(s)
elapsed_time = time.time()- start_time
print(f"Added to shopping cart {s}
{elapsed_time} seconds")
def searchBookByInfix(self, infix: str):
'''
searchBookByInfix: Search all the books that contains infix
input:
infix: A string
'''
start_time = time.time()
# todo
elapsed_time = time.time()- start_time
print(f"searchBookByInfix Completed in {elapsed_time} seconds")
def removeFromShoppingCart(self):
'''
removeFromShoppingCart: remove one book from the shoppung cart
'''
start_time = time.time()
if self.shoppingCart.size()>0:
u = self.shoppingCart.remove()
elapsed_time = time.time()- start_time
print(f"removeFromShoppingCart {u} Completed in {elapsed_time} seconds")

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 Programming Questions!