第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Python 返回符合設(shè)置參數(shù)的項(xiàng)目列表

Python 返回符合設(shè)置參數(shù)的項(xiàng)目列表

皈依舞 2023-09-26 17:16:49
我在將返回的解決方案正確裝箱為 min_cals <= sum(calories) <= max_cals 時(shí)遇到一些困難。有一些組合可以產(chǎn)生 sum(cals) < min_cals 的解決方案。除了保持在熱量范圍內(nèi)之外,解決方案還應(yīng)該生成一個(gè)列表,其中總成本盡可能接近預(yù)算而不超過(guò)預(yù)算限制。這是一些重新上下文化的代碼。我真的可以幫忙:menu = [    {'name':'Cheese Pizza Slice', 'calories': 700, 'cost': 4},    {'name':'House Salad', 'calories': 100, 'cost': 8.5},    {'name':'Grilled Shrimp', 'calories': 400, 'cost': 15},    {'name':'Beef Brisket', 'calories': 400, 'cost': 12},    {'name':'Soda', 'calories': 100, 'cost': 1},    {'name':'Cake', 'calories': 300, 'cost': 3},]def menu_recommendation(menu, min_cal, max_cal, budget):    menu = [item for item in menu if item['calories'] <= max_cal and item['cost'] <= budget]    if len(menu) == 0: return []    return min((        [item] + menu_recommendation(menu, min_cal - item['calories'], max_cal - item['calories'], budget - item['cost'])        for item in menu    ), key=         lambda recommendations: [budget - sum(item['cost'] for item in recommendations) and min_cal <= sum(item['calories'] for item in recommendations) <= max_cal, -sum(item['calories'] for item in recommendations)]    )recommendation = menu_recommendation(menu, 1000, 1200, 15)total_cost = sum(item['cost'] for item in recommendation)total_cals = sum(item['calories'] for item in recommendation)print(f'recommendation: {recommendation}')print(f'total cost: {total_cost}')print(f'total calories: {total_cals}')例如,以下命令返回總卡路里數(shù)為 700 的解決方案,低于最小值 1000。推薦 = menu_recommendation(菜單, 1000, 1200, 15)
查看完整描述

4 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

我想出了這個(gè),它基本上是一個(gè)背包,但如果不適合推薦,它會(huì)從菜單中遞歸刪除菜肴:


menu = [

    {'name':'Cheese Pizza Slice', 'calories': 700, 'cost': 4},

    {'name':'House Salad', 'calories': 100, 'cost': 8.5},

    {'name':'Grilled Shrimp', 'calories': 400, 'cost': 15},

    {'name':'Beef Brisket', 'calories': 400, 'cost': 12},

    {'name':'Soda', 'calories': 100, 'cost': 1},

    {'name':'Cake', 'calories': 300, 'cost': 3},

]




def get_price(recommendation):

    return sum(dish["cost"] for dish in recommendation)


def get_calories(recommendation):

    return sum(dish["calories"] for dish in recommendation)


def menu_recommendation(menu, min_cal, max_cal, budget):

    sorted_menu = sorted(menu, key=lambda dish: dish["cost"], reverse=True)

    recommendation = []

    for dish in sorted_menu:

      if dish["cost"] + get_price(recommendation) <= budget:

        recommendation.append(dish)

    if recommendation:

      if get_calories(recommendation) < min_cal:

        sorted_menu.remove(min(recommendation, key=lambda dish: dish["calories"]/dish["cost"]))

        return menu_recommendation(sorted_menu, min_cal, max_cal, budget)

      if get_calories(recommendation) > max_cal:

        sorted_menu.remove(max(recommendation, key=lambda dish: dish["calories"]/dish["cost"]))

        return menu_recommendation(sorted_menu, min_cal, max_cal, budget)

    return recommendation





recommendation = menu_recommendation(menu, 500, 800, 15)

total_cost = sum(item['cost'] for item in recommendation)

total_cals = sum(item['calories'] for item in recommendation)

print(f'recommendation: {recommendation}')

print(f'total cost: {total_cost}')

 

它根據(jù)卡路里/成本率刪除元素,因?yàn)檫@是應(yīng)用背包的成本。


如果您有任何疑問(wèn),請(qǐng)告訴我。


查看完整回答
反對(duì) 回復(fù) 2023-09-26
?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊

(calorie, cost)這是一個(gè)動(dòng)態(tài)編程解決方案,它構(gòu)建了一個(gè)數(shù)據(jù)結(jié)構(gòu),顯示我們可以最終得到的所有選項(xiàng)以及每個(gè)選項(xiàng)。我們尋找符合標(biāo)準(zhǔn)的最佳方案,然后找到推薦方案。


