Ejemplo n.º 1
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """    
    "*** YOUR CODE HERE ***"

    bestShop = None
    bestCost = -1

    for shop in fruitShops:
      totalCost = 0;
      for fruit, numPound in orderList:
        fruitCost = shop.getCostPerPound(fruit)
        if fruitCost != None:
          totalCost += fruitCost * numPound
        else:
          totalCost = -1;
          break
      if (totalCost > 0) and (totalCost < bestCost or bestCost == -1):
        bestShop = shop
        bestCost = totalCost

      # print "Shop %s: %.2f" % (shop.getName(), totalCost)

    return bestShop
Ejemplo n.º 2
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """    
    "*** YOUR CODE HERE ***"
    totalCost = {}
    for shop in fruitShops:
        totalCost[shop] = 0.0

    for order in orderList:
        fruit, pounds = order
        for shop in totalCost.keys():
            cost = shop.getCostPerPound(fruit)
            if cost == None:
                print "%s doesn't have %s" % (shop, fruit)
                del totalCost[shop]
            else:
                totalCost[shop] += cost * pounds

    if len(totalCost.keys()) == 0:
        return None

    bestShop = (totalCost.keys())[0]
    minCost = totalCost[bestShop]
    for shop, cost in totalCost.items():
        if cost < minCost:
            bestShop = shop
            minCost = cost

    return bestShop
Ejemplo n.º 3
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    store_num = 0 # current store we're testing
    final_store_num = 0 # the store with the best value
    min = 0 # current minimum price
    for shop in fruitShops: # iterate through stores
      total = 0
      for fruit in orderList: # iterate through grocery list
        # compute prices and add it total price
        total = total + shop.getCostPerPound(fruit[0])  * fruit[1]
      if min == 0:
        min = total
        final_store_num = store_num
      elif min > total:
        # update the minimum total if new total is less than
        min = total
        final_store_num = store_num
      # iterate store number
      store_num = store_num + 1
    #return the store than has the least cost
    return fruitShops[final_store_num]
Ejemplo n.º 4
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"

    bestShop = None
    bestCost = -1

    for shop in fruitShops:
        totalCost = 0
        for fruit, numPound in orderList:
            fruitCost = shop.getCostPerPound(fruit)
            if fruitCost != None:
                totalCost += fruitCost * numPound
            else:
                totalCost = -1
                break
        if (totalCost > 0) and (totalCost < bestCost or bestCost == -1):
            bestShop = shop
            bestCost = totalCost

        # print "Shop %s: %.2f" % (shop.getName(), totalCost)

    return bestShop
Ejemplo n.º 5
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    totalCost = {}
    for shop in fruitShops:
        totalCost[shop] = 0.0

    for order in orderList:
        fruit, pounds = order
        for shop in totalCost.keys():
            cost = shop.getCostPerPound(fruit)
            if cost == None:
                print "%s doesn't have %s" % (shop, fruit)
                del totalCost[shop]
            else:
                totalCost[shop] += cost * pounds

    if len(totalCost.keys()) == 0:
        return None

    bestShop = (totalCost.keys())[0]
    minCost = totalCost[bestShop]
    for shop, cost in totalCost.items():
        if cost < minCost:
            bestShop = shop
            minCost = cost

    return bestShop
Ejemplo n.º 6
0
def buyFruitFromShop(orderList, shop):
    totalCost = 0.0
    for tuple in orderList:
        price = shop.getCostPerPound(tuple[0])
        # Check if the fruit is available at this shop
        if not price:
            print 'Error:', tuple[0], 'is not available at this shop'
            return None
        totalCost += price * tuple[1]
    return totalCost
Ejemplo n.º 7
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    costs = {}
    for shop in fruitShops:
        costs[shop] = 0
        for fruit, pounds in orderList:
            costs[shop] += shop.getCostPerPound(fruit) * pounds

    return min(costs, key=lambda x: costs[x])
Ejemplo n.º 8
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    lista = []
    for shop in fruitShops:
        cost = 0.0
        for item in orderList:
            cost += shop.getCostPerPound(item[0]) * item[1]
        lista.append(cost)
    return fruitShops[lista.index(min(lista))]
Ejemplo n.º 9
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    lista=[]
    for shop in fruitShops:
        cost=0.0;
        for item in orderList:
            cost+=shop.getCostPerPound(item[0])*item[1]
        lista.append(cost)
    return fruitShops[lista.index(min(lista))]
Ejemplo n.º 10
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    mn_cost, res = 1e8, None
    for shop in fruitShops:
      cost = 0
      for fruit, pounds in orderList:
        cost += pounds * shop.getCostPerPound(fruit)
      if cost < mn_cost:
        mn_cost, res = cost, shop
    return res
Ejemplo n.º 11
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    mn_cost, res = 1e8, None
    for shop in fruitShops:
        cost = 0
        for fruit, pounds in orderList:
            cost += pounds * shop.getCostPerPound(fruit)
        if cost < mn_cost:
            mn_cost, res = cost, shop
    return res
Ejemplo n.º 12
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    best_price = 99999999.99
    best_shop = fruitShops[0]
    for shop in fruitShops:
        price = sum([shop.getCostPerPound(i[0]) * i[1] for i in orderList])
        if best_price > price:
            best_price = price
            best_shop = shop
    return best_shop
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    price = 100000
    selectedShop = "None"
    for shop in fruitShops:
        cost = 0
        for fruit in orderList:
            cost += shop.getCostPerPound(fruit[0]) * fruit[1]
        if cost < price:
            price = cost
            selectedShop = shop
    return selectedShop
