def bruteForceTransport(cows,limit): """ Finds the allocation of cows that minimizes the number of spaceship trips via brute force. Parameters: cows - a dictionary of name (string), weight (float) pairs limit - weight limit of the spaceship Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ min_trip = len(cows.keys()) ans_trip = {} for part in getPartitions(cows.keys()): # Specific partition # Weights for each trip weights = [sum(cows[name] for name in p) for p in part] if False not in [x <= limit for x in weights]: if len(part) < min_trip: min_trip = len(part) ans_trip = part return ans_trip
def bruteForceTransport(cows, limit): """ Finds the allocation of cows that minimizes the number of spaceship trips via brute force. Parameters: cows - a dictionary of name (string), weight (float) pairs limit - weight limit of the spaceship Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ min_trip = len(cows.keys()) ans_trip = {} for part in getPartitions(cows.keys()): # Specific partition # Weights for each trip weights = [sum(cows[name] for name in p) for p in part] if False not in [x <= limit for x in weights]: if len(part) < min_trip: min_trip = len(part) ans_trip = part return ans_trip
def bruteForceTransport(cows, limit): """ Finds the allocation of cows that minimizes the number of spaceship trips via brute force. Parameters: cows - a dictionary of name (string), weight (float) pairs limit - weight limit of the spaceship Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ def addCowWeight(list, cows): """adds a list of cows, with value coming from dict""" sum = 0.0 for key in list: sum += cows[key] return sum #list of cows cowName = (cows.keys()) ##cowName = ['Maggie', 'Lola', 'Oreo'] #list to store all partitions and useful ones allPart = [] usePart = [] for part in getPartitions(cowName): allPart.append(part) #make a test that checks each trip list if their sum <= limit #adds each partition that passes all tests to usePart for part in allPart: test = [] for trip in part: if addCowWeight(trip, cows) <= limit: test.append(trip) if len(test) == len(part): usePart.append(part) #find all the lengths of each option, and search for smallest lenIndex = [] for part in usePart: lenIndex.append(len(part)) find = min(lenIndex) for part in usePart: if len(part) == find: return part
def bruteForceTransport(cows,limit): """ Finds the allocation of cows that minimizes the number of spaceship trips via brute force. Parameters: cows - a dictionary of name (string), weight (float) pairs limit - weight limit of the spaceship Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ def addCowWeight(list, cows): """adds a list of cows, with value coming from dict""" sum = 0.0 for key in list: sum += cows[key] return sum #list of cows cowName = (cows.keys()) ##cowName = ['Maggie', 'Lola', 'Oreo'] #list to store all partitions and useful ones allPart = [] usePart = [] for part in getPartitions(cowName): allPart.append(part) #make a test that checks each trip list if their sum <= limit #adds each partition that passes all tests to usePart for part in allPart: test = [] for trip in part: if addCowWeight(trip, cows) <= limit: test.append(trip) if len(test) == len(part): usePart.append(part) #find all the lengths of each option, and search for smallest lenIndex = [] for part in usePart: lenIndex.append(len(part)) find = min(lenIndex) for part in usePart: if len(part) == find: return part
def bruteForceTransport(cows,limit): """ Finds the allocation of cows that minimizes the number of spaceship trips via brute force. Parameters: cows - a dictionary of name (string), weight (float) pairs limit - weight limit of the spaceship Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ cowWeight = sorted(cows.values()) cowWeight.reverse() print cowWeight allPart = [] lenPart = [] usePart = [] for part in getPartitions(cowWeight): allPart.append(part) for part in allPart: numList = [item for sublist in part for item in sublist] if len(numList) == len(cowWeight): print numList lenPart.append(part) assert False for part in lenPart: test = [] for trip in part: if sum(trip) < limit: test.append(trip) if len(test) == len(part): usePart.append(test) lenIndex = [] for part in usePart: lenIndex.append(len(part)) find = min(lenIndex) for part in usePart: if len(part) == find: return part
def bruteForceTransport(cows, limit): """ Finds the allocation of cows that minimizes the number of spaceship trips via brute force. Parameters: cows - a dictionary of name (string), weight (float) pairs limit - weight limit of the spaceship Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ ## # Play with generators ## for part in getPartitions([1,2,3]): ## print part # Set up the data structures best_trips_list = [] best_trips_count = len(cows) # can't be worse than 1 cow per trip # Use the generator-returning getPartitions to partition the cows into all # possible trip combinations for part in getPartitions(cows.keys()): # Check if the current partition contains only valid trips isValid = False not in [ calculateTripWeight(trip, cows) <= 1.0 for trip in part ] # If it is valid, see if it has a better trip count than the best we've # seen so far, and if so replace it if isValid and len(part) < best_trips_count: best_trips_list = part best_trips_count = len(part) # Return the best trip partition found return best_trips_list