Hello, I am currently debugging my python program that manages data…

  

Hello, I am currently debugging my python program that manages data… Hello,I am currently debugging my python program that manages data of a file that includes information about stocks and uses various searches and sort algorithms to measure efficiency. After testing all of my methods, its seems as though my linearsearch function and binary search tree functions are having issues. I know the linearsearch is wrong because it cannot locate the stocks even though they are in the file. The error for my BST tells me that I need to have the positional argument sname, but I am still not getting it to work. Code and file of list of stocks below. Each line in file represents a new stock as seen in the screenshot.”””Stock class for stock objects”””class Stock: “”” Constructor to initialize the stock object Each stock is managed using a Stock class. In the Stock class, should have variables for the different attributes of the stock.1. A string sname for the stock name.2. A string symbol for the stock symbol.3. A float val for market value.4. A list prices for a list that contains the close market price for one month. “”” def __init__(self,stockInfo,sname=””,symbol=””,val=0.0,prices=[]): attributes = stockInfo.split(“|”) # attributes split by a list self.sname = sname self.symbol = symbol self.val = val self.prices = prices self.sname = attributes[0] self.symbol=attributes[1] self.val = float(attributes[2]) self.prices = attributes[3:] “”” return the stock information as a string, including name, symbol, market value, and the price on the last day (2021-02-01). For example, the string of the first stock should be returned as: “name: Exxon Mobil Corporation; symbol: XOM; val: 384845.80; price:44.84”. “”” def __str__(self,sname): return ‘Name: {}; Symbol: {}; Val: {}; Price: {}’.format(self.sname, self.symbol, self.val, self.prices[-1]) pass”””StockLibrary class to mange stock objects”””class StockLibrary: “”” Constructor to initialize the StockLibrary “”” def __init__(self): self.stockList = list() self.size = 0 self.isSorted = False self.stockBST = None pass “”” The loadData method takes the file name of the input dataset, and stores the data of stocks into the library. Make sure the order of the stocks is the same as the one in the input file. “”” def loadData(self, filename: str): file = open(filename, “r”) next(file) for line in file: stock = Stock(line) # skip first line self.stockList.append(stock) self.size = self.size + 1 pass “”” The linearSearch method searches the stocks based on sname or symbol. It takes two arguments as the string for search and an attribute field that we want to search (“name” or “symbol”). It returns the details of the stock as described in __str__() function or a “Stock not found” message when there is no match. “”” def linearSearch(self, query: str, attribute: str): found = 0 for stock in self.stockList: if attribute == “Name” and stock.sname == query: found = found + 1 Stock.__str__(stock) elif attribute == “Symbol” and stock.symbol == query: found = found+1 Stock.__str__(stock) else: pass if found == 0: return “Stock not Found” “”” Sort the stockList using QuickSort algorithm based on the stock symbol. The sorted array should be stored in the same stockList. Remember to change the isSorted variable after sorted “”” def quickSort(self): self.qsort(self.stockList,0, len(self.stockList) – 1) self.isSorted = True def qsort(self, array, low, high): # For the quickSort, if low < high indices, it means there's still # part of the array to be sorted if low < high: # we use the method 'partition' index = self.partition(array, low, high) # Then, sort values before and after the index for the pivot self.qsort(array, low, index - 1) self.qsort(array, index + 1, high) def partition(self, array, low, high): # start finding the pivot pivot = array[high] # declare the index of the pivot to be found as low-1 to start index = low - 1 # loop through the whole array to place all the smaller values for i in range(low, high, 1): # If the value at i index has a 'smaller title', we place it on the # next index if array[i].symbol <= pivot.symbol: #increment the next index value index = index + 1 # swap between the value at index and the valye at i temp = array[index] array[index] = array[i] array[i] = temp # return it temp = array[index + 1] array[index + 1] = pivot array[high] = temp return index + 1 """ build a balanced BST of the stocks based on the symbol. Store the root of the BST as attribute bst, which is a TreeNode type. """ def buildBST(self): # loop through the array of Songs # and we add them to the Tree self.stockBST = BST() for stock in self.stockList: self.stockBST.add(stock) """ Search a stock based on the symbol attribute. It returns the details of the stock as described in __str__() function or a "Stock not found" message when there is no match. """ def searchBST(self, query, current='dnode',): # call the method binarySearch # from the tree. found = self.stockBST.binarySearch(query) # If it returns None, return None if found is None: return "Stock not found" # If not, return the toString from the song else: return Stock.__str__(query)class TreeNode: def __init__(self,stock): self.stock = stock self.value = stock.symbol self.left = None self.right = None self.height = 1class BST: def __init__(self): self.root = None def add(self,key): if self.root is None: self.root = TreeNode(key) else: self.root = self.insert(self.root,key) def insert(self, root, key): # a normal insert for BST into the tree if root is None: return TreeNode(key) if key.symbol < root.value: root.left = self.insert(root.left, key) else: root.right = self.insert(root.right, key) # update the height of the previous root using its childs heights root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) # ask for the balance using the getBalance method balance = self.getBalance(root) # 4 cases in order to balance the tree # If balance is positive and key is less than left, we right rotate our tree if balance > 1 and key.symbol < root.left.value: return self.rightRotate(root) # If balance is negative and our key is greater than right, we left rotate our tree if balance < -1 and key.symbol > root.right.value: return self.leftRotate(root) # If balance is positive and key is greater than left, we first left rotate # the left of root and then we right rotate the whole tree if balance > 1 and key.symbol > root.left.value: root.left = self.leftRotate(root.left) return self.rightRotate(root) # If balance is negative and key is less than right, we first right rotate # the right of root and then we left rotate the whole tree if balance < -1 and key.symbol < root.right.value: root.right = self.rightRotate(root.right) return self.leftRotate(root) # if there was no need to balance the tree, we just return the old root return root def leftRotate(self, node): # get the one at the right of the node which would be the new root # for the subtree and also the left of the new root root = node.right temp = root.left # rotate root.left = node node.right = temp # Finally, we update the heights and we return the new root node.height = 1 + max(self.getHeight(node.left), self.getHeight(node.right)) root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) return root """ Method that right rotate the subtree for the given node """ def rightRotate(self, node): # First get the one at the left of the node which would be the new root # for the subtree and also the right of the new root root = node.left temp = root.right # rotate root.right = node node.left = temp # update the heights and we return the new root node.height = 1 + max(self.getHeight(node.left), self.getHeight(node.right)) root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right)) return root """ Function to get the balance factor for the given node """ def getBalance(self, node): # If the node is None, then 0 if node is None: return 0 # Else, check for the heights of childs to get the respective balance else: return self.getHeight(node.left) - self.getHeight(node.right) """ Function to get the height of a given node. If Node, return 0. If not, we return its height """ def getHeight(self, root): if not root: return 0 return root.height """ Recursive function to do a binary search on the tree for the given value Returns the song object on it if found, None if not """ def binarySearch(self, val, node=0): # If node = 0, this is the first call, so call again using the root as # the starting node if node == 0: return self.binarySearch(val, self.root) # first look at the current node. # If the node is None,return false if node is None: return None # If val is the one we're looking for, we return true if node.value == val: return node.stock # If not, if value is less, call again for its left node elif val < node.value: return self.binarySearch(val, node.left) # Else, if greater, call again for its right node else: return self.binarySearch(val, node.right)# WRITE YOUR OWN TEST UNDER THIS IF YOU NEEDif __name__ == '__main__': stockLib = StockLibrary() testSymbol = 'GE' testName = 'General Electric Company' print("n-------load dataset-------") stockLib.loadData("stock_database.csv") print(stockLib.size) print("n-------linear search-------") # cannot find symbol or name print(stockLib.linearSearch(testSymbol, "symbol")) print(stockLib.linearSearch(testName, "name")) print("n-------quick sort-------") print(stockLib.isSorted) stockLib.quickSort() print(stockLib.isSorted) print("n-------build BST-------") stockLib.buildBST() print("n---------search BST---------") print(stockLib.searchBST(testSymbol)) #Name|Symbol|Market Value (millions)|2021-01-04|2021-01-05|2021-01-06|2021-01-07|2021-01-08|2021-01-11|2021-01-12|2021-01-13|2021-01-14|2021-01-15|2021-01-19|2021-01-20|2021-01-21|2021-01-22|2021-01-25|2021-01-26|2021-01-27|2021-01-28|2021-01-29Exxon Mobil Corporation|XOM|384845.80|41.50|43.50|44.61|44.96|45.46|46.84|47.88|48.42|50.31|47.89|48.84|49.53|48.11|47.43|46.90|45.87|45.35|46.06|44.84General Electric Company|GE|346042.10|83.76|86.16|90.88|90.16|90.72|91.60|94.24|92.56|93.28|90.64|91.44|91.12|88.56|88.88|87.92|90.32|91.04|88.72|85.44Microsoft Corporation|MSFT|299647.60|217.69|217.90|212.25|218.29|219.62|217.49|214.93|216.34|213.02|212.65|216.44|224.34|224.97|225.95|229.53|232.33|232.90|238.93|231.96BP p.l.c.|BP|248469.70|20.83|22.38|23.41|23.91|24.40|24.38|25.01|24.78|25.47|24.26|24.65|24.81|24.02|23.87|23.07|23.03|22.73|22.86|22.22Image transcription textName | Symbol | Market Value (millions) |2021-01-04|2021-01-05 |2021-01-06 |2021-01-07 |2021-01-08|2021-01-11|2021-01-12 |2021-01-13 |2021-01-14|2 Exxon Mobil Corporation | XOM | 384845 . 80 | 41.50 |43. 50 | 44. 61 | 44. 96 | 45. 46 | 46. 84 | 47 . 88 | 48. 42 |50. 31 | 47. 89 | 48. 84 | 49. 53 | 48. 11 | 47. 43 | 46. 90 | 45.87 | 45 General Electric Company | GE | 346042. 10 | 83. 76 | 86. 16 | 90. 88 | 90. 16 | 90. 72 |91. 60 |94.24 |92. 56 | 93. 28 | 90. 64 | 91. 44 | 91. 12 | 88. 56 | 88. 88 |87. 92 |90.32 |91 Microsoft Corporation | MSFT | 299647 . 60 |217. 69 |217. 90 |212. 25 | 218. 29 |219. 62 |217. 49 |214. 93 |216. 3... Show more... Show more Computer Science Engineering & Technology Python Programming ECE 241 Share QuestionEmailCopy link Comments (0)

Don't use plagiarized sources. Get Your Custom Essay on
Hello, I am currently debugging my python program that manages data…
Just from $13/Page
Order Essay
  

Leave a Reply

Your email address will not be published.

Related Post

Open chat
💬 Need help?
Hello 👋
Can we help you?