def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length #creates hashtable with key-source, value-destination for i in range(0, length): hash_table_insert(hashtable, tickets[i].source, tickets[i].destination) #finds the starting destination to place at the start of route route[0] = (hash_table_retrieve(hashtable, "NONE")) # finds next destination for each previous source and appends to route for i in range(1, length): source = route[i-1] route[i] = hash_table_retrieve(hashtable, str(source)) #print(route) 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) if ticket.source == 'NONE': route[0] = ticket.destination curr_loc = route[0] for ticket in tickets[1:]: destination = hash_table_retrieve(hashtable, curr_loc) route[tickets.index(ticket)] = destination curr_loc = destination return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ for i in range(len(weights)): diff = hash_table_retrieve(ht, limit - weights[i]) print(diff, limit - weights[i], weights[i]) if diff is None: print(f"diff is not in the table. ") hash_table_insert(ht, weights[i], i) print( f"diff is now in the table. Value is {weights[i]}, and index of {i} " ) else: print(f"Here is the diff {diff} and i {i}") return (i, diff) return None
def reconstruct_trip(tickets, length): ht = HashTable(length) route = [None] * length """ YOUR CODE HERE """ for index in range(length): hash_table_insert(ht, tickets[index].source, tickets[index].destination) # print(ht,tickets[index].source, tickets[index].destination) route[0] = hash_table_retrieve(ht, 'NONE') for j in range(1, length-1): route[j] = hash_table_retrieve(ht, route[j-1]) route.pop() return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) index = 0 source = "NONE" while True: destination = hash_table_retrieve(hashtable, source) if destination is not "NONE": route[index] = destination source, index = destination, index + 1 else: break return route
def reconstruct_trip(tickets, length): ht = HashTable(length) route = [None] * (length - 1) for ticket in tickets: hash_table_insert(ht, str(ticket.source), str(ticket.destination)) counter = 0 destination = True prev = 'NONE' for _ in range(length - 1): print('loop ', counter) destination = hash_table_retrieve(ht, str(prev)) route[counter] = destination prev = destination counter += 1 return route
def get_indices_of_item_weights(weights, length, limit): hashtable = HashTable(16) # Iterate through weights for i, weight in enumerate(weights): diff = limit - weight # Check to see if weight remainder (diff) is in hashashtableable... if hash_table_retrieve(hashtable, diff) is not None: # ... if it is, return the location of the difference, and tuple of current index. return i, hash_table_retrieve(hashtable, diff)[1] # ... if it's not, store a new key:value pair of weight. hash_table_insert(hashtable, weight, (diff, i)) #if no solution is found, return None return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) for wt in weights: hash_table_insert(ht, wt, weights.index(wt)) # limit - wt for wt in weights: if hash_table_retrieve(ht, (limit - wt)) is not None: if wt == (limit - wt): return (weights.index(wt, weights.index(wt) + 1), weights.index(wt)) zero = max([wt, (limit - wt)]) one = min([wt, (limit - wt)]) return (hash_table_retrieve(ht, zero), hash_table_retrieve(ht, one)) return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ result = [] if len(weights) <= 1: return None for i in weights: difference = limit - i hash_table_insert(ht, difference, i) for j, i in enumerate(weights): if hash_table_retrieve(ht, i) == limit - i: result.insert(0, j) return result
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) cur_ticket = hash_table_retrieve(hashtable, 'NONE') new_route = [] while cur_ticket != 'NONE': new_route.append(cur_ticket) cur_ticket = hash_table_retrieve(hashtable, cur_ticket) return new_route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ # Insert locations on hash table for location in range(len(tickets)): hash_table_insert(hashtable, tickets[location].source, tickets[location].destination) # set start point in route array to NONE route[0] = hash_table_retrieve(hashtable, 'NONE') for location in range(1, len(route)): route[location] = hash_table_retrieve(hashtable, route[location - 1]) return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length - 1) route = [None] * (length - 1) for tix in tickets: if tix.source == 'NONE': curr = tix.destination route[0] = curr else: hash_table_insert(hashtable, tix.source, tix.destination) indx = 1 while True: curr = hash_table_retrieve(hashtable, curr) if curr == 'NONE': break else: route[indx] = curr indx += 1 return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ index = 0 for weight in weights: difference_w = limit - weight possible_answer = hash_table_retrieve(ht, difference_w) if possible_answer is not None: if possible_answer < index: return (index, possible_answer) else: return (index, possible_answer) hash_table_insert(ht, weight, index) index += 1 return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ for i in range(length): hash_table_insert(ht, weights[i], i) for i in range(length): index_in_ht = hash_table_retrieve(ht, (limit - weights[i])) if index_in_ht: if index_in_ht > i: return [index_in_ht, i] else: return [i, index_in_ht] return None
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) # Insert all weights into hashtable (key - hashweight : value - index) for i in range(0, length): hash_table_insert(ht, weights[i], i) # Search for two keys that sum to limit for i in range(0, length): result = hash_table_retrieve(ht, (limit - weights[i])) if result is not None: if result > i: tuple = (result, i) else: tuple = (i, result) return tuple return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length for i in range(len(tickets)): # print(tickets[i].source, tickets[i].destination) hash_table_insert(hashtable, tickets[i].source, tickets[i].destination) if tickets[i].source == "NONE": route[0] = tickets[i].destination elif tickets[i].destination == "NONE": route[-1] = tickets[i].source for i in range(len(route)): if route[i] is not route[0] or route[i] is not route[-1]: route[i] = hash_table_retrieve(hashtable, route[i - 1]) return route[1:]
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length for ticket in tickets: print(ticket.source, ticket.destination) hash_table_insert(hashtable, ticket.source, ticket.destination) key = "NONE" for index in range(length): current_ticket = hash_table_retrieve(hashtable, key) key = current_ticket route[index] = current_ticket return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) """ YOUR CODE HERE """ for i in range(0, length): hash_table_insert(hashtable, tickets[i].source, tickets[i].destination) flight = [] source = "NONE" next = "NONE" for i in range (0, length): source = next next = hash_table_retrieve(hashtable, source) flight.append(next) return flight
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(16) """ YOUR CODE HERE """ for i in range(length): current_value = weights[i] hash_table_insert(ht, current_value, i) for i in range(length): target = limit - weights[i] answer = hash_table_retrieve(ht, target) if answer is not None: return (answer, i) return None
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length # Build hash table by inserting the source as key and destination as value for i in range(length): start = tickets[i].source end = tickets[i].destination hash_table_insert(hashtable, start, end) # create initial element of route list by grabbing the ticket with NONE key/source route[0] = hash_table_retrieve(hashtable, 'NONE') # loop and add tickets to the route by using the destination/value of prior ticket as # the key/source for the current ticket for i in range(1, length): route[i] = hash_table_retrieve(hashtable, route[i - 1]) return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(length) hash_table_insert(ht, weights[0], 0) current_index = 1 while current_index < length: current_weight = weights[current_index] hash_table_insert(ht, current_weight, current_index) pair_to_limit = limit - current_weight does_have_pair = hash_table_retrieve(ht, pair_to_limit) if does_have_pair: if current_index == 1: return (current_index, 0) elif does_have_pair > current_index: return (does_have_pair, current_index) else: return (current_index, does_have_pair) current_index += 1 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) if ticket.source == "NONE": route[0] = ticket.destination curr_ticket = route[0] pointer = 1 while route[-1] is None: route[pointer] = hash_table_retrieve(hashtable, curr_ticket) curr_ticket = route[pointer] pointer += 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) current = hash_table_retrieve(hashtable, 'NONE') for i in range(length): route[i] = current current = hash_table_retrieve(hashtable, current) return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [] """ YOUR CODE HERE """ for ticket in tickets: key = ticket.source value = ticket.destination hash_table_insert(hashtable, key, value) travel_destination = hash_table_retrieve(hashtable, "NONE") while travel_destination is not 'NONE': route.append(travel_destination) travel_destination = hash_table_retrieve(hashtable, travel_destination) return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) starting_place = hash_table_retrieve(hashtable, 'NONE') route[0] = starting_place for idx, destination in enumerate(route): if idx + 1 > len(route) - 1: break route[idx + 1] = hash_table_retrieve(hashtable, destination) return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length """ YOUR CODE HERE """ # insert all tickets in a hash table for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) # manually insert first ticket route[0] = hash_table_retrieve(hashtable, 'NONE') # insert tickets in order, skipping first for i in range(1, length): route[i] = hash_table_retrieve(hashtable, route[i - 1]) return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * (length - 1) """ YOUR CODE HERE """ # put all tickets into the hashtable with source as key and destination as value for ticket in tickets: hash_table_insert(hashtable, ticket.source, ticket.destination) # initialize source for loop source = "NONE" for i in range(0, length - 1): destination = hash_table_retrieve(hashtable, source) # if destination is not "NONE": route[i] = destination source = destination return route
def get_indices_of_item_weights(weights, length, limit): ht = HashTable(length * 2) """ YOUR CODE HERE """ for x in range(len(weights)): hash_table_insert(ht, weights[x], x) answer = None for x in range(len(weights)): target = limit - weights[x] current = weights[x] current_index = x valid = hash_table_retrieve(ht, target) if valid is not None: if x > valid: answer = (x, valid) else: answer = (valid, x) return answer return answer
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length current = 'NONE' for item in tickets: hash_table_insert(hashtable, item.source, item.destination) if item.source == "NONE": current = item.destination for i in range(len(route)): route[i] = current current = hash_table_retrieve(hashtable, current) """ YOUR CODE HERE """ return route
def reconstruct_trip(tickets, length): hashtable = HashTable(length) route = [None] * length for trip in tickets: # print("TRIP", trip.source) hash_table_insert(hashtable, trip.source, trip.destination) # print("HASTABLE", hashtable.storage[1].key) # for key in hashtable.storage: # print("KEY", key) current = hash_table_retrieve(hashtable, "NONE") for i in range(length): route[i] = current current = hash_table_retrieve(hashtable, current) return route