def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) if length < 2: return None # This solution does not account for resizing the hashtable if the length is >16. # Refactoring would include handling those edge cases # sets up a hashtable with each weight as the key and the index as the value for i in range(0, length): hash_table_insert(ht, weights[i], i) for i in range(0, length): # finds what the complimentary weight would be for item i complimentary_weight = limit - weights[i] # if complimentary weight exists if hash_table_retrieve(ht, complimentary_weight): # if i is the greater value, returns it as the first item in the tuple if i > hash_table_retrieve(ht, complimentary_weight): return (i, hash_table_retrieve(ht, complimentary_weight)) # else if i is the lower value, returns it as the second item in the tuple else: return (hash_table_retrieve(ht, complimentary_weight), i) # if the key doesn't exist, returns None return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ for ticket in tickets: # hash each ticket such that the starting location is the key key = ticket.source # and the destination is the value value = ticket.destination # store new key and value in hash table hash_table_insert(hashtable, key, value) # print(f'key: {ticket.source}, value: {ticket.destination}') # since the source of the first ticket is none we set the starting destination to place at the start of route route[0] = (hash_table_retrieve(hashtable, "NONE")) # print(route, "routr") # using the length of the tickets list we can find source of the next ticket in our route for i in range(1, length): # the `i`th location in the route can be found by checking the hash table for the `i-1`th location source_of_next_ticket = route[i-1] # and then set the ith location in the route as the next destination using the source retrive route[i] = hash_table_retrieve(hashtable, str(source_of_next_ticket)) # we pop off the none value on the end of our list route.pop() return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(length * 2) # twice as fat, etc., etc. one = None two = None if length == 1: return None for package, weight in enumerate(weights): attempt_retrieval = hash_table_retrieve(ht, weight) if attempt_retrieval == None: hash_table_insert(ht, weight, package) else: return package, attempt_retrieval if weight <= limit: if one == None: one = weight elif two == None: two = weight if one == None and two == None: return None weights = sorted(weights) for i in range(length): starter_weight = weights[i] starter = hash_table_retrieve(ht, starter_weight) attempt_weight = limit - starter_weight attempt = hash_table_retrieve(ht, attempt_weight) if attempt != None: if attempt > starter: return attempt, starter else: return starter, attempt else: pass return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) # 5 route = [None] * length #[None,None,None,None,None,] """ YOUR CODE HERE """ for ticket in tickets: # here we are inserting a hash per the length of the tickets so here for example we have 5 tickets and so we insert the length of the ticket list, source ,and destination # we are starting the sequence so to speak so we can work with changing it around. hash_table_insert(hashtable, ticket.source, ticket.destination) # here if find the location of the first airport which had no previous airport to fly from which will be our head so in the test case it will be ticket 1 PDX or in test2 LAX # so now we have our starting point and it's as simple as linking everything together since every ticket has a final destination location = hash_table_retrieve(hashtable, "NONE") # so we want to loop as long as the length of the list of tickets. for i in range(length): # the list above labled "route" has a list of a bunch of NONE's so we change FIRST index with the current location which is our head route[i] = location # then we retrieve the next location by running "hash_table_retrieve" with the hashtable and the current "location" as the previous airport it arrived from and replacing the current location vairable with the new found destination location = hash_table_retrieve(hashtable, location) # here we print because the READ ME said to print(route) # and now we return it return route
def reconstruct_trip(tickets, length): # Initialize Hashtable/ pass in the length(capacity), and assign to itinerary itinerary = HashTable(length) # Preallocate the route (array) memory by length/capacity route = [None] * length # For each ticket we have... for t in tickets: # Insert the itinerary (ht), source (key), and desination (value) in the hash table hash_table_insert(itinerary, t.source, t.destination) # Retrieve and assign the first ticket -- has destination, but no source (key or initial connecting flight) ticket = hash_table_retrieve(itinerary, "NONE") # While the ticket *does* have a destination on its key... while ticket != "NONE": # For each index in the range of our route array's length... for i in range(len(route)): # Assign each route's destination [value] to ticket, which places it inside the array route[i] = ticket # Call retrieve again on the same ticket's key to find the ticket with a matching source ticket = hash_table_retrieve(itinerary, ticket) # If the ticket doesn't have a destination... if ticket == "NONE": # Place at the end of our route route[ i + 1] # have to increment because we aren't assining a value to ticket # Break out of the loop since work is done break # Return our newly organized route return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) for i in range(len(weights)): hash_table_insert(ht, weights[i], i) diff = [] for i in weights: diff.append(limit-i) ans = [] for i in diff: if hash_table_retrieve(ht, i) != None: ans.append(i) if len(ans) == 2: ans.sort() x = hash_table_retrieve(ht, ans[0]) y = hash_table_retrieve(ht, ans[1]) if x == y: indices = [i for i, x in enumerate(weights) if x == ans[0]] return (indices[1], indices[0]) if x > y: return (x, y) else: return (y, x) else: return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * (length - 1) # loop over tickets and insert in ht for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) # find ticket with none src source_flight = hash_table_retrieve(hashtable, "NONE") current = source_flight # looping over n-1 times to make up for the "NONE" flight source for i in range(length - 1): # used for chaining together flights next_stop = hash_table_retrieve(hashtable, current) # setting the destination to the current 'i' index in the array route[i] = current # updating current to next flight source for next iteration current = next_stop return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ for i in range(length): key = tickets[i].source if key != "NONE": value = tickets[i].destination hash_table_insert(hashtable, key, value) # start node not in destination list. destination_list = [] for i in range(length): destination_list.append( hash_table_retrieve(hashtable, tickets[i].destination)) # start node for i in range(length): if tickets[i].source not in destination_list: start_node = tickets[i].source i = 0 route[i] = start_node while hash_table_retrieve(hashtable, start_node) is not None: i += 1 next_node = hash_table_retrieve(hashtable, start_node) route[i] = next_node start_node = next_node return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) for weight, index in enumerate(weights): hash_table_insert(ht, index, weight) if len(weights) < 2: return None elif len(weights) == 2 and weights[0] == weights[1]: return (1, 0) else: weights_list = [] for weight in weights: print("retrieve: ", hash_table_retrieve(ht, limit - weight)) print("limit minus weight: ", limit - weight) if hash_table_retrieve(ht, limit - weight) is not None: weights_list.append(hash_table_retrieve(ht, limit - weight)) weights_list.sort(reverse=True) weights_tuple = tuple(weights_list) print("weights tuple at 0: ", weights_tuple[0]) print("weights tuple at 1: ", weights_tuple[1]) print("weights tuple: ", weights_tuple) ht.print() return weights_tuple
def get_indices_of_item_weights(weights, length, limit): if length <= 1: return None ht = HashTable(length) # add all elements from weights arr to dict for i in range(length): # check if a key (element from weights arr) is already in hash table # (in case there are same elements in weights array) key_exist = hash_table_retrieve(ht, weights[i]) if (key_exist == 0 or key_exist) and (limit - weights[i] == weights[i]): # 2 same elements, each equal to limit/2 => element + element = limit return (i, key_exist) hash_table_insert(ht, weights[i], i) # placeholder for keys that sum to limit keys = [] # search for key, which adds to limit with another key => limit-key = another key for i in range(length): key_value = hash_table_retrieve(ht, limit-weights[i]) if key_value: keys.append(weights[i]) keys.append(limit-weights[i]) break indexes = [weights.index(keys[0]), weights.index(keys[1])] indexes.sort(reverse=True) return (indexes[0], indexes[1])
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ # Store all the Tickets in a hashtable using source as key for i in range(length): hash_table_insert(hashtable, tickets[i].source, tickets[i]) # Get the starting Ticket for route current_ticket = hash_table_retrieve(hashtable, 'NONE') #Numer of Tickets inserted in Route count = 0 # Add the current ticket's destination to route list and move to next ticket while current_ticket.destination != "NONE": route[count] = current_ticket.destination count += 1 current_ticket = hash_table_retrieve(hashtable, current_ticket.destination) # Add final destination to list route[count] = current_ticket.destination return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length #first, we need to insert all of the tickets into the hash table for ticket in tickets: #within the hash table, we are inserting the source and destinations of each ticket hash_table_insert(hashtable, ticket.source, ticket.destination) #the first flight is the one where the source is None #we could also count it the city we are currently in as the day starts #but lets ignore that for now :) city = hash_table_retrieve(hashtable, "NONE") #walking across the route list for i in range(0, len(route) - 1): #retrieve the city value and add it to the next element in the route list route[i] = city #once added, retrieve the next city where it is the origin city = hash_table_retrieve(hashtable, city) #this city will be added to the next part of the route at the top of the loop #COME BACK HERE #always adds a "None" at the end, for now this gets around teh solution #but i need to think through how to do this better to remove the None value return route[:-1] pass
def get_indices_of_item_weights(weights, length, limit): # change this to avoid having to resize ht = HashTable(16) """ takes in three arguments: weights - a list of integers length - the length of the list limit - the integer to solve for loop through weights and add them to the hash table. key = weight, value = index look through weights and check, is limit - weight[i] in the ht? ht has linked list functionality so will need to traverse the entries at each index to confirm the above ** retrieve does this for me What to return? Question reads as - return the first pair that satisfies these conditions, in ascending index order (i.e. whichever comes first in the array) """ for k, v in enumerate(weights): hash_table_insert(ht, v, k) for k, v in enumerate(weights): difference = limit - v if hash_table_retrieve(ht, difference) is not None: difference_index = hash_table_retrieve(ht, difference) if difference_index >= k: return [difference_index, k] else: return [k, difference_index] return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE as you add items to the hashtable, check if the item you're adding is already a key (the difference) key: value == difference between limit and weight: index if the item is there return results if not, insert the item """ results = [] for i in range(0, length): difference = limit - weights[i] if hash_table_retrieve(ht, weights[i]) is None: hash_table_insert(ht, difference, i) else: if hash_table_retrieve(ht, weights[i]) <= i: results.append(i) results.append(hash_table_retrieve(ht, weights[i])) else: results.append(hash_table_retrieve(ht, weights[i])) results.append(i) if len(results) is 0: return None else: return results
def get_indices_of_item_weights(weights, length, limit): print('weights: ' + str(weights) + ', length: ' + str(length) + ', limit: ' + str(limit)) #if fewer than 2 weights if length < 2: return None #initialize hash table ht = HashTable(limit) for index in range(0, length): hash_table_insert(ht, weights[index], index) #if 2+ weights and no exact weight index1 = 0 index2 = 0 for larger_weight in range(limit, 0, -1): if larger_weight < limit / 2: return None returned = hash_table_retrieve(ht, larger_weight) if returned != None: index1 = returned[0] smaller_weight = limit - larger_weight returned = hash_table_retrieve(ht, smaller_weight) if returned != None: index2 = returned[0] if index2 == index1: index2 = returned[1] if index1 > index2: return (index1, index2) else: return (index2, index1) return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) # seems like we could just use enumerate instead of length for index in range(length): hash_table_insert(ht, weights[index], index) # print(ht.storage) for weight in weights: # get the index of the first weight first_index = hash_table_retrieve(ht, weight) # print(f'\nfirst_index: {first_index}') difference = limit - weights[first_index] # if it exists, get the index of the next weight second_index = hash_table_retrieve(ht, difference) # print(f'\nsecond_index: {second_index}') if first_index is not None and second_index is not None: if weights[first_index] < weights[second_index]: # print(f'\nFound a sum: ({second_index}), ({first_index})\n') return (second_index, first_index) else: # print(f'\nFound a sum: ({first_index}), ({second_index})\n') return (first_index, second_index) return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) for i in range(0,len(route)): if i == 0: current = hash_table_retrieve(hashtable, "NONE") else: current = hash_table_retrieve(hashtable, current) route[i] = current # print("test 1", route) for location in route: if location == "NONE": route.remove(location) # print("test 2", route) return route # # TEST CASE # ticket_1 = Ticket("NONE", "PDX") # ticket_2 = Ticket("PDX", "DCA") # ticket_3 = Ticket("DCA", "NONE") # tickets = [ticket_1, ticket_2, ticket_3] # assert reconstruct_trip(tickets, 3) == ["PDX", "DCA"]
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length for ticket in tickets: hash_table_insert(hashtable, key=ticket.source, value=ticket.destination) # The first ticket, Value of ticket == "NONE" stack = [hash_table_retrieve(hashtable, "NONE")] # used to update index in array current_index = 0 while len(stack) > 0: current_ticket = stack.pop() route[current_index] = current_ticket current_index += 1 next_destination = hash_table_retrieve(hashtable, current_ticket) # concludes loop when we've reached the last destination if next_destination is not "NONE": stack.append(next_destination) # excludes last value return route[:-1]
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) for i in range(len(weights)): weight = weights[i] val = hash_table_retrieve(ht, weight) if val is None: hash_table_insert(ht, weight, i) else: # handle duplicates in weight hash_table_insert(ht, weight + 100, i) for weight in weights: diff = limit - weight # handle duplicates in weight if diff == weight: new_weight = weight + 100 value = hash_table_retrieve(ht, new_weight) else: value = hash_table_retrieve(ht, diff) if value is not None: index = hash_table_retrieve(ht, weight) if index > value: return [index, value] else: return [value, index] return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ #save tickets in linked list for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) # first destination destination = hash_table_retrieve(hashtable, "NONE") route[0] = destination source = destination # subsequent destinations for i in range(1, length): destination = hash_table_retrieve(hashtable, source) route[i] = destination source = destination while source is not "NONE": destination = hash_table_retrieve(hashtable, source) route.append(destination) source = destination return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ for index, item in enumerate(weights, start=0): # default is zero hash_table_insert(ht, item, index) # print(hash_table_retrieve(ht, item)) for i in weights: # print(i) first_index = hash_table_retrieve(ht, i) # print(f'first index is {first_index}') if hash_table_retrieve(ht, limit - i): second_index = hash_table_retrieve(ht, limit - i) pair = [max(first_index, second_index), min(first_index,second_index)] print(f'the pair is {pair}') return (pair) # else: print('no pair exists') return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) #if there are less than 2 items in table, there is nothing to compar if length < 2: return None for i in range(0, length): #setup a ht with each weight as the key and the index as the value hash_table_insert(ht, weights[i], i) for i in range(0, length): #finds entries whose weights sums up to limit corresponding_weight = limit - weights[i] #if corresponding_weight exists while hash_table_retrieve(ht, corresponding_weight): #return the higher valued as the zeroth index if i > hash_table_retrieve(ht, corresponding_weight): return (i, hash_table_retrieve(ht, corresponding_weight)) #otherwise return it as the first index else: return (hash_table_retrieve(ht, corresponding_weight), i) #return none if key doesnt exist return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ index = 0 for item in weights: sum_weights = limit - item existing_key = hash_table_retrieve(ht, item) if (existing_key == 0 or existing_key) and (limit - item == sum_weights): return (index, existing_key) hash_table_insert(ht, item, index) index += 1 for item in weights: sum_weights = limit - item weight_index = hash_table_retrieve(ht, sum_weights) if weight_index is not None: current_index = hash_table_retrieve(ht, item) if current_index > weight_index: return (current_index, weight_index) else: return (weight_index, current_index)
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ answer = () # insert weights as key value pairs (weight: index) for i in range(0, length): hash_table_insert(ht, (weights[i]), i) print(ht.storage) # check hash table for (limit - weight) for j in range(0, length): # if we found two weights, break loop if len(answer) > 1: break # use limit - weight as key for lookup if hash_table_retrieve(ht, limit - weights[j]) is not None: answer = answer + (hash_table_retrieve(ht, limit - weights[j]), ) # if there are only two weights of the same value and the limit is divisible by two, # then add the zero-th index to the answer tuple # we must do this because two weight values cannot be saved to the hash table, the values get overwritten if length is 2 and limit % length is 0: answer = answer + (0, ) break if answer: print(answer) return answer return None
def reconstruct_trip(tickets, length): #print(f"tickets: {tickets} length: {length}") hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ # ticket_1 = Ticket("NONE", "PDX") # ticket_2 = Ticket("PDX", "DCA") # ticket_3 = Ticket("DCA", "NONE") #we need to insert the tickets and attach them to a starting location and a destination location. start: key, dest: value for i in range(0, length): #print(f"tickets: {tickets[i]}") #need to use hash_table_insert to get the key and value into our ht buckets hash_table_insert(hashtable, tickets[i].source, tickets[i].destination) #we set index of 0 to whatever ticket has the source of NONE route[0] = hash_table_retrieve(hashtable, 'NONE') #HINT: the ith location in the route can be found by checking the hash table for the i-1th location. start at 1 because 0 has already been taken up by the 'NONE' source ticket, then iterate forward. for i in range(1, length): route[i] = hash_table_retrieve(hashtable, route[i - 1]) return route # ticket_1 = Ticket("NONE", "PDX") # ticket_2 = Ticket("PDX", "DCA") # ticket_3 = Ticket("DCA", "NONE") # tickets = [ticket_1, ticket_2, ticket_3] # reconstruct_trip(tickets, 3)
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ for i in tickets: hash_table_insert(hashtable, i.source, i.destination) begin = hash_table_retrieve(hashtable, "NONE") current = begin route[0] = current j = 0 while current != "NONE": current = hash_table_retrieve(hashtable, current) j += 1 route[j] = current for airport in route: if airport == "NONE": route.remove(airport) return route
def get_indices_of_item_weights(weights, length, limit): # make a hashTable of length equivalent to the length of the weights array ht = HashTable(len(weights) or length) """ YOUR CODE HERE """ # insert each of the weights in the hashtable using their indices as values for index, weight in enumerate(weights): hash_table_insert(ht, weight, index) # initialize variables to hold our matching indices match_pair_index = None weight_index = None # loop through the weights and find the matching pair index, if exists for weight in weights: # determine the matching digit match = limit - weight # retrieve match: should return None if not existing yet match_pair_index = hash_table_retrieve(ht, match) # if match is a weight in the hashtable if match_pair_index is not None: # retrieve the index of the weight from hashtable weight_index = hash_table_retrieve(ht, weight) # format responses, with highest value in first index if match == weight: return (1, 0) elif match_pair_index > weight_index: return (match_pair_index, weight_index) elif match_pair_index < weight_index: return (weight_index, match_pair_index) return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) # store each weight in the input list as keys and # store each weight's list index as its value index = 0 for item in weights: sum_weights = limit - item # check to see if the hash table contains an entry for weight existing_key = hash_table_retrieve(ht, item) if (existing_key == 0 or existing_key) and (limit - item == sum_weights): return (index, existing_key) hash_table_insert(ht, item, index) index += 1 for item in weights: sum_weights = limit - item weight_index = hash_table_retrieve(ht, sum_weights) if weight_index is not None: current_index = hash_table_retrieve(ht, item) if current_index > weight_index: return (current_index, weight_index) else: return (weight_index, current_index)
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ for i, weight in enumerate(weights): existing_ht_entry = hash_table_retrieve(ht, weight) if existing_ht_entry == None: hash_table_insert(ht, weight, [i]) else: hash_table_insert(ht, weight, existing_ht_entry + [i]) for weight in weights: limit_minus_weight = hash_table_retrieve(ht, limit - weight) if limit_minus_weight != None: weight_index = hash_table_retrieve(ht, weight)[0] if limit - weight == weight: # print(limit_minus_weight) weight_index = limit_minus_weight[1] limit_minus_weight = limit_minus_weight[0] # print((max(limit_minus_weight, weight_index), min(limit_minus_weight, weight_index))) return (max(limit_minus_weight, weight_index), min(limit_minus_weight, weight_index))
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) count = 0 initial_index = 0 for i in range(len(weights)): hash_table_insert(ht, weights[i], i) check_val = hash_table_retrieve(ht, limit - weights[i]) if check_val is not None: dbl_check = hash_table_retrieve(ht, limit - weights[check_val]) if dbl_check is not None: if check_val > dbl_check: return (check_val, dbl_check) elif check_val < dbl_check: return (dbl_check, check_val) elif check_val == dbl_check: if count == 1: # print((i, initial_index)) return (i, initial_index) elif count == 0: count += 1 elif check_val == None: initial_index += 1 return None