【每日一题20220913】面包师皮特

:man_cook:皮特喜欢烤一些蛋糕。他有一些食谱和配料。可惜他数学不好。你能帮他找出,考虑到他的食谱,他能烤多少蛋糕?

编写一个函数cakes() ,它接受食谱(对象)和可用成分(也是一个对象)并返回皮特可以烘烤的最大蛋糕数量(整数)。为简单起见,数量没有单位(例如,1 磅面粉或 200 克糖只是 1 或 200)。对象中不存在的成分可以视为 0。

例子:

# must return 2
cakes({flour: 500, sugar: 200, eggs: 1}, {flour: 1200, sugar: 1200, eggs: 5, milk: 200})
# must return 0
cakes({apples: 3, flour: 300, sugar: 150, milk: 100, oil: 100}, {sugar: 500, flour: 2000, milk: 2000})

题目难度:一般
题目来源: Pete, the baker | Codewars

def cakes(recipe, available):
    # your code
    return
recipe = {"flour": 500, "sugar": 200, "eggs": 1}
available = {"flour": 1200, "sugar": 1200, "eggs": 5, "milk": 200}
assert cakes(recipe, available) == 2

recipe = {"apples": 3, "flour": 300, "sugar": 150, "milk": 100, "oil": 100}
available = {"sugar": 500, "flour": 2000, "milk": 2000}
assert cakes(recipe, available) == 0
def cakes(recipe, available):
    # your code
    num_list = {}
    if set(recipe.keys()) <= set(available.keys()):
        # for i in recipe.keys():
        #     if i in available.keys():
        #         num_list[i] = available.get(i) // recipe.get(i)
        #     else:
        #         return 0
        # return min(num_list.values())
        return min([ available.get(i) // recipe.get(i) if i in available.keys() else 0 for i in recipe.keys() ])
    else:
        return 0

1 个赞
def cakes(recipe, available):
    result = []
    for i in recipe:
        if i not in available.keys():
            return 0
        else:
            result.append(available[i] // recipe[i])
    return min(result)
def cakes(recipe, available):
    # your code
    if set(recipe.keys()).issubset(set(available.keys())):
        return 0
    else:
        return min([available[k]//recipe[k] for k in recipe.keys()])
def cakes(recipe, available):
    # your code
    return min([available[k]//recipe[k] for k in recipe.keys()]) if set(recipe.keys()).issubset(set(available.keys())) else 0

def cakes(recipe, available)->int:
    return min([available.get(i)//recipe.get(i) if i in available.keys() else 0 for i in recipe.keys()])
def cakes(recipe, available):
    return min(available.get(i)//v for i, v in recipe.items()) if len(set(available.keys()) & set(recipe.keys())) == len(recipe) else 0
def cakes(recipe, available):
    ##如果recipe的keys包含available的所有key,则开始判断数量
    if set(recipe.keys()).issubset(set(available.keys())):
       return min([available.get(i)//recipe.get(i) for i in recipe.keys()])
    ##双斜杠表示取整除
    else:
        return 0
def cakes(recipe, available):
    need_foods = recipe.keys()
    have_foods = available.keys()
    my_list = []
    for need_food in need_foods:
        if need_food not in have_foods:  # 如果菜谱食材,在实际食材中找不到,返回0
            my_list.append(0)
            break
        for have_food in have_foods:
            if need_food == have_food:  # 如果菜谱食材,在实际食材中能找到,找到相同食材
                my_list.append(available[have_food] // recipe[need_food])  # 通过整除,判断能做几份
    my_list.sort()  # 找到份数的最小值,即最终能做的份数
    return my_list[0]


recipe = {"flour": 500, "sugar": 200, "eggs": 1}
available = {"flour": 1200, "sugar": 1200, "eggs": 5, "milk": 200}
assert cakes(recipe, available) == 2

recipe = {"apples": 3, "flour": 300, "sugar": 150, "milk": 100, "oil": 100}
available = {"sugar": 500, "flour": 2000, "milk": 2000}
assert cakes(recipe, available) == 0

def cakes(recipe, available):
    res_list=[]
    recipe_keys=recipe.keys()
    for i in recipe_keys:
        if i not in available.keys():
            return 0
        res_list.append(available[i]//recipe[i])
    return min(res_list)

recipe = {"flour": 500, "sugar": 200, "eggs": 1}
available = {"flour": 1200, "sugar": 1200, "eggs": 5, "milk": 200}
assert cakes(recipe, available) == 2

recipe = {"apples": 3, "flour": 300, "sugar": 150, "milk": 100, "oil": 100}
available = {"sugar": 500, "flour": 2000, "milk": 2000}
assert cakes(recipe, available) == 0