Hello, so I wrote a program that manages a file with information…

  

Hello, so I wrote a program that manages a file with information… Hello, so I wrote a program that manages a file with information regarding stocks and sorts the file and searches for stocks within the file based on the symbol or stock name. The program works fine, I am now wanting to do these tests based on the program. I am having trouble wondering how I would find the stock with the longest company name and the biggest percentage difference? Would I just compare the first value of the prices list and the last? Any help would be greatly appreciated. Code and example stocks below. TImage transcription text6. 10. 11. Implement a search function searchBST{self, symbol: str) based on the symbol attribute. It retums the details of the stock as described in _str_() function or a”Stock not found” message when there is no match. Perform a linear search based on symbol in the sorted database for the 100 stocks. You can use a randomnumber generator to arbitrarily select 100 stock symbols. Record the average time spent on the linear search. Perform a search of the same 100 stocks in the BST…. Show more… Show moreImage 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 Corporatio… Show more… Show more “””UMass ECE 241 – Advanced ProgrammingProject #1 Fall 2021project1.py – Sorting and Searching”””import matplotlib.pyplot as pltimport randomimport time”””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 = attributes[0] # company name self.symbol = attributes[1] # symbol self.val = float(attributes[2]) # market value self.prices = attributes[3:] # list of prices “”” 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): return ‘Name: {}; Symbol: {}; Val: {}; Price: {}’.format(self.sname, self.symbol, self.val, self.prices[-1]) # format to string”””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) # skip first line for line in file: stock = Stock(line) # each line represents stock, add to size 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): for stock in self.stockList: if attribute == “name” and stock.sname == query: # if name matches, return string return str(stock) elif attribute == “symbol” and stock.symbol == query: # if symbol matches, return string return str(stock) else: 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 stocks # 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,): # 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 stock else: return str(found)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 stock 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 = 'XOM' testName = 'Exxon Mobil Corporation' print("n-------load dataset-------") stockLib.loadData("stock_database.csv") print(stockLib.size) print("n-------linear search-------") 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-------") start = time.perf_counter_ns() stockLib.buildBST() end = time.perf_counter_ns() print(end-start) print("n---------search BST---------") print(stockLib.searchBST(testSymbol)) times_linear = [] times_bst = [] for i in range(100): # picks random 100 songs from library random_stock = random.choice(stockLib.stockList) random_symbol = random_stock.symbol start = time.perf_counter() stockLib.linearSearch(random_symbol, "symbol") # searches random song using Linearsearch end = time.perf_counter() times_linear.append(end - start) # calculates total time start = time.perf_counter() stockLib.searchBST(random_symbol) # searches using BST end = time.perf_counter() times_bst.append(end - start) # calculates time. average_time_linear = sum(times_linear) / len( times_linear ) # calculates the average time for BST and Linear average_time_bst = sum(times_bst) / len( times_bst ) # and returns to user to compare. print( "The average time for the linear search is: {} seconds".format( average_time_linear ) ) print("The average time for the BST search is: {} seconds".format(average_time_bst)) print()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.22Citigroup Inc.|C|231977.70|60.14|61.70|65.25|66.02|65.37|66.44|66.67|66.86|69.01|64.23|63.69|63.25|61.87|61.33|60.56|60.91|59.40|60.34|57.99Procter & Gamble Company (The)|PG|194814.00|137.82|138.70|140.16|138.85|138.79|137.85|137.05|137.26|135.80|134.78|133.60|131.93|131.01|130.00|132.24|133.09|128.38|130.36|128.21Wal-Mart Stores Inc.|WMT|191978.50|146.53|145.75|146.66|146.65|146.63|147.29|148.97|147.45|146.97|144.64|143.39|145.51|144.85|146.33|146.20|147.51|143.84|143.75|140.49Pfizer Inc.|PFE|189297.40|36.81|37.19|36.87|37.06|37.13|37.77|37.18|36.86|36.75|36.70|36.73|36.50|36.48|36.55|37.28|37.31|36.24|35.86|35.90HSBC Holdings plc.|HBC|188804.60|16750.00|16650.00|16750.00|17150.00|17000.00|16700.00|17300.00|17200.00|17000.00|17100.00|15800.00|15000.00|16050.00|16700.00|17850.00|18900.00|18900.00|17600.00|16900.00Toyota Motor Corporation|TM|187196.40|153.29|153.40|153.53|152.67|152.85|153.39|151.75|150.65|151.34|148.41|148.13|149.98|151.34|147.95|146.72|145.98|142.64|143.30|140.52JOHNSON & JOHNSON|JNJ|179515.80|156.50|158.34|159.83|160.37|160.04|159.37|158.13|157.89|160.65|160.30|162.78|162.38|161.73|163.55|165.98|170.48|167.88|169.16|163.13Bank of America Corporation|BAC|176906.20|30.03|30.26|32.15|32.86|32.53|33.07|33.66|33.46|33.99|33.01|32.77|32.34|31.77|31.55|31.16|30.94|29.83|30.32|29.65American International Group Inc.|AIG|169908.50|37.13|37.75|40.21|40.89|40.62|40.28|41.00|40.72|41.30|41.35|42.25|42.08|41.05|40.08|39.58|39.11|37.60|38.48|37.44Novartis AG|NVS|151092.70|94.37|94.61|93.51|93.40|94.04|94.39|93.85|93.31|94.62|95.63|96.69|96.92|96.88|96.90|98.47|95.94|92.90|92.84|90.47 Computer Science Engineering & Technology Python Programming ECE 241 Share QuestionEmailCopy link Comments (0)

Don't use plagiarized sources. Get Your Custom Essay on
Hello, so I wrote a program that manages a file with information…
Just from $13/Page
Order Essay
  

Leave a Reply

Your email address will not be published.

Related Post

y=2r -41+4 Vertex Opening of the groph Arn of Symmetry Domain Range X- intercept Y – intercept B. Illustrate the graphs of the following quodrafic…y=2r -41+4 Vertex Opening of the groph Arn of Symmetry Domain Range X- intercept Y – intercept B. Illustrate the graphs of the following quodrafic…

  ThankyouuuuuImage transcription text4.y=2r -41+4 Vertex Opening of the groph Arn of Symmetry Domain Range X- intercept Y – intercept B.Illustrate the graphs of the following quodrafic functions then analyze the

READ MOREREAD MORE
Open chat
💬 Need help?
Hello 👋
Can we help you?