def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ # firstly, insert all tickets in the tickets list into the hashtable using insert method # passing in the ticket source as the key and the ticket destination as the value for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) # we know that the very first ticket is going to have to have a source on "NONE" # declare variable 'source' and initialize it to "NONE" source = "NONE" # now loop over the route array for i in range(len(route)): # at every index, set the value equal to what is returned from retrieving from the hash table, passing the current value of source as the key to look for curr_ticket = hash_table_retrieve(hashtable, source) # set the value of source to the value of curr_ticket, will be used as the key to retrieve the next ticket from hashtable source = curr_ticket # set the value in the route array at the current index equal to the curr_ticket route[i] = curr_ticket # return the route return [r for r in route if r != "NONE"]
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [] # loop through scrambled tickets for ticket in tickets: # add to hashtable hash_table_insert(hashtable, ticket.source, ticket.destination) current_ticket = hash_table_retrieve(hashtable, 'NONE') route.append(current_ticket) # find order of tickers def sortTickets(ticket): if ticket == 'NONE': return # get next stop ticket next_ticket = hash_table_retrieve(hashtable, ticket) if next_ticket != 'NONE': # add to route route.append(next_ticket) # go to next ticket in trip return sortTickets(next_ticket) sortTickets(current_ticket) return route
def reconstruct_trip(tickets, length): ht = HashTable(length) route = [None] * length current_ticket = None # for every ticket in tickets # add it to the hash table key = source, value = destination for i in range(length): hash_table_insert(ht, tickets[i].source, tickets[i].destination) # when you find ticket with current location (NONE) if tickets[i].source == "NONE": # add it to variable `current_ticket` current_ticket = tickets[i].destination # and make it the first destination of the route route[0] = current_ticket # then loop over the tickets again for i in range(1, length): # find the next destination dest = hash_table_retrieve(ht, current_ticket) # add it to the route array route[i] = dest # and make it current_ticket current_ticket = dest 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) """ YOUR CODE HERE """ # For loop: for i in range of 0 and the length of the weights. for i in range(0, len(weights)): # Set wt to weights[i] wt = weights[i] # set retrieve to hash table retrieve `limit - weight` retrieve = hash_table_retrieve(ht, limit - wt) if retrieve is None: # then return hash table insert hash_table_insert(ht, wt, i) print(f'Weight Index: {i} Value: {wt}') # Otherwise else: # return i, and retrieve return (i, retrieve) return None
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 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 get_indices_of_item_weights(weights, length, limit): # weights = weights_1, weights_2, etc # length = lenght of weights_1, weights2, etc # limit = Amount needed when the 2 weights are added together ht = HashTable(16) """ YOUR CODE HERE """ result = [] # Must add 2 numbers, so only 1 number would automatically be false if length == 1: result = None # insert values for i in range(length): hash_table_insert(ht, weights[i], i) # retrieve values for i in range(length): package1 = weights[i] package2 = limit - package1 check = hash_table_retrieve(ht, package2) if check is not None: if check > i: result = (check, i) elif check < i: result = (i, check) return result
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ # BRUTE FORCE # for i in range(len(weights) - 1): # for j in range(i+1, len(weights)): # print(weights[i] + weights[j]) # if weights[i] + weights[j] == limit: # result = (i, j) if i > j else (j, i) # return result # return None if len(weights) == 2 and weights[0] + weights[1] == limit: return (1, 0) for i in range(len(weights) - 1): hash_table_insert(ht, weights[i], i) for i in range(len(weights) - 1): key = hash_table_retrieve(ht, limit - weights[i]) if key: val_1 = i val_2 = weights.index(limit - weights[i]) result = (val_1, val_2) if val_1 > val_2 else (val_2, val_1) return result
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(length) # print("ht". ht.storage) count = 0 for i in weights: print("IIII", i, count, hash_table_retrieve(ht, i)) if hash_table_retrieve(ht, i) != None: print("I and I", i + i, limit) if i + i == limit: print("Hello") if hash_table_retrieve(ht, i) > count: return (hash_table_retrieve(ht, i), count) return (count, hash_table_retrieve(ht, i), count) hash_table_insert(ht, i, count) if hash_table_retrieve(ht, limit - i): print("Inside", hash_table_retrieve(ht, limit - i), count) if hash_table_retrieve(ht, limit - i) > count: print("Inside Inside", hash_table_retrieve(ht, limit - i), count) return (hash_table_retrieve(ht, limit - i), count) print("Inside Outside", count, hash_table_retrieve(ht, limit - i)) return (count, hash_table_retrieve(ht, limit - i), count) print("COUNT", count) count += 1 return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length # loop through the tickets for ticket in tickets: # add the tickets source/destination to the hashtable as a pair hash_table_insert(hashtable, ticket.source, ticket.destination) # set a variable source to string None source = "NONE" # set a variable position to 0 position = 0 # loop while position is less than length while position < length: # get the destination for the source from the hashtable destination = hash_table_retrieve(hashtable, source) # set the value of source in route using the position route[position] = destination # set source to the destination for the next iteration source = destination # increment position position += 1 return route
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 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) # 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 """ 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) 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 get_indices_of_item_weights(weights, length, limit): ht = HashTable(length) """ YOUR CODE HERE """ if length < 2: return None for i in range(0, length): hash_table_insert(ht, weights[i], i) # print(ht.storage) keep_going = True i = 0 while i < length: val_to_find = limit - weights[i] check_find = hash_table_retrieve(ht, val_to_find) # print(f"check_find: {check_find} | val_to_find: {val_to_find}") if check_find is not None: if check_find < i: return [i, check_find] else: return [check_find, i] else: i += 1 return None
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 get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ # loop through the weights for i in range(0, length): # the current weight is the weight at that index current_weight = weights[i] # use the hashtable insert function hash_table_insert(ht, current_weight, i) # get the index of limit - current weight using the retrieve function diff = limit - current_weight result = hash_table_retrieve(ht, diff) # since the order matters, check the size of the indices # of the weights and return in the correct order. # The higher valued index should be placed in the `zeroth` index # and the smaller index should be placed in the `first` index if result: if length == 2: return (1, 0) return (i, result) return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length available_ticket = None for i in range(length): departure = tickets[i].source arrival = tickets[i].destination hash_table_insert(hashtable, departure, arrival) # check if the ticket for your first flight has a destination with a source of NONE if departure == "NONE": available_ticket = arrival # set as starting location route[0] = available_ticket for i in range(1, length): next_destination = hash_table_retrieve(hashtable, available_ticket) route[i] = next_destination available_ticket = next_destination return route
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 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 get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ final = [] for i in range(length): hash_table_insert(ht, weights[i], i) for w in range(length): # print(limit - weights[w]) if hash_table_retrieve(ht, limit - weights[w]): # print('above if') final = [w, weights.index(limit - weights[w])] # if w > limit - weights[w]: # final[0] = w # final[1] = limit - weights[w] # return final # else: # final[0] = limit - weights[w] # final[1] = w # return final if len(final): return final else: return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) print("\nRUN") # print the inputs we are working with # print(f'\nLength: {length}') # print(f'Weights: {weights}') print(f'Limit: {limit}') # loop through weights array for i in range(0, length): # store the weight = weights[i] weight = weights[i] print(f'Weight: {weight}') # store the difference between the weight and limit difference = limit - weight print(f'Difference: {difference}') # check if hash table contains an entry for limit - weight retrieved = hash_table_retrieve(ht, difference) print(retrieved) if retrieved is not None: print((i, retrieved)) return (i, retrieved) # store weight's list index as value hash_table_insert(ht, weight, i)
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ # Look the length of weights given for index in range(length): # compare the difference between the limit and each weight given diff = limit - weights[index] # Check the table before newer weights added, otherwise proceed adding more weights to hashtable # Find the indices of weights that hit the limit target = hash_table_retrieve(ht, diff) # If target is not empty, then index of other element needed to sum up to limit is found if target is not None: # sum of Index weight and target weight should hit the limit return (index, target) # Store the weights inside of a hashtable hash_table_insert(ht, weights[index], index) return None
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(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 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 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