I have a dictionary containing my available food in my fridge and I want to print the recipes I can make but also the ones that I could make if I had enough items (showing the number of items missing) and also print the ones that have an ingredient missing and thus what ingredient it is. I just managed to show them all.
fridge = {
"orange" : 5,
"citron" : 3,
"sel" : 100,
"sucre" : 50,
"farine" : 250,
"tomates" : 6,
"huile" : 100,
"pomme" : 1,
"lait" : 1,
"pois chiche" : 1,
}
recipes = {
"jus_de_fruit" : {
"orange" : 3,
"citron" : 1,
"pomme" : 2
},
"salad" : {
"tomates" : 4,
"huile" : 10,
"sel" : 3
},
"crepes" : {
"lait" : 1,
"farine" : 250,
"oeufs" : 2
},
"glace" : {
"bac a glace" : 1,
"coulis abricot" : 1,
"batonnet" : 1
}
}
recette_dispo=[]
counter = 0
for recipe, recipe_contents in recipes.items():
if all(elem in list(fridge.keys()) for elem in list(recipes[recipe].keys())):
if all(recipe_contents[elem] <= fridge[elem] for elem in recipe_contents):
print(f'\n \n **** nice! you can make this recipe : {recipe} ****')
counter = 1
recette_dispo=recipe
else :
print(f'You have all ingredients but not enough in quantity for: {recipe}, you need: {list(recipe_contents.items())}')
else :
print(f'\n\n Tu nas pas tous les ingrédients pour : {recipe}')
print(f' You need these ingredients in order to make it : {list(recipe_contents.items())}')
print("\n I can make in total", counter, "recipe(s) from the ingredients in my fridge:",recette_dispo)
Output:
You have all ingredients but not enough in quantity for: jus_de_fruit, you need:[('orange', 3), ('citron', 1), ('pomme', 2)]
#here I would like instead only the missing numbers of ingredients
**** nice! you can make this recipe : salad ****
You don't have all ingredients for: crepes
You need these ingredients : [('lait', 1), ('farine', 250), ('oeufs', 2)]
#here I would like instead not all the ingredients of the recipe but only the ones missing in my fridge
Tu nas pas tous les ingrédients pour : glace
You need these ingredients: [('bac a glace', 1), ('coulis abricot', 1), ('batonnet', 1)]
I can make in total 1recipe(s) from the ingredients in my fridge: salade
CodePudding user response:
You can use this example how to find missing items from the fridge:
fridge = {
"orange": 5,
"citron": 3,
"sel": 100,
"sucre": 50,
"farine": 250,
"tomates": 6,
"huile": 100,
"pomme": 1,
"lait": 1,
"pois chiche": 1,
}
recipes = {
"jus_de_fruit": {"orange": 3, "citron": 1, "pomme": 2},
"salad": {"tomates": 4, "huile": 10, "sel": 3},
"crepes": {"lait": 1, "farine": 250, "oeufs": 2},
"glace": {"bac a glace": 1, "coulis abricot": 1, "batonnet": 1},
}
for recipe_name, ingredients in recipes.items():
print(f"Recipe: {recipe_name}")
if (missing := ingredients.keys() - fridge.keys()) :
print("Missing ingredients:", missing)
else:
common = {
k: fridge[k] - ingredients[k]
for k in ingredients.keys() & fridge.keys()
}
if all(v >= 0 for v in common.values()):
print("All ingredients are OK")
else:
for i, v in common.items():
if v < 0:
print(f"Ingredient {i}: missing {-v} items")
print("-" * 80)
Prints:
Recipe: jus_de_fruit
Ingredient pomme: missing 1 items
--------------------------------------------------------------------------------
Recipe: salad
All ingredients are OK
--------------------------------------------------------------------------------
Recipe: crepes
Missing ingredients: {'oeufs'}
--------------------------------------------------------------------------------
Recipe: glace
Missing ingredients: {'batonnet', 'coulis abricot', 'bac a glace'}
--------------------------------------------------------------------------------
CodePudding user response:
I tried to implement something similar to this years ago (in assembly language), but being a purchasing agent (in a previous life) did not provide me with the level of diligence required to update the inventory of my refrigerator, spice rack, etc. as I was using my ingredients. Things like ketchup, salad dressings, olives, and sliced pickles do not lend themselves well to bean-counting. Inventory management should be for pantries and deep freezers and should be avoided at the refrigerator level.
That's just my opinion, but if we take that into consideration and rely more upon the human touch we can make the primary mechanics of the program more elegant by using sets
:
frigo = {
"orange" : 5,
"citron" : 3,
"sel" : 100,
"sucre" : 50,
"farine" : 250,
"tomates" : 6,
"huile" : 100,
"pomme" : 1,
"lait" : 1,
"pois chiche" : 1,
}
recettes = {
"jus_de_fruit" : {
"orange" : 3,
"citron" : 1,
"pomme" : 2
},
"salad" : {
"tomates" : 4,
"huile" : 10,
"sel" : 3
},
"crepes" : {
"lait" : 1,
"farine" : 250,
"oeufs" : 2
},
"glace" : {
"bac a glace" : 1,
"coulis abricot" : 1,
"batonnet" : 1
}
}
frigoSet = set(frigo)
peutFaire = []
nePeutPasFaire = dict()
for recette in recettes:
recetteSet = set(recettes[recette])
recetteSet.difference_update(frigoSet)
if len(recetteSet) == 0:
peutFaire.append(recette)
else:
nePeutPasFaire.update({recette: list(recetteSet)})
print("Vous avez les ingrédients pour faire:")
for i in peutFaire:
print(" {}".format(i))
print("\nIl manque des ingrédients pour les recettes suivantes:")
for i in nePeutPasFaire:
print(" {}".format(i))
for j in nePeutPasFaire[i]:
print(" {}".format(j))
The output of the program is:
Vous avez les ingrédients pour faire:
jus_de_fruit
salad
Il manque des ingrédients pour les recettes suivantes:
crepes
oeufs
glace
coulis abricot
bac a glace
batonnet
You can then use your personal knowledge of what you have in your refrigerator to determine what you want to make. After all, you may decide not to make those crepes this morning because you want to save the few eggs you have for brouillés tomorrow instead.
That's just to give you an idea of how you can use sets to get the basic information without looping through a bunch of comparisons for every ingredient in every recipe from the outset.