0

im making a personal app for my job. i want to list indigent's for each thing i make so i know what im gonna need for the day in weight.

so lets say like i have 3 different things i can make

food 1 = sugar: 2, eggs: 4, cheese: 3
food 2 = sugar: 5, eggs: 4, brownSugar: 3
food 3 = flour: 2, eggs: 4, cheese: 3

so let's say today have to make 2 batches of food 1, 3 batches of food 2 and 1 batch of food 3.

it would output

Sugar: 19
Eggs: 24
Cheese: 9
brownSugar: 9
Flour: 2

If anyone can point me in the right direction on going about this that would be great.. i was thinking set every food up as an array and try to add them each ingredients value by name.

1
  • 1
    You should show what you tried. People aren't going to show interest in helping you if it doesn't even look like you tried. Commented Mar 17, 2019 at 4:02

3 Answers 3

0

Since your food is basically pairs (ingredient: count) it will ideally represented by Dictionary where key is your ingredient (right now use String, later could be any Hashable):

let food1 = ["sugar": 2, "eggs": 4, "cheese": 3]
let food2 = ["sugar": 5, "eggs": 4, "brownSugar": 3]
let food3 = ["flour": 2, "eggs": 4, "cheese": 3]

This allow us easily combine them by using Dictionary.merging(_, uniquingKeysWith:)

food1.merging(food2, uniquingKeysWith: +)
/// ["sugar": 7, "brownSugar": 3, "cheese": 3, "eggs": 8]

Here we merge food1 with food2 and combine amount of same ingredients by summing counts.

And to merge list of foods we could use reduce(into:, _)

[food1, food2, food3]
    .reduce(into: [:]) { sum, food in
        sum.merge(food, uniquingKeysWith: +)
    }
/// ["sugar": 7, "brownSugar": 3, "cheese": 6, "flour": 2, "eggs": 12]

Here we start with empty sum ([:]) and merging in it all foods.

Sign up to request clarification or add additional context in comments.

Comments

0

Kind of like:

var foodArray: [Foods] = []
// append all your foods in it when needed

func calculateAmount(foodChoices : [Foods]) -> (Sugar:Int, Eggs: Int, Cheese: Int) {

    var cheese = 0
    var sugar = 0
    var eggs = 0

    for g in foodChoices {
        if g.name == foodArray.name {
            cheese =+ g.cheese
            sugar =+ g.sugar
            eggs =+ g.eggs
        }
    }  
    return (Sugar, Eggs, Cheese)
}

You'll need to create a struct for your food and then you can work with it. Probably have better ways but this is the quickest I can think of

4 Comments

Awesome. Thanks I’ll give it a shot.
if you are confused on how to use the tuple that comes back, have a look at stackoverflow.com/questions/27531195/… Please mark the answer if it helped :) it would help me a lot
This isn't a good use case for a tuple. It doesn't scale well. There are only 3 ingredients now, but what if it eventually needs to support hundreds?
yeah I'm assuming there is only 5 ingredients as this point based on his question
0

function orderFood(food1count,food2count,food3count){
    var sugarCount = food1count*2+food2count*5;
    var eggCount = (food1count+food2count+food3count)*4;
    var flourCount = food3count*2;
    var cheeseCount = (food1count+food2count)*3;
    var brownSugarCount = food2count*3;
    console.log('sugar :'+sugarCount);
    console.log('egg :'+eggCount);
    console.log('flour :'+flourCount);
    console.log('cheese :'+cheeseCount);
}

orderFood(2,3,1);

1 Comment

This is not Swift

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.