Ejemplo n.º 14
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    #assume that the fruits are available in all shops
    shopname = fruitShops[0]
    min_cost = 10000
    for shop in fruitShops:
        cost = 0
        for fruit, numPound in orderList:
            cost += shop.getCostPerPound(fruit) * numPound
        if cost < min_cost:
            min_cost = cost
            shopname = shop
    return shopname
Ejemplo n.º 15
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    cheapest = 0.0
    finalShop = 0
    for shop in fruitShops:
        total = 0.0
        for a,b in orderList:
            total += b * shop.getCostPerPound(a)
        if cheapest is 0.0 or total < cheapest:
            cheapest = total
            finalShop = shop
    return finalShop
Ejemplo n.º 16
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    min_shop = None
    min_price = float("Infinity")
    for shop in fruitShops:
        price = 0.0
        for order in orderList:
            price += shop.getCostPerPound(order[0]) * order[1]
        if price < min_price:
            min_price = price
            min_shop = shop
    return min_shop
Ejemplo n.º 17
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
  """
    "*** YOUR CODE HERE ***"
    minShop = None
    minPrice = 99999.0
    for shop in fruitShops:
        totalThisShop = 0.0
        for order in orderList:
            totalThisShop += order[1] * shop.getCostPerPound(order[0])
        if totalThisShop < minPrice:
            minPrice = totalThisShop
            minShop = shop
    return minShop
Ejemplo n.º 18
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    min = -1
    bestShop = None
    for shop in fruitShops:
        total = 0
        for order in orderList:
            fruitName, amount = order
            total = total + shop.getCostPerPound(fruitName) * amount
        if total < min or min < 0:
            min = total
            bestShop = shop
    return bestShop
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    totalCost = float("inf")
    name = ""
    for shop in fruitShops:
        payment = 0.0
        for fruit, num in orderList:
            payment = payment + shop.getCostPerPound(fruit) * num
        if payment < totalCost:
            totalCost = payment
            name = shop
    return name
Ejemplo n.º 20
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    bestCost = 99999999
    bestShop = None
    for shop in fruitShops:
        cost = 0
        for f, q in orderList:
            cost = cost + shop.getCostPerPound(f) * q
        if (cost < bestCost):
            bestCost = cost
            bestShop = shop
    return bestShop
Ejemplo n.º 21
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    cheapest = 999
    current = fruitShops[0]
    for shop in fruitShops:
        totalCost = 0
        for fruit, pound in orderList:
            cost = shop.getCostPerPound(fruit)*pound
            totalCost = totalCost+cost
        if totalCost < cheapest:
            cheapest = totalCost
            current = shop
    return current
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    m = 0
    name = fruitShops[0]
    for fruit in orderList:
        m += fruit[1] * name.getCostPerPound(fruit[0])
    for shop in fruitShops:
        costShop = 0
        for fruit in orderList:
            costShop = costShop + fruit[1] * shop.getCostPerPound(fruit[0])
        if costShop < m:
            m = costShop
            name = shop
    return name
Ejemplo n.º 23
0
    def getFruitCostPerPoundOnRoute(self, fruit, route):
        """
            fruit: Fruit string

            route: List of shop names
        Returns the best cost per pound of 'fruit' at any of the shops along
        the route. If none of the shops carry 'fruit', returns None
        """
        routeShops = [shop for shop in self.shops if shop.getName() in route]
        costs = []
        for shop in routeShops:
            cost = shop.getCostPerPound(fruit)
            if cost is not None:
                costs.append(cost)
        if not costs:
            # None of the shops carry this fruit
            return None
        return min(costs)
    def getFruitCostPerPoundOnRoute(self, fruit, route):
        """
            fruit: Fruit string

            route: List of shop names
        Returns the best cost per pound of 'fruit' at any of the shops along 
        the route. If none of the shops carry 'fruit', returns None
        """
        routeShops = [ shop for shop in self.shops if shop.getName() in route ]
        costs = []
        for shop in routeShops:
            cost = shop.getCostPerPound(fruit)
            if cost is not None:
                costs.append(cost)
        if not costs:
            # None of the shops carry this fruit
            return None
        return min(costs)
Ejemplo n.º 25
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    "*** YOUR CODE HERE ***"
    minCost = 99999
    minShop = ''
    for shop in fruitShops:
        totalCost = 0.0
        for f in orderList:
            fru, num = f
            totalCost += num * shop.getCostPerPound(fru)
        if totalCost < minCost:
            minCost = totalCost
            minShop = shop

    return minShop
Ejemplo n.º 26
0
def shopSmart(orderList, fruitShops):
    """
        orderList: List of (fruit, numPound) tuples
        fruitShops: List of FruitShops
    """
    
    current_total = 0.0
    cheaper_shop = None
    #first loop iterates through all the shop instances
    for shop in fruitShops:
        temp_total= 0.0
        #second loop check at the total cost of the orderlist for each instance.
        for fruit_name, quant, in orderList:
            temp_total = temp_total+(quant*shop.getCostPerPound(fruit_name))
        #if statement checks the total price of current shop list against the previous
        if temp_total<current_total or current_total == 0.0:
            current_total= temp_total
            cheaper_shop = shop
        print("current total",current_total,"temp total",temp_total)
    return cheaper_shop