Cooking Recipes

You are given an array recipes containing a list of cooking recipes. Find the recipes containing a key ingredient keyword. Example: beef, eggs, etc.

Example

Input:

let recipes = ["Put eggs in a frying pan", "Cut the beef", "Boil the beef"]
let keyword = "beef"

Expected value/output:

["Cut the beef", "Boil the beef"]

[collapse]
Hint

Simply iterate through every word in every recipe and check if it matches the keyword.

[collapse]
Solution

let recipes = ["Put eggs in a frying pan", "Cut the beef", "Boil the beef"]
let keyword = "beef"

import Foundation

func searchRecipes(recipes:[String], keyword:String) -> [String] {
    var foundRecipes = [String]()
    for recipe in recipes {
        if recipe.contains(keyword) == true {
            foundRecipes.append(recipe)
        }
    }
    return foundRecipes
}

var foundRecipes = searchRecipes(recipes: recipes, keyword: keyword)

[collapse]
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.00 out of 5)
Loading...
Subscribe
We send about one email per week with our latest tutorials and updates
Never display this again :)