def test_contains(self): """TODO: Modify this test after changing the input data""" tree = DecimalSearchTree() tree.insert('00', 1) assert tree.contains('00') is True assert tree.contains('0') is True assert tree.contains('01') is False
def test_insert(self): """TODO: Modify this test after changing the input data""" tree = DecimalSearchTree() # Insert one item to the tree tree.insert('0', ("hello", 1)) assert tree.size == 1 assert tree.height() == 1 child = tree.root.next[0] assert child is not None assert child.data == ("hello", 1) # Insert a grandchild tree.insert('01', ("hello", 1)) assert child.next[1] is not None assert child.next[1].data == ("hello", 1) assert tree.height() == 2
def test_inserting_lower_price(self): """TODO: Modify this test after changing the input data""" tree = DecimalSearchTree() # Insert one item to the tree tree.insert('00', ("hello", 1)) tree.insert('00', ("hello", 0.3)) child_node = tree.root.next[0] # Change the data since it is larger assert child_node.next[0].data == ("hello", 0.3) tree.insert('01', ("hello", 34)) assert child_node.next[1].data == ("hello", 34) # Doesn't change the data since it is larger tree.insert('01', ("hello", 43)) assert child_node.next[1].data == ("hello", 34)
def test_search(self): tree = DecimalSearchTree() tree.insert('00', ('hello', 1)) assert tree.search('00') == ('hello', 1) assert tree.search('001') is None
class CallRouting: def __init__(self, phone_number_files, carriers): self.carriers = self._format_carriers(carriers) # A dictionary of {'carrier name', file path} self.phone_numbers_paths = [] self.decimal_search_tree = DecimalSearchTree() for file in phone_number_files: self.phone_numbers_paths.append(os.path.join(THIS_FOLDER, file + '.txt')) # A path string of to the phone number file self.dict_of_routes = {} # dictionary of string : double {carrier : [(route number, price)]} self.list_of_numbers = [] # list of strings [phone numbers] def run(self): start_creating_dict = time.time() self._get_routes_from_carriers() self._get_phone_numbers() end = time.time() print("Runtime to create dictionary of carriers and routes, and phone number: " + str(end - start_creating_dict)) start_creating_tree = time.time() self.popuplate_tree() end_tree = time.time() print('Runtime for creating tree: ' + str(end_tree - start_creating_tree)) start_searching = time.time() self.check_prices() end_searching = time.time() print("Runtime for searching phone number: " + str(end_searching - start_searching)) def _get_phone_numbers(self): """Convert the given phone numbers file to a list of phone number [String]""" for path in self.phone_numbers_paths: with open(path) as f: self.list_of_numbers = f.read().splitlines() def _format_carriers(self, carriers): """Format the carrier dictionary value into a proper file path.""" new_dict = {} # Create new dictionary with the proper file path for pair in carriers: new_dict[pair[0]] = os.path.join(THIS_FOLDER, pair[1] + '.txt') return new_dict def _get_routes_from_carriers(self): """Must be call after self._format_carriers is done.""" for key in self.carriers.keys(): with open(self.carriers[key]) as f: file = f.read().splitlines() for line in file: route_number, price = line.split(',') tup = (route_number, float(price)) if key in self.dict_of_routes: self.dict_of_routes[key].append(tup) else: self.dict_of_routes[key] = [tup] def popuplate_tree(self): """Convert the dictionary of routes prices into a tree""" for key in self.dict_of_routes.keys(): list_of_route_prices = self.dict_of_routes[key] for item in list_of_route_prices: number = item[0][1:] data = (key, item[1]) self.decimal_search_tree.insert(number, data) def check_prices(self): """Check the price of the phone numbers in the tree""" result_prices = [] # [(phone number, (carrier name, price))] for number in self.list_of_numbers: search_result = self.decimal_search_tree.get_price(number[1:]) if search_result is None: # signalling that there is no matching prefix for the current number result_prices.append((number, ('None', 0))) # Appending this way to keep everything consistent else: result_prices.append((number, search_result)) # Found the longest matching prefix and got a price return result_prices