Home > Enterprise >  How can I make my function use two helper functions to return the desired output?
How can I make my function use two helper functions to return the desired output?

Time:11-23

So I'm trying to create a function def mealcal(data, recipes, menu ) that takes in three strings like

menu = ["T-Bone", "T-Bone", "Green Salad1"]
recipes = ["Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
           "Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
           "T-Bone:Carrot*2,Steak Meat*1"]
data = ["Cabbage:4,2,0", "Carrot:9,1,5", "Fatty Pork:431,1,5",
        "Pineapple:7,1,0", "Steak Meat:5,20,10", "Rabbit Meat:7,2,20"]

and returns

[22295, 690, 405]

In order to do this I've created multiple helper functions to get to the result, and here is the code..

def totalcal(data):
    calorie_dict = {}
    for el in data:
      food, values = el.split(':')
      a, b, c = values.split(',')
      calories = (int(a) * 5)   (int(b) * 5)   (int(c) * 9)
      calorie_dict[food] = calories
    return calorie_dict
def rdict(recipes):
    recipes_splitted = {}
    for r in recipes:
        recipe_name, parts = r.split(":")
        recipe_parts = {}
        for part in parts.split(','):
            product, number = part.split('*')
            recipe_parts[product] = int(number)
        recipes_splitted[recipe_name] = recipe_parts
    return recipes_splitted
def extract(recipes, menu):
    data = rdict(menu)
    result = []
    for r in recipes:
        tmp = []
        for key in data[r]:
            tmp.append(f"{key}:{data[r][key]}")
        final_string = ""
        for i in range(len(tmp)):
            if i < len(tmp) - 1:
                final_string  = tmp[i]   ", "
            else:
                final_string  = tmp[i]
        result.append(final_string)
    return result
def recipecal(recipes, data):
    calories_list = []
    for recipe_name in rdict(recipes):
        recipe_parts = rdict(recipes)[recipe_name]
        calories = 0
        for product in recipe_parts:
            number = recipe_parts[product]
            calories  = number * totalcal(data)[product]
        calories_list.append(calories)
    return calories_list
print(extract(['T-Bone', 'Green Salad1','T-Bone'],["Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
"Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
"T-Bone:Carrot*2,Steak Meat*1"]))
def mealcal(menu, recipes, data):
    x = extract(recipes,menu)
    return recipecal(x,data)

So now, for mealcal(menu, recipes, data) to work, I must put the inputs recipes and menu into my helper function extract since the function extract will return the output like this

['Carrot:2, Steak Meat:1', 'Cabbage:10, Carrot:2, Pineapple:5', 'Carrot:2, Steak Meat:1']

I must then use this output of extract(recipes,menu) to put into the recipes portion of recipecal(recipes,data)

Data is a seperate string that will be inputted at the start and it will look like this

data = ["Cabbage:4,2,0", "Carrot:9,1,5", "Fatty Pork:431,1,5",
"Pineapple:7,1,0", "Steak Meat:5,20,10", "Rabbit Meat:7,2,20"] 

Furthermore, when the two inputs are put into recipecal(recipes,data), the function will return the numbers

[22295, 690, 405]

So now my issue is how can I program mealcal(menu, recipes, data) to put recipes and menu into extract(recipes, menu) and then take the output of extract(recipes, menu) and input it into the recipe portion of totalcal(recipes,data) and then return the output correctly??

Please use the simplest method of code possible. I'm not very strong in programming and I would like to understand the codes that I write. Thank you:)

CodePudding user response:

Here working code.

This part was correct

    x = extract(recipes,menu)
    return recipecal(x,data)

But there was few mistakes inside extract() which made problem.

It was using : instead of * - so it was creating product:number instead of product*number,

It didn't add name: before product1*number1,product1*number1

Rest is rather the same.

def rdict(recipes):
    recipes_splitted = {}
    for r in recipes:
        recipe_name, parts = r.split(":")
        recipe_parts = {}
        for part in parts.split(','):
            product, number = part.split('*')
            recipe_parts[product] = int(number)
        recipes_splitted[recipe_name] = recipe_parts
    return recipes_splitted

def totalcal(data):
    calorie_dict = {}
    for el in data:
        food, values = el.split(':')
        a, b, c = values.split(',')
        calories = (int(a) * 5)   (int(b) * 5)   (int(c) * 9)
        calorie_dict[food] = calories
    return calorie_dict

def extract(recipes, menu):
    recipes = rdict(recipes)
    result = []
    for r in menu:
        tmp = []
        for key in recipes[r]:
            tmp.append(f"{key}*{recipes[r][key]}")  
        final_string = f"{r}:"
        for i in range(len(tmp)):
            if i < len(tmp) - 1:
                final_string  = tmp[i]   ","
            else:
                final_string  = tmp[i]
        result.append(final_string)
    return result

def recipecal(recipes, data):
    calories_list = []
    for recipe_name in rdict(recipes):
        recipe_parts = rdict(recipes)[recipe_name]
        calories = 0
        for product in recipe_parts:
            number = recipe_parts[product]
            calories  = number * totalcal(data)[product]
        calories_list.append(calories)
    return calories_list

def mealcal(menu, recipes, data):
    recipes = extract(recipes, menu)
    return recipecal(recipes, data)
    # OR
    #return recipecal(extract(recipes, menu), data)

# --- main ---

data = [
    "Cabbage:4,2,0",
    "Carrot:9,1,5",
    "Fatty Pork:431,1,5",
    "Pineapple:7,1,0",
    "Steak Meat:5,20,10",
    "Rabbit Meat:7,2,20"
] 

recipes = [
    "Pork Stew:Cabbage*5,Carrot*1,Fatty Pork*10",
    "Green Salad1:Cabbage*10,Carrot*2,Pineapple*5",
    "T-Bone:Carrot*2,Steak Meat*1"
]

#menu = ['T-Bone',    'Green Salad1', 'T-Bone']
menu = ['Pork Stew', 'Green Salad1', 'T-Bone']

result = mealcal(menu, recipes, data)

print(result)

menu = ['T-Bone',    'Green Salad1', 'T-Bone']
#menu = ['Pork Stew', 'Green Salad1', 'T-Bone']

result = []

for single_product in menu: 
    #print(single_product)  

    # `menu` was a list `[string, string, string]
    # `single_product` is a `string` but `mealcal` needs list `[string]`
    single_result = mealcal( [single_product] , recipes, data)

    #print(single_result)

    result = result   single_result   # [a]   [b] gives [a, b]
    
print(result)
  • Related