def menu_recommendation(menu, min_cal, max_cal, budget):

    # This finds the best possible solution in pseudo-polynomial time.

    recommendation_tree = {(0, 0.0): None}

    for item in menu:

        # This tree will wind up being the old plus new entries from adding this item.

        new_recommendation_tree = {}

        for key in recommendation_tree.keys():

            calories, cost = key

            new_recommendation_tree[key] = recommendation_tree[key]

            new_key = (calories + item['calories'], cost + item['cost'])

            if new_key not in recommendation_tree and new_key[0] <= max_cal:

                # This is a new calorie/cost combination to look at.

                new_recommendation_tree[new_key] = item

        # And now save the work.

        recommendation_tree = new_recommendation_tree


    # Now we look for the best combination.

    best = None

    for key in recommendation_tree:

        calories, cost = key

        # By construction, we know that calories <= max_cal

        if min_cal <= calories:

            if best is None or abs(budget - cost) < abs(budget - best[1]):

                # We improved!

                best = key


    if best is None:

        return None

    else:

        # We need to follow the tree back to the root to find the recommendation

        calories, cost = best

        item = recommendation_tree[best]

        answer = []

        while item is not None:

            # This item is part of the menu.

            answer.append(item)

            # And now find where we were before adding this item.

            calories = calories - item['calories']

            cost = cost - item['cost']

            best = (calories, cost)

            item = recommendation_tree[best]

        return answer


查看完整回答
反對(duì) 回復(fù) 2023-09-26
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

也許我們可以做一些遞歸的事情。


def smallest_combo(lst, m, n, z):

    # filter list to remove elements we can't use next without breaking the rules

    lst = [dct for dct in lst if m <= dct['x'] <= n and dct['y'] <= z]

    # recursive base case

    if len(lst) == 0:

        return []

    # go through our list of eligibles

    # simulate 'what would the best possibility be if we picked that one to go with next'

    # then of those results select the one with the sum('y') closest to z

    #   (breaking ties with the largest sum('x'))

    return min((

        [dct] + smallest_combo(lst, m - dct['x'], n - dct['x'], z - dct['y'])

        for dct in lst

    ), key= 

        lambda com: [z - sum(d['y'] for d in com), -sum(d['x'] for d in com)]

    )


inp = [{'name': 'item1', 'x': 600, 'y': 5},

 {'name': 'item2', 'x': 200, 'y': 8},

 {'name': 'item3', 'x': 500, 'y': 12.5},

 {'name': 'item4', 'x': 0, 'y': 1.5},

 {'name': 'item5', 'x': 100, 'y': 1}]

print(smallest_combo(inp, 500, 1500, 25))

# [{'name': 'item3', 'x': 500, 'y': 12.5}, {'name': 'item3', 'x': 500, 'y': 12.5}]

有多種方法可以加快這一速度。首先通過(guò)制作遞歸緩存,然后采用動(dòng)態(tài)編程方法(即從底部開始而不是從頂部開始)。


查看完整回答
反對(duì) 回復(fù) 2023-09-26
?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

我相信我已經(jīng)解決了提出超出范圍 min_cal / max_cal 邊界的建議的問(wèn)題,但我仍然覺(jué)得可能有一個(gè)更接近預(yù)算的解決方案。


這是我更新的代碼:


menu = [

    {'name':'Cheese Pizza Slice', 'calories': 700, 'cost': 4},

    {'name':'House Salad', 'calories': 100, 'cost': 8.5},

    {'name':'Grilled Shrimp', 'calories': 400, 'cost': 15},

    {'name':'Beef Brisket', 'calories': 400, 'cost': 12},

    {'name':'Soda', 'calories': 100, 'cost': 1},

    {'name':'Cake', 'calories': 300, 'cost': 3},

]


def menu_recommendation(menu, min_cal, max_cal, budget):

    menu = [item for item in menu if item['calories'] <= max_cal and item['cost'] <= budget]

    if len(menu) == 0: return []

    return min(([item] + menu_recommendation(menu, min_cal - item['calories'], max_cal - item['calories'], budget - item['cost']) 

        for item in menu

), key= 

    lambda recommendations: [budget - sum(item['cost'] for item in recommendations) 

                             and min_cal - sum(item['calories'] for item in recommendations) >= 0

                             and max_cal - sum(item['calories'] for item in recommendations) >= 0, 

                             -sum(item['calories'] for item in recommendations)]

)


recommendation = menu_recommendation(menu, 1000, 1200, 15)

total_cost = sum(item['cost'] for item in recommendation)

total_cals = sum(item['calories'] for item in recommendation)

print(f'recommendation: {recommendation}')

print(f'total cost: {total_cost}')

print(f'total calories: {total_cals}')

如果有人有任何改進(jìn),我很樂(lè)意聽到他們的聲音!


查看完整回答
反對(duì) 回復(fù) 2023-09-26
  • 4 回答
  • 0 關(guān)注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)