Codewars刷题升级 (Python)5Kyu Pete, the baker 皮特,面包师

题目描述:

Pete likes to bake some cakes. He has some recipes and ingredients. Unfortunately he is not good in maths. Can you help him to find out, how many cakes he could bake considering his recipes?
皮特喜欢烤蛋糕。他有一些食谱和原材料。不幸的是,他数学不好。你能帮他计算出,根据他的食谱,他能烤多少蛋糕吗?
Write a function cakes(), which takes the recipe (object) and the available ingredients (also an object) and returns the maximum number of cakes Pete can bake (integer). For simplicity there are no units for the amounts (e.g. 1 lb of flour or 200 g of sugar are simply 1 or 200). Ingredients that are not present in the objects, can be considered as 0.
编写一个函数cakes(),它获取配方(对象)和可用成分(也是对象),并返回皮特可以烘焙的最大蛋糕数(整数)。为了简单起见,数量没有单位(例如1磅面粉或200克糖仅为1或200)。物品中不存在的成分可以被视为0。

Examples:
例子:

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

解题思路:

设定结果变量为一个很大的值,或者浮点数的无穷大float(‘inf’),循环食谱字典的键,如果原料里有此键且原料键值除以食谱键值向下取整小于结果变量,则将取整的值赋值给结果变量,否则返回0.

代码实现:

def cakes(recipe, available):
    # TODO: insert code
    result=9999
    for i in recipe:
        if i in available:
            if result > available[i] // recipe[i]:
                result=available[i] // recipe[i]
        else:
            return 0
    return result
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值